lib/opentok/client.rb in opentok-2.4.0.beta.1 vs lib/opentok/client.rb in opentok-2.4.0

- old
+ new

@@ -1,10 +1,12 @@ +require "opentok/constants" require "opentok/exceptions" require "opentok/extensions/hash" require "active_support/inflector" require "httparty" +require "jwt" module OpenTok # @private For internal use by the SDK. class Client include HTTParty @@ -15,19 +17,38 @@ # debug_output $stdout def initialize(api_key, api_secret, api_url, ua_addendum="") self.class.base_uri api_url self.class.headers({ - "X-TB-PARTNER-AUTH" => "#{api_key}:#{api_secret}", "User-Agent" => "OpenTok-Ruby-SDK/#{VERSION}" + (ua_addendum ? " #{ua_addendum}" : "") }) @api_key = api_key + @api_secret = api_secret end + def generate_jwt(api_key, api_secret) + now = Time.now.to_i + payload = { + :iss => api_key, + :iat => now, + :exp => now + AUTH_EXPIRE + } + token = JWT.encode payload, api_secret, 'HS256', :ist => 'project' + token + end + + def generate_headers(extra_headers = {}) + defaults = { "X-OPENTOK-AUTH" => generate_jwt(@api_key, @api_secret) } + defaults.merge extra_headers + end + def create_session(opts) opts.extend(HashExtensions) - response = self.class.post("/session/create", :body => opts.camelize_keys!) + response = self.class.post("/session/create", { + :body => opts.camelize_keys!, + :headers => generate_headers + }) case response.code when (200..300) response when 403 raise OpenTokAuthenticationError, "Authentication failed while creating a session. API Key: #{@api_key}" @@ -39,13 +60,13 @@ end def start_archive(session_id, opts) opts.extend(HashExtensions) body = { "sessionId" => session_id }.merge(opts.camelize_keys!) - response = self.class.post("/v2/partner/#{@api_key}/archive", { + response = self.class.post("/v2/project/#{@api_key}/archive", { :body => body.to_json, - :headers => { "Content-Type" => "application/json" } + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response when 400 @@ -62,11 +83,13 @@ rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end def get_archive(archive_id) - response = self.class.get("/v2/partner/#{@api_key}/archive/#{archive_id}") + response = self.class.get("/v2/project/#{@api_key}/archive/#{archive_id}", { + :headers => generate_headers + }) case response.code when 200 response when 400 raise OpenTokArchiveError, "The archive could not be retrieved. The Archive ID was invalid: #{archive_id}" @@ -77,16 +100,18 @@ end rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end - def list_archives(offset, count) + def list_archives(offset, count, sessionId) query = Hash.new query[:offset] = offset unless offset.nil? query[:count] = count unless count.nil? - response = self.class.get("/v2/partner/#{@api_key}/archive", { - :query => query.empty? ? nil : query + query[:sessionId] = sessionId unless sessionId.nil? + response = self.class.get("/v2/project/#{@api_key}/archive", { + :query => query.empty? ? nil : query, + :headers => generate_headers }) case response.code when 200 response when 403 @@ -97,12 +122,12 @@ rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end def stop_archive(archive_id) - response = self.class.post("/v2/partner/#{@api_key}/archive/#{archive_id}/stop", { - :headers => { "Content-Type" => "application/json" } + response = self.class.post("/v2/project/#{@api_key}/archive/#{archive_id}/stop", { + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 200 response when 400 @@ -119,11 +144,11 @@ rescue StandardError => e raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}" end def delete_archive(archive_id) - response = self.class.delete("/v2/partner/#{@api_key}/archive/#{archive_id}", { - :headers => { "Content-Type" => "application/json" } + response = self.class.delete("/v2/project/#{@api_key}/archive/#{archive_id}", { + :headers => generate_headers("Content-Type" => "application/json") }) case response.code when 204 response when 403