Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions custom_templates/configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module {{moduleName}}
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token

# Defines the URL used to obtain access tokens (Bearer) used with OAuth2.
# @return [String]
attr_accessor :access_token_url

# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
Expand Down Expand Up @@ -175,16 +179,16 @@ module {{moduleName}}
{{#authMethods}}
{{#isOAuth}}
@access_token_expiration = nil
@access_token_url = '{{tokenUrl}}'
@access_token_getter = Proc.new {
access_token_valid = @access_token && (@access_token_expiration.nil? || @access_token_expiration > Time.now + 60)
next @access_token if access_token_valid
next unless @client_id && @client_secret

puts "Refreshing access token..." if @debugging
# obtain new access token using client credentials
token_url = '{{tokenUrl}}'
auth_header = 'Basic ' + ["#{@client_id}:#{@client_secret}"].pack('m').delete("\r\n")
conn = Faraday.new(url: token_url) do |faraday|
conn = Faraday.new(url: @access_token_url) do |faraday|
faraday.request :url_encoded
faraday.adapter Faraday.default_adapter
end
Expand Down
8 changes: 6 additions & 2 deletions lib/bandwidth-sdk/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class Configuration
# Defines the access token (Bearer) used with OAuth2.
attr_accessor :access_token

# Defines the URL used to obtain access tokens (Bearer) used with OAuth2.
# @return [String]
attr_accessor :access_token_url

# Defines a Proc used to fetch or refresh access tokens (Bearer) used with OAuth2.
# Overrides the access_token if set
# @return [Proc]
Expand Down Expand Up @@ -193,16 +197,16 @@ def initialize
@force_ending_format = false
@logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
@access_token_expiration = nil
@access_token_url = 'https://api.bandwidth.com/api/v1/oauth2/token'
@access_token_getter = Proc.new {
access_token_valid = @access_token && (@access_token_expiration.nil? || @access_token_expiration > Time.now + 60)
next @access_token if access_token_valid
next unless @client_id && @client_secret

puts "Refreshing access token..." if @debugging
# obtain new access token using client credentials
token_url = 'https://api.bandwidth.com/api/v1/oauth2/token'
auth_header = 'Basic ' + ["#{@client_id}:#{@client_secret}"].pack('m').delete("\r\n")
conn = Faraday.new(url: token_url) do |faraday|
conn = Faraday.new(url: @access_token_url) do |faraday|
faraday.request :url_encoded
faraday.adapter Faraday.default_adapter
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/calls_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/conferences_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.return_binary_data = true
config.host = '127.0.0.1:4010'
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/endpoints_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/media_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.return_binary_data = true
config.host = '127.0.0.1:4010'
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/messages_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/mfa_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/multi_channel_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/phone_number_lookup_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/recordings_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.return_binary_data = true
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/statistics_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/toll_free_verification_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down
3 changes: 1 addition & 2 deletions spec/unit/api/transcriptions_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
before(:all) do
Bandwidth.configure do |config|
config.debugging = true
config.username = BW_USERNAME
config.password = BW_PASSWORD
config.access_token = 'abcd1234'
config.ignore_operation_servers = true
config.host = '127.0.0.1:4010'
end
Expand Down