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'
EINVOICING_SERVICE_PRODUCTION_URL =

Official URL of EInvoicing Service (Production by Environment)

'https://api.avalara.com'
EINVOICING_SERVICE_SANDBOX_URL =
'https://api.sbx.avalara.com'
EINVOICING_SERVICE_QA_URL =
'https://superapi.qa.avalara.io'
EINVOICING_SERVICE_DEV_URL =
'https://superapi.dev.avalara.io'
A1099_SERVICE_PRODUCTION_URL =

Official URL of A1099 Service (Production by Environment)

'https://api.avalara.com/avalara1099'
A1099_SERVICE_SANDBOX_URL =
'https://api.sbx.avalara.com/avalara1099'
A1099_SERVICE_QA_URL =
'https://api-ava1099.gamma.qa.us-west-2.aws.avalara.io'
A1099_SERVICE_DEV_URL =
'https://api-ava1099.gamma.dev.us-west-2.aws.avalara.io'

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:



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/avalara_sdk/configuration.rb', line 171

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)



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

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



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

def api_key_prefix
  @api_key_prefix
end

#app_nameString

Defines the application name

Returns:

  • (String)


159
160
161
# File 'lib/avalara_sdk/configuration.rb', line 159

def app_name
  @app_name
end

#app_versionString

Defines the application version

Returns:

  • (String)


164
165
166
# File 'lib/avalara_sdk/configuration.rb', line 164

def app_version
  @app_version
end

#base_pathObject

Defines url base path



37
38
39
# File 'lib/avalara_sdk/configuration.rb', line 37

def base_path
  @base_path
end

#bearer_tokenObject

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



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

def bearer_token
  @bearer_token
end

#client_idObject

Defines the ClientId used for the OAuth2 Client Credentials flow.



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

def client_id
  @client_id
end

#client_secretObject

The ClientSecret used for the OAuth2 Client Credentials flow.



81
82
83
# File 'lib/avalara_sdk/configuration.rb', line 81

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)


113
114
115
# File 'lib/avalara_sdk/configuration.rb', line 113

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)


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

def debugging
  @debugging
end

#device_authorization_urlObject

Defines Device Authorization URL for OAuth 2.0 flows.



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

def device_authorization_url
  @device_authorization_url
end

#environmentObject

Defines environment



31
32
33
# File 'lib/avalara_sdk/configuration.rb', line 31

def environment
  @environment
end

#force_ending_formatObject

Returns the value of attribute force_ending_format.



154
155
156
# File 'lib/avalara_sdk/configuration.rb', line 154

def force_ending_format
  @force_ending_format
end

#inject_formatObject

Returns the value of attribute inject_format.



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

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)


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

def logger
  @logger
end

#machine_nameString

Defines the machine name

Returns:

  • (String)


169
170
171
# File 'lib/avalara_sdk/configuration.rb', line 169

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:


150
151
152
# File 'lib/avalara_sdk/configuration.rb', line 150

def params_encoding
  @params_encoding
end

#passwordString

Defines the password used with HTTP basic authentication.

Returns:

  • (String)


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

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



135
136
137
# File 'lib/avalara_sdk/configuration.rb', line 135

def ssl_ca_file
  @ssl_ca_file
end

#ssl_client_certObject

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



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

def ssl_client_cert
  @ssl_client_cert
end

#ssl_client_keyObject

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



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

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)


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

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)



129
130
131
# File 'lib/avalara_sdk/configuration.rb', line 129

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)


104
105
106
# File 'lib/avalara_sdk/configuration.rb', line 104

def temp_folder_path
  @temp_folder_path
end

#test_base_pathObject

Defines Test base Path



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

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.



75
76
77
# File 'lib/avalara_sdk/configuration.rb', line 75

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.



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

def test_token_url
  @test_token_url
end

#timeoutObject

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



108
109
110
# File 'lib/avalara_sdk/configuration.rb', line 108

def timeout
  @timeout
end

#token_urlObject

Defines Token URL for OAuth 2.0 flows.



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

def token_url
  @token_url
end

#usernameString

Defines the username used with HTTP basic authentication.

Returns:

  • (String)


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

def username
  @username
end

Class Method Details

.defaultObject

The default Configuration object.



208
209
210
# File 'lib/avalara_sdk/configuration.rb', line 208

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



268
269
270
271
272
273
274
275
276
# File 'lib/avalara_sdk/configuration.rb', line 268

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.



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/avalara_sdk/configuration.rb', line 284

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

#basic_auth_tokenObject

Gets Basic Auth token string



279
280
281
# File 'lib/avalara_sdk/configuration.rb', line 279

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

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

Yields:

  • (_self)

Yield Parameters:



212
213
214
# File 'lib/avalara_sdk/configuration.rb', line 212

def configure
  yield(self) if block_given?
end

#configure_middleware(connection) ⇒ Object

Set up middleware on the connection



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/avalara_sdk/configuration.rb', line 319

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_base_path(microservice = :none) ⇒ Object

Returns base URL for specified operation based on server settings and microservice



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/avalara_sdk/configuration.rb', line 222

def get_base_path(microservice = :none)
  puts "get_base_path called with microservice = #{microservice.inspect}"

  if environment.downcase == 'test' && test_base_path.empty?
    fail ArgumentError, "Test_Base_Path must be configured to run in test environment mode."
  end

  case microservice
  when :EInvoicing
    case environment.downcase
    when 'production'
      return EINVOICING_SERVICE_PRODUCTION_URL
    when 'sandbox'
      return EINVOICING_SERVICE_SANDBOX_URL
    when 'qa'
      return EINVOICING_SERVICE_QA_URL
    when 'dev'
      return EINVOICING_SERVICE_DEV_URL
    when 'test'
      return test_base_path
    else
      fail ArgumentError, 'Environment not configured correctly, Acceptable values are "production", "sandbox", "qa", "dev", and "test".'
    end
  when :A1099
    case environment.downcase
    when 'production'
      return A1099_SERVICE_PRODUCTION_URL
    when 'sandbox'
      return A1099_SERVICE_SANDBOX_URL
    when 'qa'
      return A1099_SERVICE_QA_URL
    when 'dev'
      return A1099_SERVICE_DEV_URL
    when 'test'
      return test_base_path
    else
      fail ArgumentError, 'Environment not configured correctly, Acceptable values are "production", "sandbox", "qa", "dev", and "test".'
    end
  when :none
    fail ArgumentError, 'Microservice not configured correctly, Acceptable values are "EInvoicing", "A1099", and "none".'
  end
  
end

#get_token_url(openid_connect_url) ⇒ Object



361
362
363
364
365
366
# File 'lib/avalara_sdk/configuration.rb', line 361

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



350
351
352
353
354
355
356
357
358
359
# File 'lib/avalara_sdk/configuration.rb', line 350

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



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/avalara_sdk/configuration.rb', line 333

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



309
310
311
# File 'lib/avalara_sdk/configuration.rb', line 309

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

#response(*middleware) ⇒ Object

Adds response middleware to the stack



314
315
316
# File 'lib/avalara_sdk/configuration.rb', line 314

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

#use(*middleware) ⇒ Object

Adds middleware to the stack



304
305
306
# File 'lib/avalara_sdk/configuration.rb', line 304

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