Class: Kaltura::KalturaClientBase

Inherits:
Object
  • Object
show all
Defined in:
lib/kaltura_client_base.rb

Direct Known Subclasses

KalturaClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ KalturaClientBase

Returns a new instance of KalturaClientBase.



50
51
52
53
54
55
56
57
58
59
# File 'lib/kaltura_client_base.rb', line 50

def initialize(config)
  @ks = KalturaNotImplemented
	@should_log = false
	@config = config
	@calls_queue = []
	
	if @config.logger != nil
		@should_log = true
	end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



45
46
47
# File 'lib/kaltura_client_base.rb', line 45

def config
  @config
end

#is_multirequestObject (readonly)

Returns the value of attribute is_multirequest.



47
48
49
# File 'lib/kaltura_client_base.rb', line 47

def is_multirequest
  @is_multirequest
end

#ksObject

Returns the value of attribute ks.



46
47
48
# File 'lib/kaltura_client_base.rb', line 46

def ks
  @ks
end

#responseHeadersObject (readonly)

Returns the value of attribute responseHeaders.



48
49
50
# File 'lib/kaltura_client_base.rb', line 48

def responseHeaders
  @responseHeaders
end

Instance Method Details

#add_param(params, name, value) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/kaltura_client_base.rb', line 217

def add_param(params, name, value)
  if value == KalturaNotImplemented
    return
	elsif value == nil
		params[name + '__null'] = ''
	elsif value.is_a? Hash
		if value.empty?
			add_param(params, "#{name}:-", "");
		else
			value.each do |sub_name, sub_value|
				add_param(params, "#{name}:#{sub_name}", sub_value);
			end
		end
    elsif value.is_a? Array
      if value.empty?
        add_param(params, "#{name}:-", "");
      else
        value.each_with_index do |ele, i|
          if ele.is_a? KalturaObjectBase
            add_param(params, "#{name}:#{i}", ele.to_params)
          end
        end
      end
	elsif value.is_a? KalturaObjectBase
		add_param(params, name, value.to_params)
	else
		params[name] = value
	end
end

#do_http_request(url, params) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/kaltura_client_base.rb', line 163

def do_http_request(url, params)
  
  options = {:method => :post, :url => url, :payload => params}
  
  options.merge!(:headers => @config.requestHeaders)
  options.merge!(:timeout => @config.timeout) if @config.timeout
  options.merge!(:open_timeout => @config.timeout) if @config.timeout
        
  res = RestClient::Request.execute(options)
  
  return res
end

#do_multirequestObject



204
205
206
# File 'lib/kaltura_client_base.rb', line 204

def do_multirequest()
	return do_queue()
end

#do_queueObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/kaltura_client_base.rb', line 74

def do_queue()
  begin 
			@responseHeaders = {}
			start_time = Time.now
	
			if @calls_queue.length == 0
				@is_multirequest = false
				return []
			end
			
			log('service url: [' + @config.service_url + ']')
	
			# append the basic params
			params = {}
			add_param(params, "format", @config.format)
			add_param(params, "clientTag", @config.client_tag)
	
			url = @config.service_url+"/api_v3/index.php?service="
			if (@is_multirequest)
				url += "multirequest"
				i = 1
				@calls_queue.each do |call|
					call_params = call.get_params_for_multirequest(i)
					params.merge!(call_params)
					i = i.next
				end
			else
				call = @calls_queue[0]
				url += call.service + "&action=" + call.action
				params.merge!(call.params)
			end
	
			# reset
			@calls_queue = []
			@is_multirequest = false
	
			signature = signature(params)
			add_param(params, "kalsig", signature)
	
			log("url: " + url)
			log("params: " + params.to_yaml)
						
			result = do_http_request(url, params)

			@responseHeaders = result.headers
			log("server: [" + result.headers[:x_me].to_s + "], session: [" + result.headers[:x_kaltura_session].to_s + "]")
	
			log("result (xml): " + result.body)
			
			result_object = parse_to_objects(result.body)
	
			log("result (object yaml dump): " + result_object.to_yaml)
			
			end_time = Time.now
			
			log("execution time for [#{url}]: [#{end_time - start_time}]")
			
			return result_object
			
		rescue KalturaAPIError => e
  		raise e
		rescue Exception => e
		  raise KalturaAPIError.new("KALTURA_RUBY_CLIENT_ERROR", e.to_s)
		end			
