Class: WscSdk::Client
- Inherits:
-
Object
- Object
- WscSdk::Client
- Includes:
- HTTParty
- Defined in:
- lib/wsc_sdk/client.rb
Overview
A client to manage credentials and act as an entrypoint for the SDK and all API interactions.
Constant Summary collapse
- MAX_REQUESTS_PER_SECOND =
Maximum number of requests to execute per second.
5
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#base_path ⇒ Object
Returns the value of attribute base_path.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#last_request_time ⇒ Object
readonly
Returns the value of attribute last_request_time.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#path_version ⇒ Object
Returns the value of attribute path_version.
Class Method Summary collapse
-
.configured_instance ⇒ WscSdk::Client
Get an instance of the Client built using the SDK configuration object.
-
.file_to_base64(file_path) ⇒ String
Convert a file to a base64 encoded string.
Instance Method Summary collapse
-
#delete(path, options = {}) ⇒ Object
Make a GET request against the API.
-
#full_request_path(path) ⇒ String
Get the fully qualified request path for an API request.
-
#generate_headers(request_path, options = {}) ⇒ Hash
Get the correct headers for an API request, including the properly formed signed authentication headers.
-
#get(path, options = {}) ⇒ Object
Make a GET request against the API.
-
#info ⇒ Object
Get debug information about the client configuration.
-
#initialize(options = {}) ⇒ Client
constructor
Create a new instance of the client.
-
#live_streams ⇒ Wsc::Endpoints::Transcoders
Access the /transcoders endpoints.
-
#patch(path, options = {}) ⇒ Object
Make a GET request against the API.
-
#post(path, options = {}) ⇒ Object
Make a POST request against the API.
-
#put(path, options = {}) ⇒ Object
Make a GET request against the API.
-
#request_endpoint(method, path, body = {}) ⇒ Object
A generic way to request against an endpoint in the API.
-
#request_params(options = {}) ⇒ Object
Generate a set of request params for an API request.
-
#stream_targets ⇒ Wsc::Endpoints::StreamTargets
Access the /stream_targets endpoints.
-
#to_s ⇒ String
Outputs the details of the client as a string.
-
#transcoders ⇒ Wsc::Endpoints::Transcoders
Access the /transcoders endpoints.
Constructor Details
#initialize(options = {}) ⇒ Client
Create a new instance of the client.
61 62 63 64 65 66 67 68 69 |
# File 'lib/wsc_sdk/client.rb', line 61 def initialize(={}) @api_key = .fetch(:api_key, WscSdk.configuration.api_key) @access_key = .fetch(:access_key, WscSdk.configuration.access_key) @hostname = .fetch(:hostname, WscSdk.configuration.hostname) @path_version = .fetch(:version, WscSdk.configuration.version) @base_path = ["", "api", @path_version].join("/") @logger = .fetch(:logger, WscSdk.configuration.logger) self.class.base_uri(@hostname) end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key
35 36 37 |
# File 'lib/wsc_sdk/client.rb', line 35 def access_key @access_key end |
#api_key ⇒ Object
Returns the value of attribute api_key
35 36 37 |
# File 'lib/wsc_sdk/client.rb', line 35 def api_key @api_key end |
#base_path ⇒ Object
Returns the value of attribute base_path
35 36 37 |
# File 'lib/wsc_sdk/client.rb', line 35 def base_path @base_path end |
#hostname ⇒ Object
Returns the value of attribute hostname
35 36 37 |
# File 'lib/wsc_sdk/client.rb', line 35 def hostname @hostname end |
#last_request_time ⇒ Object (readonly)
Returns the value of attribute last_request_time
37 38 39 |
# File 'lib/wsc_sdk/client.rb', line 37 def last_request_time @last_request_time end |
#logger ⇒ Object
Returns the value of attribute logger
35 36 37 |
# File 'lib/wsc_sdk/client.rb', line 35 def logger @logger end |
#path_version ⇒ Object
Returns the value of attribute path_version
35 36 37 |
# File 'lib/wsc_sdk/client.rb', line 35 def path_version @path_version end |
Class Method Details
.configured_instance ⇒ WscSdk::Client
Get an instance of the Client built using the SDK configuration object.
399 400 401 |
# File 'lib/wsc_sdk/client.rb', line 399 def self.configured_instance @@client_instance ||= WscSdk::Client.new end |
.file_to_base64(file_path) ⇒ String
Convert a file to a base64 encoded string.
412 413 414 415 |
# File 'lib/wsc_sdk/client.rb', line 412 def self.file_to_base64(file_path) raise ArgumentError.new("Could not convert the specified file to Base64, the file couldn't be found: #{file_path}") unless File.exists?(file_path) Base64.encode64(File.open(file_path, "rb").read) end |
Instance Method Details
#delete(path, options = {}) ⇒ Object
Make a GET request against the API.
192 193 194 |
# File 'lib/wsc_sdk/client.rb', line 192 def delete(path, ={}) send_request(:delete, path, ) end |
#full_request_path(path) ⇒ String
Get the fully qualified request path for an API request.
80 81 82 |
# File 'lib/wsc_sdk/client.rb', line 80 def full_request_path(path) [ base_path, path ].join("/") end |
#generate_headers(request_path, options = {}) ⇒ Hash
Get the correct headers for an API request, including the properly formed signed authentication headers.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/wsc_sdk/client.rb', line 270 def generate_headers(request_path, ={}) = .fetch(:timestamp, Time.now.to_i) request_path = request_path.split("?").first.chomp("/") data = "#{}:#{request_path}:#{api_key}" signature = OpenSSL::HMAC.hexdigest("SHA256", api_key, data) content_type = .fetch(:content_type, "multipart/form-data") { 'User-Agent' => WscSdk::USER_AGENT, 'Wsc-Access-Key' => access_key, 'Wsc-Timestamp' => .to_s, 'Wsc-Signature' => signature, 'Content-Type' => content_type } end |
#get(path, options = {}) ⇒ Object
Make a GET request against the API.
144 145 146 |
# File 'lib/wsc_sdk/client.rb', line 144 def get(path, ={}) send_request(:get, path, ) end |
#info ⇒ Object
Get debug information about the client configuration
326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/wsc_sdk/client.rb', line 326 def info { access_key: ("#{access_key[0..5]}****#{access_key[-6..-1]}" rescue "invalid"), api_key: ("#{api_key[0..5]}****#{api_key[-6..-1]}" rescue "invalid"), base_path: base_path, hostname: hostname, path_version: path_version, logger: (logger.class.name rescue "nil"), logger_level: (logger.level rescue "nil"), throttle: MAX_REQUESTS_PER_SECOND, user_agent: WscSdk::USER_AGENT } end |
#live_streams ⇒ Wsc::Endpoints::Transcoders
Access the /transcoders endpoints
105 106 107 |
# File 'lib/wsc_sdk/client.rb', line 105 def live_streams WscSdk::Endpoints::LiveStreams.new(self) end |
#patch(path, options = {}) ⇒ Object
Make a GET request against the API.
180 181 182 |
# File 'lib/wsc_sdk/client.rb', line 180 def patch(path, ={}) send_request(:patch, path, ) end |
#post(path, options = {}) ⇒ Object
Make a POST request against the API.
156 157 158 |
# File 'lib/wsc_sdk/client.rb', line 156 def post(path, ={}) send_request(:post, path, ) end |
#put(path, options = {}) ⇒ Object
Make a GET request against the API.
168 169 170 |
# File 'lib/wsc_sdk/client.rb', line 168 def put(path, ={}) send_request(:put, path, ) end |
#request_endpoint(method, path, body = {}) ⇒ Object
A generic way to request against an endpoint in the API
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/wsc_sdk/client.rb', line 209 def request_endpoint(method, path, body={}) begin = {} = { body: body } unless [:get, :delete].include?(method) response = send_request(method, path, ) return nil if response.body.to_s.empty? begin data = JSON.parse(response.body) return data rescue JSON::ParserError => jpe return build_exception_response(500, "Bad JSON returned by request: [#{method.to_s.upcase}] #{path}", jpe) end rescue Exception => e # return build_exception_response(500, "Exception while requesting endpoint: [#{method.to_s.upcase}] #{path}", e) raise e end end |
#request_params(options = {}) ⇒ Object
Generate a set of request params for an API request
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/wsc_sdk/client.rb', line 300 def request_params(={}) pagination = .fetch(:pagination, nil) filters = .fetch(:filters, nil) params = .fetch(:params, {}) if pagination params[:page] = pagination.fetch(:page, 1) params[:per_page] = pagination.fetch(:per_page, 25) end if filters if filters.is_a?(Hash) filter_index = 0 filters.each do |key, value| params["filter[#{filter_index}][field]"] = key params["filter[#{filter_index}][eq]"] = value filter_index += 1 end end end params end |
#stream_targets ⇒ Wsc::Endpoints::StreamTargets
Access the /stream_targets endpoints
114 115 116 |
# File 'lib/wsc_sdk/client.rb', line 114 def stream_targets WscSdk::Endpoints::StreamTargets.new(self) end |
#to_s ⇒ String
Outputs the details of the client as a string.
89 90 91 |
# File 'lib/wsc_sdk/client.rb', line 89 def to_s "WSC SDK Client > hostname: #{hostname} | version: #{path_version} | base_path: #{base_path}" end |
#transcoders ⇒ Wsc::Endpoints::Transcoders
Access the /transcoders endpoints
123 124 125 |
# File 'lib/wsc_sdk/client.rb', line 123 def transcoders WscSdk::Endpoints::Transcoders.new(self) end |