ruby-bandwidth-iris 7.3.2 → 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac14c8ba1c0f0e31bc32931eed443c3e3ed8db5b742bca7759bd476fcc632686
4
- data.tar.gz: 74113994a7b82692fc1e82d5774426393648990869c933d9bf92017eb7044af3
3
+ metadata.gz: bd81d5a6d13a8b102719a5f2b793068ba6e389a6022f352548ca1e5fa31e85fd
4
+ data.tar.gz: 1ed7e328742ad6cb2a51a81dfbb88530067b92f6dd2627344050dbc304b98075
5
5
  SHA512:
6
- metadata.gz: 6812782ba2e456150fc9a98e809d36dbd434ecb74b01de94fcd89b109193a621703d4f838b35d94b1668f43a3eba0b043c1bc2412a3b54d574a72a3883688200
7
- data.tar.gz: 5abf667171c05245e0d6cf8ee3368a684ee2808c6a0b07429c4a9242ec12ff05eec4a3eb97ff0a9df7a62ae1cf7eda6b5842ceb070beb177e34bbe7335fb2eb6
6
+ metadata.gz: 5c3a68c1001b7705b4c79b4e1f05b89b72017593c26582aed1ef38c1447dd3e3362f456da307da8a434d9bd4e9189a880399a299ef679dda7c891e452e3abefb
7
+ data.tar.gz: b9f2c513adbe909b29ddbb8b3c10ba2b7d6ed75286892b502a71dff4b33b8dcf1b367238104ffbda08cacd64496137dfa14a6a138851c3f810a01eb280b2336f
data/README.md CHANGED
@@ -73,14 +73,20 @@ to run the tests
73
73
  require 'ruby-bandwidth-iris'
74
74
 
75
75
  # Using directly
76
- client = BandwidthIris::Client.new('accountId', 'userName', 'password')
76
+ client = BandwidthIris::Client.new('accountId', 'userName', 'password') # Basic Auth
77
+ client = BandwidthIris::Client.new({access_token: 'accessToken'}) # Bearer Token Auth
78
+ client = BandwidthIris::Client.new({client_id: 'clientId', client_secret: 'clientSecret'}) # OAuth 2 using Client Credentials
77
79
  sites = BandwidthIris::Site.list(client)
78
80
 
79
81
  # Or you can use default client instance (do this only once)
80
82
  BandwidthIris::Client.global_options = {
81
83
  :account_id => 'accountId',
82
84
  :username => 'userName',
83
- :password => 'password'
85
+ :password => 'password',
86
+ :access_token => 'accessToken',
87
+ :access_token_expiration => Time.now + 3600
88
+ :client_id => 'client_id',
89
+ :client_secret => 'client_secret'
84
90
  }
85
91
 
86
92
  # Now you can call any functions without first arg 'client'
@@ -19,8 +19,12 @@ module BandwidthIris
19
19
  end
20
20
  options = options || @@global_options
21
21
  account_id = options[:account_id] unless account_id
22
- user_name = options[:user_name] || options[:username] unless user_name
22
+ user_name = options[:user_name] || options[:username] unless user_name
23
23
  password = options[:password] unless password
24
+ @access_token = options[:access_token]
25
+ @access_token_expiration = options[:access_token_expiration] || Time.now + 3600
26
+ @client_id = options[:client_id]
27
+ @client_secret = options[:client_secret]
24
28
  options[:api_endpoint] = @@global_options[:api_endpoint] unless options[:api_endpoint]
25
29
  options[:api_version] = @@global_options[:api_version] unless options[:api_version]
26
30
  api_endpoint = options[:api_endpoint] || "https://dashboard.bandwidth.com"
