Class: BandwidthIris::Client
- Inherits:
-
Object
- Object
- BandwidthIris::Client
- Defined in:
- lib/bandwidth-iris/client.rb
Constant Summary collapse
- @@global_options =
{}
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
Class Method Summary collapse
-
.get_id_from_location_header(location) ⇒ String
Extract id from location header.
-
.global_options ⇒ Object
Return global options.
-
.global_options=(v) ⇒ Object
Set global options.
Instance Method Summary collapse
- #build_xml(data) ⇒ Object
-
#check_response(response) ⇒ Object
Check response object and raise error if status code >= 400.
-
#concat_account_path(path) ⇒ Object
Build url path like /accounts/<account-id>/<path>.
-
#create_connection ⇒ Faraday::Connection
Return new configured connection object.
-
#initialize(account_id = nil, user_name = nil, password = nil, options = nil) ⇒ Client
constructor
A new instance of Client.
-
#make_request(method, path, data = {}) ⇒ Array
Make HTTP request to IRIS API.
-
#make_request_file_download(method, path, data = {}) ⇒ string
Makes an HTTP request for a file download.
-
#make_request_file_upload(method, path, data, content_type) ⇒ Array
Makes an HTTP request for file uploads.
- #refresh_auth_token ⇒ Object
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 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bandwidth-iris/client.rb', line 13 def initialize (account_id = nil, user_name = nil, password = nil, = nil) if user_name == nil && password == nil && == nil if account_id && account_id.is_a?(Hash) = account_id account_id = nil end end = || @@global_options account_id = [:account_id] unless account_id user_name = [:user_name] || [:username] unless user_name password = [:password] unless password @access_token = [:access_token] @access_token_expiration = [:access_token_expiration] || Time.now + 3600 @client_id = [:client_id] @client_secret = [:client_secret] [:api_endpoint] = @@global_options[:api_endpoint] unless [:api_endpoint] [:api_version] = @@global_options[:api_version] unless [:api_version] api_endpoint = [:api_endpoint] || "https://dashboard.bandwidth.com" api_version = [: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 if @access_token && @access_token_expiration > Time.now + 60 faraday.request :authorization, 'Bearer', @access_token elsif @client_id && @client_secret refresh_auth_token faraday.request :authorization, 'Bearer', @access_token else faraday.request :authorization, 'Basic', Base64.strict_encode64("#{user_name}:#{password}") end # 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/#{account_id}" + (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_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
58 59 60 |
# File 'lib/bandwidth-iris/client.rb', line 58 def api_endpoint @api_endpoint end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
58 59 60 |
# File 'lib/bandwidth-iris/client.rb', line 58 def api_version @api_version end |
Class Method Details
.get_id_from_location_header(location) ⇒ String
Extract id from location header
75 76 77 78 79 |
# File 'lib/bandwidth-iris/client.rb', line 75 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_options ⇒ Object
Return global options
63 64 65 |
# File 'lib/bandwidth-iris/client.rb', line 63 def Client. @@global_options end |
.global_options=(v) ⇒ Object
Set global options
68 69 70 |
# File 'lib/bandwidth-iris/client.rb', line 68 def Client.(v) @@global_options = v end |
Instance Method Details
#build_xml(data) ⇒ Object
166 167 168 169 |
# File 'lib/bandwidth-iris/client.rb', line 166 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
149 150 151 152 153 |
# File 'lib/bandwidth-iris/client.rb', line 149 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>
156 157 158 |
# File 'lib/bandwidth-iris/client.rb', line 156 def concat_account_path(path) @concat_account_path.call(path) end |
#create_connection ⇒ Faraday::Connection
Return new configured connection object
162 163 164 |
# File 'lib/bandwidth-iris/client.rb', line 162 def create_connection() @create_connection.call() end |
#make_request(method, path, data = {}) ⇒ Array
Make HTTP request to IRIS API
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/bandwidth-iris/client.rb', line 101 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
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/bandwidth-iris/client.rb', line 134 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
122 123 124 125 126 127 |
# File 'lib/bandwidth-iris/client.rb', line 122 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 |
#refresh_auth_token ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bandwidth-iris/client.rb', line 81 def refresh_auth_token token_url = 'https://api.bandwidth.com/api/v1/oauth2/token' response = Faraday.new do |faraday| faraday.request :url_encoded faraday.request :authorization, :basic, @client_id, @client_secret @set_adapter.call(faraday) end.post(token_url, {grant_type: 'client_credentials'}) if response.status >= 400 raise Errors::GenericError.new(response.status, response.reason_phrase, response.headers, response.body) end body = JSON.parse(response.body) @access_token = body['access_token'] @access_token_expiration = Time.now + body['expires_in'] end |