Class: AvalaraSdk::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/avalara_sdk/configuration.rb

Constant Summary collapse

PRODUCTION_OPENID_CONFIG_URL =
'https://identity.avalara.com/.well-known/openid-configuration'
SANDBOX_OPENID_CONFIG_URL =
'https://ai-sbx.avlr.sh/.well-known/openid-configuration'
QA_OPENID_CONFIG_URL =
'https://ai-awsfqa.avlr.sh/.well-known/openid-configuration'
FALLBACK_TOKEN_URL =
'https://identity.avalara.com/connect/token'
FALLBACK_DEVICE_AUTHORIZATION_URL =
'https://identity.avalara.com/connect/token'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Configuration

Returns a new instance of Configuration.

Yields:

  • (_self)

Yield Parameters:



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/avalara_sdk/configuration.rb', line 159

def initialize
  @base_path = ''
  @environment=''
  @app_name=''
  @app_version=''
  @machine_name=''
  @client_id=''
  @client_secret=''
  @bearer_token=''
  @token_url=''
  @device_authorization_url=''
  @test_base_path=''
  @test_token_url=''
  @test_device_authorization_url=''
  @username=''
  @password=''
  @api_key = {}
  @api_key_prefix = {}
  @client_side_validation = true
  @ssl_verify = true
  @ssl_verify_mode = nil
  @ssl_ca_file = nil
  @ssl_client_cert = nil
  @ssl_client_key = nil
  @middlewares = []
  @request_middlewares = []
  @response_middlewares = []
  @timeout = 60
  @debugging = false
  @inject_format = false
  @force_ending_format = false
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)

  yield(self) if block_given?
end

Instance Attribute Details

#api_keyHash

Defines API keys used with API Key authentications.

Examples:

parameter name is “api_key”, API key is “xxx” (e.g. “api_key=xxx” in query string)

config.api_key['api_key'] = 'xxx'

Returns:

  • (Hash)

    key: parameter name, value: parameter value (API key)



33
34
35
# File 'lib/avalara_sdk/configuration.rb', line 33

def api_key
  @api_key
end

#api_key_prefixHash

Defines API key prefixes used with API Key authentications.

Examples:

parameter name is “Authorization”, API key prefix is “Token” (e.g. “Authorization: Token xxx” in headers)

config.api_key_prefix['api_key'] = 'Token'

Returns:

  • (Hash)

    key: parameter name, value: API key prefix



41
42
43
# File 'lib/avalara_sdk/configuration.rb', line 41

def api_key_prefix
  @api_key_prefix
end

#app_nameString

Defines the application name

Returns:

  • (String)


147
148
149
# File 'lib/avalara_sdk/configuration.rb', line 147

def app_name
  @app_name
end

#app_versionString

Defines the application version

Returns:

  • (String)


152
153
154
# File 'lib/avalara_sdk/configuration.rb', line 152

def app_version
  @app_version
end

#base_pathObject

Defines url base path



25
26
27
# File 'lib/avalara_sdk/configuration.rb', line 25

def base_path
  @base_path
end

#bearer_tokenObject

The OAuth2 Avalara Identity Bearer Token that will be used for API access.



72
73
74
# File 'lib/avalara_sdk/configuration.rb', line 72

def bearer_token
  @bearer_token
end

#client_idObject

Defines the ClientId used for the OAuth2 Client Credentials flow.



66
67
68
# File 'lib/avalara_sdk/configuration.rb', line 66

def client_id
  @client_id
end

#client_secretObject

The ClientSecret used for the OAuth2 Client Credentials flow.



69
70
71
# File 'lib/avalara_sdk/configuration.rb', line 69

def client_secret
  @client_secret
end

#client_side_validationtrue, false

Set this to false to skip client side validation in the operation. Default to true.

Returns:

  • (true, false)


101
102
103
# File 'lib/avalara_sdk/configuration.rb', line 101

def client_side_validation
  @client_side_validation
end

#debuggingtrue, false

Set this to enable/disable debugging. When enabled (set to true), HTTP request/response details will be logged with ‘logger.debug` (see the `logger` attribute). Default to false.

Returns:

  • (true, false)


79
80
81
# File 'lib/avalara_sdk/configuration.rb', line 79

def debugging
  @debugging
end

#device_authorization_urlObject

Defines Device Authorization URL for OAuth 2.0 flows.



57
58
59
# File 'lib/avalara_sdk/configuration.rb', line 57

def device_authorization_url
  @device_authorization_url
end

#environmentObject

Defines environment



19
20
21
# File 'lib/avalara_sdk/configuration.rb', line 19

def environment
  @environment
end

#force_ending_formatObject

Returns the value of attribute force_ending_format.



142
143
144
# File 'lib/avalara_sdk/configuration.rb', line 142

def force_ending_format
  @force_ending_format
end

#inject_formatObject

Returns the value of attribute inject_format.



140
141
142
# File 'lib/avalara_sdk/configuration.rb', line 140

def inject_format
  @inject_format
end

#logger#debug

Defines the logger used for debugging. Default to ‘Rails.logger` (when in Rails) or logging to STDOUT.