end

#escape(s) ⇒ Object

Escapes a query parameter. Taken from RFuzz



248
249
250
251
252
# File 'lib/kaltura_client_base.rb', line 248

def escape(s)
	s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
		'%' + $1.unpack('H2'*$1.size).join('%').upcase
	}.tr(' ', '+')
end

#generate_session(admin_secret, user_id, kaltura_session_type, partner_id, expiry = 86400, privileges = nil) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/kaltura_client_base.rb', line 260

def generate_session(admin_secret, user_id, kaltura_session_type, partner_id, expiry=86400, privileges=nil)

    ks = "#{partner_id};#{partner_id};#{Time.now.to_i + expiry};#{kaltura_session_type};#{rand.to_s.gsub("0.", "")};#{user_id};#{privileges};"

    digest_generator = OpenSSL::Digest::Digest.new('sha1')
    
    digest_generator.update(admin_secret)  
    digest_generator.update(ks) 
    
    digest = digest_generator.hexdigest 
                                
    signature = digest + "|" + ks                             
    b64 = Base64.encode64(signature)
    cleaned = b64.gsub("\n","")
    
    @ks = cleaned
end

#get_serve_urlObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/kaltura_client_base.rb', line 140

def get_serve_url()
			url = @config.service_url+"/api_v3/index.php?service="

			call = @calls_queue[0]
			url += call.service + "&action=" + call.action
			params = call.params
			
			# reset
			@calls_queue = []
			@is_multirequest = false
			
			query_string = ''
  params.each do |name, value|
    query_string << "&#{name}=#{CGI::escape(value.to_s)}"
  end

  serve_url = "#{url}#{query_string}"
  
  log("serve_url: " + serve_url)
  
  return serve_url      
end

#is_error(doc) ⇒ Object



196
197
198
# File 'lib/kaltura_client_base.rb', line 196

def is_error(doc)
	return doc.elements["xml/result/error/message"] && doc.elements["xml/result/error/code"];
end

#log(msg) ⇒ Object



254
255
256
257
258
# File 'lib/kaltura_client_base.rb', line 254

def log(msg)
	if @should_log
		config.logger.log(Logger::INFO, msg)
	end
end

#parse_to_objects(data) ⇒ Object



176
177
178
# File 'lib/kaltura_client_base.rb', line 176

def parse_to_objects(data)
	parse_xml_to_objects(data)
end

#parse_xml_to_objects(xml) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/kaltura_client_base.rb', line 180

def parse_xml_to_objects(xml)
	doc = REXML::Document.new(xml)
	raise_exception_if_error(doc)
	doc.elements.each('xml/result') do | element |
		return KalturaClassFactory.object_from_xml(element)
	end
end

#queue_service_action_call(service, action, params = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kaltura_client_base.rb', line 61

def queue_service_action_call(service, action, params = {})
	# in start session partner id is optional (default nil). if partner id was not set, use the one in the config
	if !params.key?('partnerId')
		params['partnerId'] = config.partner_id
		params.delete('partnerId__null') if params.key?('partnerId__null')
	end	
	
	add_param(params, 'ks', @ks);
	
	call = KalturaServiceActionCall.new(service, action, params);
	@calls_queue.push(call);
end

#raise_exception_if_error(doc) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/kaltura_client_base.rb', line 188

def raise_exception_if_error(doc)
	if is_error(doc)
		code = doc.elements["xml/result/error/code"].text
		message = doc.elements["xml/result/error/message"].text
		raise KalturaAPIError.new(code, message)
	end
end

#signature(params) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/kaltura_client_base.rb', line 208

def signature(params)
	kParams = params.select { |key, value| !value.is_a?(File) }
	str = kParams.keys.map {|key| key.to_s }.sort.map {|key|
		"#{key}#{params[key]}"
	}.join("")
	
	Digest::MD5.hexdigest(str)
end

#start_multirequestObject



200
201
202
# File 'lib/kaltura_client_base.rb', line 200

def start_multirequest()
	@is_multirequest = true
end