Class: WscSdk::Client

Inherits:
Object
  • Object
show all
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.

Examples:

Usage

api_key     = "[your-api-key]"
access_key  = "[your-api-access-key]"
client      = WscSdk::Client.new(api_key, access_key)

Usage with Environment Variables: Shell Config

export WSC_API_KEY=[your-api-key]
export WSC_API_ACCESS_KEY=[your-api-access-key]

Usage with Environment Variables: Client Config

client      = WscSdk::Client.new()

Constant Summary collapse

MAX_REQUESTS_PER_SECOND =

Maximum number of requests to execute per second.

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new instance of the client.

Parameters:

  • options (Hash) (defaults to: {})

    A hash of options.

Options Hash (options):

  • :api_key (String)

    The account's API key. Used to generated signatures for the API request headers.

  • :access_key (String)

    The account's access key. Used to identify the profile making the API requests.

  • :hostname (String)

    The hostname to use when generating requests to the API.

  • :logger (Logger)

    The logger to use when generating log output in the SDK.

  • :version (String)

    The version of the API to use when generating requests to the API.



61
62
63
64
65
66
67
68
69
# File 'lib/wsc_sdk/client.rb', line 61

def initialize(options={})
  @api_key        = options.fetch(:api_key,     WscSdk.configuration.api_key)
  @access_key     = options.fetch(:access_key,  WscSdk.configuration.access_key)
  @hostname       = options.fetch(:hostname,    WscSdk.configuration.hostname)
  @path_version   = options.fetch(:version,     WscSdk.configuration.version)
  @base_path      = ["", "api", @path_version].join("/")
  @logger         = options.fetch(:logger,      WscSdk.configuration.logger)
  self.class.base_uri(@hostname)
end

Instance Attribute Details

#access_keyObject

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_keyObject

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_pathObject

Returns the value of attribute base_path



35
36
37
# File 'lib/wsc_sdk/client.rb', line 35

def base_path
  @base_path
end

#hostnameObject

Returns the value of attribute hostname



35
36
37
# File 'lib/wsc_sdk/client.rb', line 35

def hostname
  @hostname
end

#last_request_timeObject (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

#loggerObject

Returns the value of attribute logger



35
36
37
# File 'lib/wsc_sdk/client.rb', line 35

def logger
  @logger
end

#path_versionObject

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_instanceWscSdk::Client

Get an instance of the Client built using the SDK configuration object.

Returns:



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.

Parameters:

  • file_path (String)

    A valid path to a file that should be converted to base64 encoding

Returns:

  • (String)

    The base64 encoded string that represents the file.

Raises:



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.

Parameters:

  • path (String)

    The path to request.

  • options (Hash) (defaults to: {})

    A hash of options.



192
193
194
# File 'lib/wsc_sdk/client.rb', line 192

def delete(path, options={})
  send_request(:delete, path, options)
end

#full_request_path(path) ⇒ String

Get the fully qualified request path for an API request.

Parameters:

  • path (String)

    The path of the API request.

Returns:

  • (String)

    The fully qualified request path.



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.

Parameters:

  • request_path (String)

    The request path to generate the headers for.

  • options (Hash) (defaults to: {})

    A hash of options

Options Hash (options):

  • :timestamp (Time)

    The timestamp of the request.

  • :content_type (String) — default: "multipart/form-data"

    The content type to return in the headers.

Returns:

  • (Hash)

    A hash of request 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, options={})
  timestamp     = options.fetch(:timestamp, Time.now.to_i)
  request_path  = request_path.split("?").first.chomp("/")
  data          = "#{timestamp}:#{request_path}:#{api_key}"
  signature     = OpenSSL::HMAC.hexdigest("SHA256", api_key, data)
  content_type  = options.fetch(:content_type, "multipart/form-data")

  {
    'User-Agent'      => WscSdk::USER_AGENT,
    'Wsc-Access-Key'  => access_key,
    'Wsc-Timestamp'   => timestamp.to_s,
    'Wsc-Signature'   => signature,
    'Content-Type'    => content_type
  }
end

#get(path, options = {}) ⇒ Object

Make a GET request against the API.

Parameters:

  • path (String)

    The path to request.

  • options (Hash) (defaults to: {})

    A hash of options.



144
145
146
# File 'lib/wsc_sdk/client.rb', line 144

def get(path, options={})
  send_request(:get, path, options)
end

#infoObject

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_streamsWsc::Endpoints::Transcoders

Access the /transcoders endpoints

Returns:

  • (Wsc::Endpoints::Transcoders)

    An instance of the Transcoders endpoint class.



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.

Parameters:

  • path (String)

    The path to request.

  • options (Hash) (defaults to: {})

    A hash of options.



180
181
182
# File 'lib/wsc_sdk/client.rb', line 180

def patch(path, options={})
  send_request(:patch, path, options)
end

#post(path, options = {}) ⇒ Object

Make a POST request against the API.

Parameters:

  • path (String)

    The path to request.

  • options (Hash) (defaults to: {})

    A hash of options.



156
157
158
# File 'lib/wsc_sdk/client.rb', line 156

def post(path, options={})
  send_request(:post, path, options)
end

#put(path, options = {}) ⇒ Object

Make a GET request against the API.

Parameters:

  • path (String)

    The path to request.

  • options (Hash) (defaults to: {})

    A hash of options.



168
169
170
# File 'lib/wsc_sdk/client.rb', line 168

def put(path, options={})
  send_request(:put, path, options)
end

#request_endpoint(method, path, body = {}) ⇒ Object

A generic way to request against an endpoint in the API

Parameters:

  • method (Symbol)

    The method to use to formulate the request.

  • path (String)

    The path to the endpoint. This shoudl not include the protocol, domain or base path information. Only the endpoint path itself.

  • body (Hash) (defaults to: {})

    A Hash of payload data to send in the request. This data will not be presented in the request if it's a GET or DELETE method.



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
    options = {}
    options = { body: body } unless [:get, :delete].include?(method)
    response = send_request(method, path, options)
    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

Parameters:

  • options (Hash) (defaults to: {})

    A hash of options.

Options Hash (options):

  • :params (Hash)

    A hash of query params or body content to be sent in the request.

  • :pagination (Hash)

    Pagination configuration options.

  • :filters (Hash)

    Filter configuration options.



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(options={})
  pagination  = options.fetch(:pagination, nil)
  filters     = options.fetch(:filters, nil)
  params      = options.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_targetsWsc::Endpoints::StreamTargets

Access the /stream_targets endpoints

Returns:

  • (Wsc::Endpoints::StreamTargets)

    An instance of the StreamTargets endpoint class.



114
115
116
# File 'lib/wsc_sdk/client.rb', line 114

def stream_targets
  WscSdk::Endpoints::StreamTargets.new(self)
end

#to_sString

Outputs the details of the client as a string.

Returns:

  • (String)

    The details of the client.



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

#transcodersWsc::Endpoints::Transcoders

Access the /transcoders endpoints

Returns:

  • (Wsc::Endpoints::Transcoders)

    An instance of the Transcoders endpoint class.



123
124
125
# File 'lib/wsc_sdk/client.rb', line 123

def transcoders
  WscSdk::Endpoints::Transcoders.new(self)
end