Class: Bandwidth::Client

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

Overview

Catapult client class. It is used by any api related class

Constant Summary

@@global_options =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_id = nil, api_token = nil, api_secret = nil, api_endpoint = 'https://api.catapult.inetwork.com', api_version = 'v1') ⇒ Client

Initializer

Examples:

client = Client.new("userId", "token", "secret")
client = Client.new(:user_id => "userId", :api_token => "token", :api_secret => "secret") # with has of options
client = Client.new() #options from Client.global_options will be used here

Parameters:

  • user_id (String|Hash) (defaults to: nil)

    user id to connect to Catapult API. If value is hash it will be used as options storage

  • api_token (String) (defaults to: nil)

    token to connect to Catapult API.

  • api_secret (String) (defaults to: nil)

    catapult API secret

  • api_endpoint (String) (defaults to: 'https://api.catapult.inetwork.com')

    base url of Catapult API

  • api_version (String) (defaults to: 'v1')

    version of Catapult API

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bandwidth/client.rb', line 21

def initialize (user_id = nil, api_token = nil, api_secret = nil, api_endpoint = 'https://api.catapult.inetwork.com', api_version = 'v1')
  if api_token == nil && api_secret == nil
    if  user_id == nil
      user_id = @@global_options
    end
    if user_id.is_a?(Hash)
      opts = user_id
      api_version = opts[:api_version] if opts[:api_version]
      api_endpoint = opts[:api_endpoint] if opts[:api_endpoint]
      api_secret = opts[:api_secret]
      api_token = opts[:api_token]
      user_id = opts[:user_id]
    end
  end
  raise Errors::MissingCredentialsError.new() if (user_id || '').length == 0 || (api_token || '').length == 0 || (api_secret || '').length == 0
  @concat_user_path = lambda {|path| "/users/#{user_id}" + (if path[0] == "/" then path else "/#{path}" end) }
  @build_path = lambda {|path| "/#{api_version}" + (if path[0] == "/" then path else "/#{path}" end) }
  @set_adapter = lambda {|faraday| faraday.adapter(Faraday.default_adapter)}
  @create_connection = lambda{||
    Faraday.new(api_endpoint) { |faraday|
      faraday.basic_auth(api_token, api_secret)
      faraday.headers['Accept'] = 'application/json'
      faraday.headers['User-Agent'] = "ruby-bandwidth/v#{Bandwidth::VERSION}"
      @set_adapter.call(faraday)
    }
  }
  @api_endpoint = api_endpoint
  @api_version = api_version
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint



51
52
53
# File 'lib/bandwidth/client.rb', line 51

def api_endpoint
  @api_endpoint
end

#api_versionObject (readonly)

Returns the value of attribute api_version



51
52
53
# File 'lib/bandwidth/client.rb', line 51

def api_version
  @api_version
end

Class Method Details

.get_id_from_location_header(location) ⇒ String

Extract id from location header

Parameters:

  • location (String)

    location header value

Returns:

  • (String)

    extracted id

Raises:

  • (StandardError)


70
71
72
73
74
# File 'lib/bandwidth/client.rb', line 70

def Client.get_id_from_location_header(location)
  items = (location || '').split('/')
  raise StandardError.new('Missing id in the location header') if items.size < 2
  items.last
end

.global_optionsHash

Return global options

Returns:

  • (Hash)

    Options



57
58
59
# File 'lib/bandwidth/client.rb', line 57

def Client.global_options
  @@global_options
end

.global_options=(v) ⇒ Object

Set global options

Parameters:

  • v (Hash)

    Options to set



63
64
65
# File 'lib/bandwidth/client.rb', line 63

def Client.global_options=(v)
  @@global_options = v
end

Instance Method Details

#check_response(response) ⇒ Object

Check response object and raise error if status code >= 400

Parameters:

  • response

    response object



98
99
100
101
102
103
# File 'lib/bandwidth/client.rb', line 98

def check_response(response)
  if response.status >= 400
    parsed_body = JSON.parse(response.body)
    raise Errors::GenericError.new(parsed_body['code'], parsed_body['message'])
  end
end

#concat_user_path(path) ⇒ Object

Build url path like /users/<user-id>/<path>



106
107
108
# File 'lib/bandwidth/client.rb', line 106

def concat_user_path(path)
  @concat_user_path.call(path)
end

#create_connectionFaraday::Connection

Return new configured connection object

Returns:

  • (Faraday::Connection)

    connection



112
113
114
# File 'lib/bandwidth/client.rb', line 112

def create_connection()
  @create_connection.call()
end

#make_request(method, path, data = {}) ⇒ Array

Make HTTP request to Catapult API

Parameters:

  • method (Symbol)

    http method to make

  • path (String)

    path of url (exclude api verion and endpoint) to make call

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

    data which will be sent with request (for :get and :delete request they will be sent with query in url)

Returns:

  • (Array)

    array with 2 elements: parsed json data of response and response headers



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bandwidth/client.rb', line 81

def make_request(method, path, data = {})
  d  = camelcase(data)
  connection = @create_connection.call()
  response =  if method == :get || method == :delete
                connection.run_request(method, @build_path.call(path), nil, nil) do |req|
                  req.params = d unless d == nil || d.empty?
                end
              else
                connection.run_request(method, @build_path.call(path), d.to_json(), {'Content-Type' => 'application/json'})
              end
  check_response(response)
  r = if response.body.strip().size > 0 then symbolize(JSON.parse(response.body)) else {} end
  [r, symbolize(response.headers || {})]
end