Class: NbApiClient::Configuration
- Inherits:
-
Object
- Object
- NbApiClient::Configuration
- Defined in:
- lib/nb_api_client/configuration.rb
Instance Attribute Summary collapse
-
#cache_store ⇒ Object
Cache store used to count repeated "unauthorized" responses for a nation.
-
#logger ⇒ Object
Logger for rate-limit and retry warnings.
-
#max_rate_limit_retries ⇒ Object
Returns the value of attribute max_rate_limit_retries.
-
#max_token_retries ⇒ Object
Returns the value of attribute max_token_retries.
-
#on_repeated_unauthorized ⇒ Object
Called with the nation whenever it has been deauthorized after too many consecutive "unauthorized" API responses.
-
#rate_limit ⇒ Object
Sliding-window rate limit: at most
rate_limitrequests perrate_limit_window_seconds, enforced across all processes viacache_store(above). -
#rate_limit_max_jitter ⇒ Object
Returns the value of attribute rate_limit_max_jitter.
-
#rate_limit_max_wait_seconds ⇒ Object
Returns the value of attribute rate_limit_max_wait_seconds.
-
#rate_limit_poll_interval ⇒ Object
Returns the value of attribute rate_limit_poll_interval.
-
#rate_limit_window_seconds ⇒ Object
Returns the value of attribute rate_limit_window_seconds.
-
#short_circuit_in_test ⇒ Object
If true (the default), NbApiClient::Request#call returns {} immediately in Rails.env.test? instead of making a real HTTP request.
-
#unauthorized_error_threshold ⇒ Object
How many "unauthorized" responses within unauthorized_error_window before a nation is deauthorized.
-
#unauthorized_error_window ⇒ Object
Rolling window, in seconds, over which unauthorized_error_threshold is counted.
Instance Method Summary collapse
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nb_api_client/configuration.rb', line 42 def initialize @cache_store = (defined?(Rails) && Rails.respond_to?(:cache)) ? Rails.cache : nil @logger = (defined?(Rails) && Rails.respond_to?(:logger)) ? Rails.logger : nil @on_repeated_unauthorized = nil @unauthorized_error_threshold = 25 @unauthorized_error_window = 5 * 60 @short_circuit_in_test = true @max_rate_limit_retries = 3 @max_token_retries = 2 @rate_limit = ENV.fetch("API_RATE_LIMIT", 200).to_i @rate_limit_window_seconds = ENV.fetch("API_RATE_WINDOW", 10).to_i @rate_limit_max_wait_seconds = ENV.fetch("API_RATE_MAX_WAIT_SECONDS", 120).to_i @rate_limit_poll_interval = 0.25 @rate_limit_max_jitter = 0.25 end |
Instance Attribute Details
#cache_store ⇒ Object
Cache store used to count repeated "unauthorized" responses for a nation. Must respond to #increment(key, amount, options) and #exist?/#write (if you also use the cache in on_repeated_unauthorized). Defaults to Rails.cache.
8 9 10 |
# File 'lib/nb_api_client/configuration.rb', line 8 def cache_store @cache_store end |
#logger ⇒ Object
Logger for rate-limit and retry warnings. Defaults to Rails.logger.
11 12 13 |
# File 'lib/nb_api_client/configuration.rb', line 11 def logger @logger end |
#max_rate_limit_retries ⇒ Object
Returns the value of attribute max_rate_limit_retries.
30 31 32 |
# File 'lib/nb_api_client/configuration.rb', line 30 def max_rate_limit_retries @max_rate_limit_retries end |
#max_token_retries ⇒ Object
Returns the value of attribute max_token_retries.
31 32 33 |
# File 'lib/nb_api_client/configuration.rb', line 31 def max_token_retries @max_token_retries end |
#on_repeated_unauthorized ⇒ Object
Called with the nation whenever it has been deauthorized after too many consecutive "unauthorized" API responses. Use this to schedule re-authorization (e.g. a background job) or notify the account owner. Optional.
16 17 18 |
# File 'lib/nb_api_client/configuration.rb', line 16 def @on_repeated_unauthorized end |
#rate_limit ⇒ Object
Sliding-window rate limit: at most rate_limit requests per rate_limit_window_seconds,
enforced across all processes via cache_store (above). If cache_store is nil,
rate limiting is disabled entirely.
36 37 38 |
# File 'lib/nb_api_client/configuration.rb', line 36 def rate_limit @rate_limit end |
#rate_limit_max_jitter ⇒ Object
Returns the value of attribute rate_limit_max_jitter.
40 41 42 |
# File 'lib/nb_api_client/configuration.rb', line 40 def rate_limit_max_jitter @rate_limit_max_jitter end |
#rate_limit_max_wait_seconds ⇒ Object
Returns the value of attribute rate_limit_max_wait_seconds.
38 39 40 |
# File 'lib/nb_api_client/configuration.rb', line 38 def rate_limit_max_wait_seconds @rate_limit_max_wait_seconds end |
#rate_limit_poll_interval ⇒ Object
Returns the value of attribute rate_limit_poll_interval.
39 40 41 |
# File 'lib/nb_api_client/configuration.rb', line 39 def rate_limit_poll_interval @rate_limit_poll_interval end |
#rate_limit_window_seconds ⇒ Object
Returns the value of attribute rate_limit_window_seconds.
37 38 39 |
# File 'lib/nb_api_client/configuration.rb', line 37 def rate_limit_window_seconds @rate_limit_window_seconds end |
#short_circuit_in_test ⇒ Object
If true (the default), NbApiClient::Request#call returns {} immediately in Rails.env.test? instead of making a real HTTP request. This only applies when Request.call is invoked directly (not when it has been stubbed out in tests).
28 29 30 |
# File 'lib/nb_api_client/configuration.rb', line 28 def short_circuit_in_test @short_circuit_in_test end |
#unauthorized_error_threshold ⇒ Object
How many "unauthorized" responses within unauthorized_error_window before a nation is deauthorized.
20 21 22 |
# File 'lib/nb_api_client/configuration.rb', line 20 def @unauthorized_error_threshold end |
#unauthorized_error_window ⇒ Object
Rolling window, in seconds, over which unauthorized_error_threshold is counted.
23 24 25 |
# File 'lib/nb_api_client/configuration.rb', line 23 def @unauthorized_error_window end |