Returns:

  • (#debug)


85
86
87
# File 'lib/avalara_sdk/configuration.rb', line 85

def logger
  @logger
end

#machine_nameString

Defines the machine name

Returns:

  • (String)


157
158
159
# File 'lib/avalara_sdk/configuration.rb', line 157

def machine_name
  @machine_name
end

#params_encodingObject

Set this to customize parameters encoding of array parameter with multi collectionFormat. Default to nil.

github.com/typhoeus/ethon/blob/master/lib/ethon/easy/queryable.rb#L96

See Also:

  • params_encoding option of Ethon. Related source code:


138
139
140
# File 'lib/avalara_sdk/configuration.rb', line 138

def params_encoding
  @params_encoding
end

#passwordString

Defines the password used with HTTP basic authentication.

Returns:

  • (String)


51
52
53
# File 'lib/avalara_sdk/configuration.rb', line 51

def password
  @password
end

#ssl_ca_fileString

TLS/SSL setting Set this to customize the certificate file to verify the peer.

Returns:

  • (String)

    the path to the certificate file



123
124
125
# File 'lib/avalara_sdk/configuration.rb', line 123

def ssl_ca_file
  @ssl_ca_file
end

#ssl_client_certObject

TLS/SSL setting Client certificate file (for client certificate)



127
128
129
# File 'lib/avalara_sdk/configuration.rb', line 127

def ssl_client_cert
  @ssl_client_cert
end

#ssl_client_keyObject

TLS/SSL setting Client private key file (for client certificate)



131
132
133
# File 'lib/avalara_sdk/configuration.rb', line 131

def ssl_client_key
  @ssl_client_key
end

#ssl_verifytrue, false

Note:

Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.

TLS/SSL setting Set this to false to skip verifying SSL certificate when calling API from https server. Default to true.

Returns:

  • (true, false)


110
111
112
# File 'lib/avalara_sdk/configuration.rb', line 110

def ssl_verify
  @ssl_verify
end

#ssl_verify_modeObject

Note:

Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.

TLS/SSL setting Any ‘OpenSSL::SSL::` constant (see ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL.html)



117
118
119
# File 'lib/avalara_sdk/configuration.rb', line 117

def ssl_verify_mode
  @ssl_verify_mode
end

#temp_folder_pathString

Defines the temporary folder to store downloaded files (for API endpoints that have file response). Default to use ‘Tempfile`.

Returns:

  • (String)


92
93
94
# File 'lib/avalara_sdk/configuration.rb', line 92

def temp_folder_path
  @temp_folder_path
end

#test_base_pathObject

Defines Test base Path



22
23
24
# File 'lib/avalara_sdk/configuration.rb', line 22

def test_base_path
  @test_base_path
end

#test_device_authorization_urlObject

Defines override device Authorization URL for OAuth 2.0 flows when using the test environment.



63
64
65
# File 'lib/avalara_sdk/configuration.rb', line 63

def test_device_authorization_url
  @test_device_authorization_url
end

#test_token_urlObject

Defines override token URL for OAuth 2.0 flows when using the test environment.



60
61
62
# File 'lib/avalara_sdk/configuration.rb', line 60

def test_token_url
  @test_token_url
end

#timeoutObject

The time limit for HTTP request in seconds. Default to 0 (never times out).



96
97
98
# File 'lib/avalara_sdk/configuration.rb', line 96

def timeout
  @timeout
end

#token_urlObject

Defines Token URL for OAuth 2.0 flows.



54
55
56
# File 'lib/avalara_sdk/configuration.rb', line 54

def token_url
  @token_url
end

#usernameString

Defines the username used with HTTP basic authentication.

Returns:

  • (String)


46
47
48
# File 'lib/avalara_sdk/configuration.rb', line 46

def username
  @username
end

Class Method Details

.defaultObject

The default Configuration object.



196
197
198
# File 'lib/avalara_sdk/configuration.rb', line 196

def self.default
  @@default ||= Configuration.new
end

Instance Method Details

#api_key_with_prefix(param_name, param_alias = nil) ⇒ Object

Gets API key (with prefix if set).

Parameters:

  • param_name (String)

    the parameter name of API key auth



230
231
232
233
234
235
236
237
238
# File 'lib/avalara_sdk/configuration.rb', line 230

def api_key_with_prefix(param_name, param_alias = nil)
  key = @api_key[param_name]
  key = @api_key.fetch(param_alias, key) unless param_alias.nil?
  if @api_key_prefix[param_name]
    "#{@api_key_prefix[param_name]} #{key}"
  else
    key
  end
end

#auth_settingsObject

Returns Auth Settings hash for api client.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/avalara_sdk/configuration.rb', line 246

def auth_settings
  {
    'BasicAuth' =>
      {
        type: 'basic',
        in: 'header',
        key: 'Authorization',
        value: basic_auth_token
      },
    'Bearer' =>
      {
        type: 'api_key',
        in: 'header',
        key: 'Authorization',
        value: api_key_with_prefix('Bearer')
      },
  }
end

#base_urlObject

Returns base URL for specified operation based on server settings



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/avalara_sdk/configuration.rb', line 210

def base_url
  case environment.downcase  
  when 'sandbox'
    return 'https://api.sbx.avalara.com'
  when 'production'
    return 'https://api.avalara.com'
  when 'qa'
    return 'https://superapi.qa.avalara.io'
  when 'test'
    if test_base_path.empty?
      fail ArgumentError, "Test_Base_Path must be configured to run in test environment mode."
    end
    return test_base_path
  else
    fail ArgumentError, "Invalid environment value"
  end
end

#basic_auth_tokenObject

Gets Basic Auth token string



241
242
243
# File 'lib/avalara_sdk/configuration.rb', line 241

def basic_auth_token
  'Basic ' + ["#{username}:#{password}"].pack('m').delete("\r\n")
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



200
201
202
# File 'lib/avalara_sdk/configuration.rb', line 200

def configure
  yield(self) if block_given?
end

#configure_middleware(connection) ⇒ Object

Set up middleware on the connection



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/avalara_sdk/configuration.rb', line 281

def configure_middleware(connection)
  @middlewares.each do |middleware|
    connection.use(*middleware)
  end

  @request_middlewares.each do |middleware|
    connection.request(*middleware)
  end

  @response_middlewares.each do |middleware|
    connection.response(*middleware)
  end
end

#get_token_url(openid_connect_url) ⇒ Object



323
324
325
326
327
328
# File 'lib/avalara_sdk/configuration.rb', line 323

def get_token_url(openid_connect_url)
  response = Faraday.get(openid_connect_url) do |req|
    req.headers['Accept'] = 'application/json'
  end
  JSON.parse(response.body)
end

#openid_connect_urlObject



312
313
314
315
316
317
318
319
320
321
# File 'lib/avalara_sdk/configuration.rb', line 312

def openid_connect_url
  case @environment.downcase
  when 'sandbox'
    SANDBOX_OPENID_CONFIG_URL
  when 'production'
    PRODUCTION_OPENID_CONFIG_URL
  when 'qa'
    QA_OPENID_CONFIG_URL
  end
end

#populate_token_urlObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/avalara_sdk/configuration.rb', line 295

def populate_token_url
  if @environment.downcase == 'test'
    @token_url = @test_token_url
    @device_authorization_url = @test_device_authorization_url
  elsif @token_url.nil? || @token_url.length == 0
    begin
      token_response = get_token_url openid_connect_url
      @token_url = token_response['token_endpoint']
      @device_authorization_url = token_response['device_authorization_endpoint']
    rescue Exception => e
      puts "Exception when calling OpenIdConnect to fetch the token endpoint. Error: #{e.message}"
      @token_url = FALLBACK_TOKEN_URL
      @device_authorization_url = FALLBACK_DEVICE_AUTHORIZATION_URL
    end
  end
end

#request(*middleware) ⇒ Object

Adds request middleware to the stack



271
272
273
# File 'lib/avalara_sdk/configuration.rb', line 271

def request(*middleware)
  @request_middlewares << middleware
end

#response(*middleware) ⇒ Object

Adds response middleware to the stack



276
277
278
# File 'lib/avalara_sdk/configuration.rb', line 276

def response(*middleware)
  @response_middlewares << middleware
end

#use(*middleware) ⇒ Object

Adds middleware to the stack



266
267
268
# File 'lib/avalara_sdk/configuration.rb', line 266

def use(*middleware)
  @middlewares << middleware
end