Class: BandwidthIris::Client

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

Constant Summary collapse

@@global_options =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id = nil, user_name = nil, password = nil, options = nil) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
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
# File 'lib/bandwidth-iris/client.rb', line 13

def initialize ( = nil, user_name = nil, password = nil, options = nil)
  if user_name == nil && password == nil && options == nil
    if  && .is_a?(Hash)
      options = 
       = nil
    end
  end
  options = options || @@global_options
   = options[:account_id] unless 
  user_name = options[:user_name] || options[:username]  unless user_name
  password = options[:password] unless password
  options[:api_endpoint] = @@global_options[:api_endpoint] unless options[:api_endpoint]
  options[:api_version] = @@global_options[:api_version] unless options[:api_version]
  api_endpoint = options[:api_endpoint] || "https://dashboard.bandwidth.com"
  api_version = options[:api_version] || "v1.0"

  @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|
      # To make this gem compatible with Faraday v1 and v2, the basic_auth middleware can't be used because it was removed in v2
      faraday.request :authorization, 'Basic', Base64.strict_encode64("#{user_name}:#{password}")
      #faraday.response :logger
      faraday.headers['Accept'] = 'application/xml'
      faraday.headers['user-agent'] = 'Ruby-Bandwidth-Iris'
      faraday.response :follow_redirects # use Faraday::FollowRedirects::Middleware
      @set_adapter.call(faraday)
    }
  }
  @concat_account_path = lambda {|path| "/accounts/#{}" + (if path then (if path[0] == "/" then path else "/#{path}" end) else '' end) }
  @api_endpoint = api_endpoint
  @api_version = api_version
end

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



47
48
49
# File 'lib/bandwidth-iris/client.rb', line 47

def api_endpoint
  @api_endpoint
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



47
48
49
# File 'lib/bandwidth-iris/client.rb', line 47

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)


64
65
66
67
68
# File 'lib/bandwidth-iris/client.rb', line 64

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_optionsObject

Return global options



52
53
54
# File 'lib/bandwidth-iris/client.rb', line 52

def Client.global_options
  @@global_options
end

.global_options=(v) ⇒ Object

Set global options



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

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

Instance Method Details

#build_xml(data) ⇒ Object



140
141
142
143
# File 'lib/bandwidth-iris/client.rb', line 140

def build_xml(data)
   doc = build_doc(data, data.keys.first.to_s().camelcase(:upper))
   doc.values.first.to_xml({:root => doc.keys.first, :skip_types => true, :indent => 0 })
end

#check_response(response) ⇒ Object

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

Parameters:

  • response

    response object

Raises:



123
124
125
126
127
# File 'lib/bandwidth-iris/client.rb', line 123

def check_response(response)
  parsed_body = parse_xml(response.body || '')
  raise Errors::GenericError.new(response.status, response.reason_phrase, response.headers, parsed_body) if response.status >= 400
  parsed_body
end

#concat_account_path(path) ⇒ Object

Build url path like /accounts/<account-id>/<path>



130
131
132
# File 'lib/bandwidth-iris/client.rb', line 130

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

#create_connectionFaraday::Connection

Return new configured connection object

Returns:

  • (Faraday::Connection)

    connection



136
137
138
# File 'lib/bandwidth-iris/client.rb', line 136

def create_connection()
  @create_connection.call()
end

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

Make HTTP request to IRIS 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 data of response and response headers



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bandwidth-iris/client.rb', line 75

def make_request(method, path, data = {})
  connection = @create_connection.call()
  response =  if method == :get || method == :delete
                d  = camelcase(data)
                connection.run_request(method, @build_path.call(path), nil, nil) do |req|
                  req.params = d unless d == nil || d.empty?
                end
              else
                xml_to_send = build_xml(data) # help debug
                connection.run_request(method, @build_path.call(path), xml_to_send, {'Content-Type' => 'application/xml'})
              end
  body = check_response(response)
  [body || {}, symbolize(response.headers || {})]
end

#make_request_file_download(method, path, data = {}) ⇒ string

Makes an HTTP request for a file download

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:

  • (string)

    raw response from the API



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bandwidth-iris/client.rb', line 108

def make_request_file_download(method, path, data = {})
  connection = @create_connection.call()
  response =  if method == :get || method == :delete
                d  = camelcase(data)
                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), build_xml(data), {'Content-Type' => 'application/xml'})
              end
  return response.body
end

#make_request_file_upload(method, path, data, content_type) ⇒ Array

Makes an HTTP request for file uploads

Parameters:

  • method (Symbol)

    http method to make

  • path (string)

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

  • data (string)

    the raw binary string representing the file to upload

  • content_type (string)

    the content type of the request

Returns:

  • (Array)

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



96
97
98
99
100
101
# File 'lib/bandwidth-iris/client.rb', line 96

def make_request_file_upload(method, path, data, content_type)
  connection = @create_connection.call()
  response = connection.run_request(method, @build_path.call(path), data, {'Content-Type' => content_type})
  body = check_response(response)
  [body || {}, symbolize(response.headers || {})]
end