lib/opentok/client.rb in opentok-2.2.4 vs lib/opentok/client.rb in opentok-2.3.0
- old
+ new
@@ -1,13 +1,18 @@
require "opentok/exceptions"
+require "extensions/hash"
+require "active_support/inflector"
require "httparty"
module OpenTok
# @private For internal use by the SDK.
class Client
include HTTParty
+
+ default_timeout 1 # Set HTTParty default timeout (open/read) to 1 second
+
# TODO: expose a setting for http debugging for developers
# debug_output $stdout
def initialize(api_key, api_secret, api_url)
self.class.base_uri api_url
@@ -17,24 +22,27 @@
})
@api_key = api_key
end
def create_session(opts)
- response = self.class.post("/session/create", :body => opts)
+ opts.extend(HashExtensions)
+ response = self.class.post("/session/create", :body => opts.camelize_keys!)
case response.code
when (200..300)
response
when 403
raise OpenTokAuthenticationError, "Authentication failed while creating a session. API Key: #{@api_key}"
else
raise OpenTokError, "Failed to create session. Response code: #{response.code}"
end
+ rescue StandardError => e
+ raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end
def start_archive(session_id, opts)
- body = { "sessionId" => session_id }
- body["name"] = opts[:name] unless opts[:name].nil?
+ opts.extend(HashExtensions)
+ body = { "sessionId" => session_id }.merge(opts.camelize_keys!)
response = self.class.post("/v2/partner/#{@api_key}/archive", {
:body => body.to_json,
:headers => { "Content-Type" => "application/json" }
})
case response.code
@@ -49,10 +57,12 @@
when 409
raise OpenTokArchiveError, "The archive could not be started. The session could be peer-to-peer or the session is already being recorded."
else
raise OpenTokArchiveError, "The archive could not be started"
end
+ 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}")
case response.code
@@ -63,10 +73,12 @@
when 403
raise OpenTokAuthenticationError, "Authentication failed while retrieving an archive. API Key: #{@api_key}"
else
raise OpenTokArchiveError, "The archive could not be retrieved."
end
+ rescue StandardError => e
+ raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end
def list_archives(offset, count)
query = Hash.new
query[:offset] = offset unless offset.nil?
@@ -80,10 +92,12 @@
when 403
raise OpenTokAuthenticationError, "Authentication failed while retrieving archives. API Key: #{@api_key}"
else
raise OpenTokArchiveError, "The archives could not be retrieved."
end
+ 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" }
@@ -100,10 +114,12 @@
when 409
raise OpenTokArchiveError, "The archive could not be stopped. The archive is not currently recording."
else
raise OpenTokArchiveError, "The archive could not be stopped."
end
+ 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" }
@@ -116,9 +132,10 @@
when 409
raise OpenTokArchiveError, "The archive could not be deleted. The status must be 'available', 'deleted', or 'uploaded'. Archive ID: #{archive_id}"
else
raise OpenTokArchiveError, "The archive could not be deleted."
end
+ rescue StandardError => e
+ raise OpenTokError, "Failed to connect to OpenTok. Response code: #{e.message}"
end
-
end
end