@@ -31,8 +35,15 @@ module BandwidthIris
31
35
  @create_connection = lambda{||
32
36
  Faraday.new(api_endpoint) { |faraday|
33
37
  # To make this gem compatible with Faraday v1 and v2, the basic_auth middleware can't be used because it was removed in v2
34
- faraday.request :authorization, 'Basic', Base64.strict_encode64("#{user_name}:#{password}")
35
- #faraday.response :logger
38
+ if @access_token && @access_token_expiration > Time.now + 60
39
+ faraday.request :authorization, 'Bearer', @access_token
40
+ elsif @client_id && @client_secret
41
+ refresh_auth_token
42
+ faraday.request :authorization, 'Bearer', @access_token
43
+ else
44
+ faraday.request :authorization, 'Basic', Base64.strict_encode64("#{user_name}:#{password}")
45
+ end
46
+ # faraday.response :logger
36
47
  faraday.headers['Accept'] = 'application/xml'
37
48
  faraday.headers['user-agent'] = 'Ruby-Bandwidth-Iris'
38
49
  faraday.response :follow_redirects # use Faraday::FollowRedirects::Middleware
@@ -67,9 +78,24 @@ module BandwidthIris
67
78
  items.last
68
79
  end
69
80
 
81
+ def refresh_auth_token
82
+ token_url = 'https://api.bandwidth.com/api/v1/oauth2/token'
83
+ response = Faraday.new do |faraday|
84
+ faraday.request :url_encoded
85
+ faraday.request :authorization, :basic, @client_id, @client_secret
86
+ @set_adapter.call(faraday)
87
+ end.post(token_url, {grant_type: 'client_credentials'})
88
+ if response.status >= 400
89
+ raise Errors::GenericError.new(response.status, response.reason_phrase, response.headers, response.body)
90
+ end
91
+ body = JSON.parse(response.body)
92
+ @access_token = body['access_token']
93
+ @access_token_expiration = Time.now + body['expires_in']
94
+ end
95
+
70
96
  # Make HTTP request to IRIS API
71
97
  # @param method [Symbol] http method to make
72
- # @param path [string] path of url (exclude api verion and endpoint) to make call
98
+ # @param path [string] path of url (exclude api version and endpoint) to make call
73
99
  # @param data [Hash] data which will be sent with request (for :get and :delete request they will be sent with query in url)
74
100
  # @return [Array] array with 2 elements: parsed data of response and response headers
75
101
  def make_request(method, path, data = {})
@@ -89,7 +115,7 @@ module BandwidthIris
89
115
 
90
116
  # Makes an HTTP request for file uploads
91
117
  # @param method [Symbol] http method to make
92
- # @param path [string] path of url (exclude api verion and endpoint) to make call
118
+ # @param path [string] path of url (exclude api version and endpoint) to make call
93
119
  # @param data [string] the raw binary string representing the file to upload
94
120
  # @param content_type [string] the content type of the request
95
121
  # @return [Array] array with 2 elements: parsed data of response and response headers
@@ -102,7 +128,7 @@ module BandwidthIris
102
128
 
103
129
  # Makes an HTTP request for a file download
104
130
  # @param method [Symbol] http method to make
105
- # @param path [string] path of url (exclude api verion and endpoint) to make call
131
+ # @param path [string] path of url (exclude api version and endpoint) to make call
106
132
  # @param data [Hash] data which will be sent with request (for :get and :delete request they will be sent with query in url)
107
133
  # @return [string] raw response from the API
108
134
  def make_request_file_download(method, path, data = {})
@@ -39,7 +39,7 @@ module BandwidthIris
39
39
  def get_sip_peer(peer_id)
40
40
  item = @client.make_request(:get, "#{@client.concat_account_path(SITE_PATH)}/#{id}/sippeers/#{peer_id}")[0][:sip_peer]
41
41
  item[:site_id] = id
42
- puts item
42
+ # puts item
43
43
  item
44
44
  end
45
45
 
@@ -1,4 +1,4 @@
1
1
  module BandwidthIris
2
2
  # Version of this gem
3
- VERSION = "7.3.2"
3
+ VERSION = "7.4.0"
4
4
  end
@@ -20,8 +20,8 @@ describe BandwidthIris::Client do
20
20
  expect(Client.get_id_from_location_header('http://localhost/path1/path2/id')).to eql('id')
21
21
  end
22
22
  it 'should raise error if location is missing or nil' do
23
- expect{Client.get_id_from_location_header('')}.to raise_error
24
- expect{Client.get_id_from_location_header(nil)}.to raise_error
23
+ expect{Client.get_id_from_location_header('')}.to raise_error(StandardError)
24
+ expect{Client.get_id_from_location_header(nil)}.to raise_error(StandardError)
25
25
  end
26
26
  end
27
27
 
@@ -51,12 +51,22 @@ describe BandwidthIris::Client do
51
51
 
52
52
  describe '#make_request' do
53
53
  client = nil
54
+ token_client = nil
55
+ expired_token_client = nil
56
+ client_credentials_client = nil
57
+
54
58
  before :each do
55
59
  client = Helper.get_client()
60
+ token_client = Helper.get_token_client()
61
+ expired_token_client = Helper.get_expired_token_client()
62
+ client_credentials_client = Helper.get_client_credentials_client()
56
63
  end
57
64
 
58
65
  after :each do
59
66
  client.stubs.verify_stubbed_calls()
67
+ token_client.stubs.verify_stubbed_calls()
68
+ expired_token_client.stubs.verify_stubbed_calls()
69
+ client_credentials_client.stubs.verify_stubbed_calls()
60
70
  end
61
71
 
62
72
  it 'should pass basic auth headers' do
@@ -65,6 +75,29 @@ describe BandwidthIris::Client do
65
75
  expect(client.make_request(:get, '/test-auth')).to eql([{:echoed_auth=>"Basic #{Base64.strict_encode64('username:password')}"}, {}])
66
76
  end
67
77
 
78
+ it 'should pass bearer auth header using token' do
79
+ token_client.stubs.get('/v1.0/test-auth-token') { |env| [200, {}, "<Result><EchoedAuth>#{env[:request_headers]['Authorization']}</EchoedAuth></Result>"] }
80
+ expect(token_client.make_request(:get, '/test-auth-token')).to eql([{:echoed_auth=>"Bearer accessToken"}, {}])
81
+ expect(token_client.instance_variable_get(:@access_token)).to eql('accessToken')
82
+ expect(token_client.instance_variable_get(:@access_token_expiration)).to be_a(Time)
83
+ end
84
+
85
+ it 'should refresh expired token and pass bearer auth header' do
86
+ expired_token_client.stubs.post('https://api.bandwidth.com/api/v1/oauth2/token') { |env| [200, {}, '{"access_token":"newAccessToken","expires_in":3600}'] }
87
+ expired_token_client.stubs.get('/v1.0/test-auth-expired-token') { |env| [200, {}, "<Result><EchoedAuth>#{env[:request_headers]['Authorization']}</EchoedAuth></Result>"] }
88
+ expect(expired_token_client.make_request(:get, '/test-auth-expired-token')).to eql([{:echoed_auth=>"Bearer newAccessToken"}, {}])
89
+ expect(expired_token_client.instance_variable_get(:@access_token)).to eql('newAccessToken')
90
+ expect(expired_token_client.instance_variable_get(:@access_token_expiration)).to be_a(Time)
91
+ end
92
+
93
+ it 'should use client credentials to get token and pass bearer auth header' do
94
+ client_credentials_client.stubs.post('https://api.bandwidth.com/api/v1/oauth2/token') { |env| [200, {}, '{"access_token":"clientCredentialsAccessToken","expires_in":3600}'] }
95
+ client_credentials_client.stubs.get('/v1.0/test-auth-client-credentials') { |env| [200, {}, "<Result><EchoedAuth>#{env[:request_headers]['Authorization']}</EchoedAuth></Result>"] }
96
+ expect(client_credentials_client.make_request(:get, '/test-auth-client-credentials')).to eql([{:echoed_auth=>"Bearer clientCredentialsAccessToken"}, {}])
97
+ expect(client_credentials_client.instance_variable_get(:@access_token)).to eql('clientCredentialsAccessToken')
98
+ expect(client_credentials_client.instance_variable_get(:@access_token_expiration)).to be_a(Time)
99
+ end
100
+
68
101
  it 'should make GET request and return xml data' do
69
102
  client.stubs.get('/v1.0/path1') { |env| [200, {}, '<Result><Test>data</Test></Result>'] }
70
103
  client.stubs.get('/v1.0/path2?testField=10') { |env| [200, {'Location'=>'url'}, '<Root><TestValue>10</TestValue><DataArray>1</DataArray><DataArray>2</DataArray><BoolValue>true</BoolValue><BoolValue2>false</BoolValue2><DateTimeValue>2015-05-29T01:02:03Z</DateTimeValue></Root>'] }
data/spec/helper.rb CHANGED
@@ -30,6 +30,23 @@ module Helper
30
30
  Client.new('accountId', 'username', 'password')
31
31
  end
32
32
 
33
+ def self.get_token_client()
34
+ Client.new({access_token: 'accessToken'})
35
+ end
36
+
37
+ def self.get_expired_token_client()
38
+ Client.new({
39
+ access_token: 'expiredAccessToken',
40
+ access_token_expiration: Time.now - 3600,
41
+ client_id: 'clientId',
42
+ client_secret: 'clientSecret'
43
+ })
44
+ end
45
+
46
+ def self.get_client_credentials_client()
47
+ Client.new({client_id: 'clientId', client_secret: 'clientSecret'})
48
+ end
49
+
33
50
  def self.setup_environment()
34
51
  Client.global_options[:account_id] = 'accountId'
35
52
  Client.global_options[:username] = 'username'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-bandwidth-iris
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.3.2
4
+ version: 7.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Belchikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-01-27 00:00:00.000000000 Z
11
+ date: 2026-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder