Class: Stripe::APIRequestor

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Defined in:
lib/stripe/api_requestor.rb

Overview

APIRequestor executes requests against the Stripe API and allows a user to recover both a resource a call returns as well as a response object that contains information on the HTTP call.

Defined Under Namespace

Classes: RequestLogContext, StripeRequestMetrics, SystemProfiler, ThreadContext

Constant Summary collapse

CONNECTION_MANAGER_GC_LAST_USED_EXPIRY =

Time (in seconds) that a connection manager has not been used before it's eligible for garbage collection.

120
CONNECTION_MANAGER_GC_PERIOD =

How often to check (in seconds) for connection managers that haven't been used in a long time and which should be garbage collected.

60
ERROR_MESSAGE_CONNECTION =
"Unexpected error communicating when trying to connect to " \
"Stripe (%s). You may be seeing this message because your DNS is not " \
"working or you don't have an internet connection.  To check, try " \
"running `host stripe.com` from the command line."
ERROR_MESSAGE_SSL =
"Could not establish a secure connection to Stripe (%s), you " \
"may need to upgrade your OpenSSL version. To check, try running " \
"`openssl s_client -connect api.stripe.com:443` from the command " \
"line."
ERROR_MESSAGE_TIMEOUT_SUFFIX =

Common error suffix sared by both connect and read timeout messages.

"Please check your internet connection and try again. " \
"If this problem persists, you should check Stripe's service " \
"status at https://status.stripe.com, or let us know at " \
"[email protected]."
ERROR_MESSAGE_TIMEOUT_CONNECT =
(
  "Timed out connecting to Stripe (%s). " +
  ERROR_MESSAGE_TIMEOUT_SUFFIX
).freeze
ERROR_MESSAGE_TIMEOUT_READ =
(
  "Timed out communicating with Stripe (%s). " +
  ERROR_MESSAGE_TIMEOUT_SUFFIX
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_arg = {}) ⇒ APIRequestor

Initializes a new APIRequestor



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/stripe/api_requestor.rb', line 18

def initialize(config_arg = {})
  @last_request_metrics = Queue.new

  @config = case config_arg
            when Hash
              StripeConfiguration.new.reverse_duplicate_merge(config_arg)
            when Stripe::StripeConfiguration
              config_arg
            when String
              StripeConfiguration.new.reverse_duplicate_merge(
                { api_key: config_arg }
              )
            else
              raise ArgumentError, "Can't handle argument: #{config_arg}"
            end
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



35
36
37
# File 'lib/stripe/api_requestor.rb', line 35

def config
  @config
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/stripe/api_requestor.rb', line 35

def options
  @options
end

Class Method Details

.active_requestor ⇒ Object

Gets a currently active APIRequestor. Set for the current thread when APIRequestor#request is being run so that API operations being executed inside of that block can find the currently active requestor. It's reset to the original value (hopefully nil) after the block ends.

For internal use only. Does not provide a stable API and may be broken with future non-major changes.



68
69
70
# File 'lib/stripe/api_requestor.rb', line 68

def self.active_requestor
  current_thread_context.active_requestor || default_requestor
end

.clear_all_connection_managers(config: nil) ⇒ Object

Finishes any active connections by closing their TCP connection and clears them from internal tracking in all connection managers across all threads.

If passed a config object, only clear connection managers for that particular configuration.

For internal use only. Does not provide a stable API and may be broken with future non-major changes.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stripe/api_requestor.rb', line 81

def self.clear_all_connection_managers(config: nil)
  # Just a quick path for when configuration is being set for the first
  # time before any connections have been opened. There is technically some
  # potential for thread raciness here, but not in a practical sense.
  return if @thread_contexts_with_connection_managers.empty?

  @thread_contexts_with_connection_managers_mutex.synchronize do
    pruned_contexts = Set.new

    @thread_contexts_with_connection_managers.each do |thread_context|
      # Note that the thread context itself is not destroyed, but we clear
      # its connection manager and remove our reference to it. If it ever
      # makes a new request we'll give it a new connection manager and
      # it'll go back into `@thread_contexts_with_connection_managers`.
      thread_context.default_connection_managers.reject! do |cm_config, cm|
        if config.nil? || config.key == cm_config
          cm.clear
          true
        end
      end

      pruned_contexts << thread_context if thread_context.default_connection_managers.empty?
    end

    @thread_contexts_with_connection_managers.subtract(pruned_contexts)
  end
end

.current_thread_context ⇒ Object

Access data stored for APIRequestor within the thread's current context. Returns ThreadContext.

For internal use only. Does not provide a stable API and may be broken with future non-major changes.



444
445
446
# File 'lib/stripe/api_requestor.rb', line 444

def self.current_thread_context
  Thread.current[:api_requestor__internal_use_only] ||= ThreadContext.new
end

.default_connection_manager(config = Stripe.config) ⇒ Object

A default connection manager for the current thread scoped to the configuration object that may be provided.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stripe/api_requestor.rb', line 116

def self.default_connection_manager(config = Stripe.config)
  current_thread_context.default_connection_managers[config.key] ||= begin
    connection_manager = ConnectionManager.new(config)

    @thread_contexts_with_connection_managers_mutex.synchronize do
      maybe_gc_connection_managers
      @thread_contexts_with_connection_managers << current_thread_context
    end

    connection_manager
  end
end

.default_requestor ⇒ Object

A default requestor for the current thread.



110
111
112
# File 'lib/stripe/api_requestor.rb', line 110

def self.default_requestor
  current_thread_context.default_requestor ||= APIRequestor.new(Stripe.config)
end

.maybe_gc_connection_managers ⇒ Object

Garbage collects connection managers that haven't been used in some time, with the idea being that we want to remove old connection managers that belong to dead threads and the like.

Prefixed with maybe_ because garbage collection will only run periodically so that we're not constantly engaged in busy work. If connection managers live a little passed their useful age it's not harmful, so it's not necessary to get them right away.

For testability, returns nil if it didn't run and the number of connection managers that were garbage collected otherwise.

IMPORTANT: This method is not thread-safe and expects to be called inside a lock on @thread_contexts_with_connection_managers_mutex.

For internal use only. Does not provide a stable API and may be broken with future non-major changes.



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/stripe/api_requestor.rb', line 465

def self.maybe_gc_connection_managers
  next_gc_time = @last_connection_manager_gc + CONNECTION_MANAGER_GC_PERIOD
  return nil if next_gc_time > Util.monotonic_time

  last_used_threshold =
    Util.monotonic_time - CONNECTION_MANAGER_GC_LAST_USED_EXPIRY

  pruned_contexts = []
  @thread_contexts_with_connection_managers.each do |thread_context|
    thread_context
      .default_connection_managers
      .each do |config_key, connection_manager|
        next if connection_manager.last_used > last_used_threshold

        connection_manager.clear
        thread_context.default_connection_managers.delete(config_key)
      end
  end

  @thread_contexts_with_connection_managers.each do |thread_context|
    next unless thread_context.default_connection_managers.empty?

    pruned_contexts << thread_context
  end

  @thread_contexts_with_connection_managers -= pruned_contexts
  @last_connection_manager_gc = Util.monotonic_time

  pruned_contexts.count
end

.should_retry?(error, num_retries:, config: Stripe.config) ⇒ Boolean

Checks if an error is a problem that we should retry on. This includes both socket errors that may represent an intermittent problem and some special HTTP statuses.

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/stripe/api_requestor.rb', line 132

def self.should_retry?(error,
                       num_retries:, config: Stripe.config)
  return false if num_retries >= config.max_network_retries

  case error
  when Net::OpenTimeout, Net::ReadTimeout, Net::HTTPFatalError
    # Retry on timeout-related problems (either on open or read).
    true
  when EOFError, Errno::ECONNREFUSED, Errno::ECONNRESET, # rubocop:todo Lint/DuplicateBranch
        Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketError
    # Destination refused the connection, the connection was reset, or a
    # variety of other connection failures. This could occur from a single
    # saturated server, so retry in case it's intermittent.
    true
  when Stripe::StripeError
    # The API may ask us not to retry (e.g. if doing so would be a no-op),
    # or advise us to retry (e.g. in cases of lock timeouts). Defer to
    # those instructions if given.
    return false if error.http_headers["stripe-should-retry"] == "false"
    return true if error.http_headers["stripe-should-retry"] == "true"

    # 409 Conflict
    return true if error.http_status == 409

    # 429 Too Many Requests
    #
    # There are a few different problems that can lead to a 429. The most
    # common is rate limiting, on which we *don't* want to retry because
    # that'd likely contribute to more contention problems. However, some
    # 429s are lock timeouts, which is when a request conflicted with
    # another request or an internal process on some particular object.
    # These 429s are safe to retry.
    return true if error.http_status == 429 && error.code == "lock_timeout"

    # Retry on 500, 503, and other internal errors.
    #
    # Note that we expect the stripe-should-retry header to be false
    # in most cases when a 500 is returned, since our idempotency framework
    # would typically replay it anyway.
    true if error.http_status >= 500
  else
    false
  end
end

.sleep_time(num_retries, config: Stripe.config) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/stripe/api_requestor.rb', line 177

def self.sleep_time(num_retries, config: Stripe.config)
  # Apply exponential backoff with initial_network_retry_delay on the
  # number of num_retries so far as inputs. Do not allow the number to
  # exceed max_network_retry_delay.
  sleep_seconds = [
    config.initial_network_retry_delay * (2**(num_retries - 1)),
    config.max_network_retry_delay,
  ].min

  # Apply some jitter by randomizing the value in the range of
  # (sleep_seconds / 2) to (sleep_seconds).
  sleep_seconds *= (0.5 * (1 + rand))

  # But never sleep less than the base sleep seconds.
  [config.initial_network_retry_delay, sleep_seconds].max
end

Instance Method Details

#encode_with(coder) ⇒ Object

Implements custom encoding for YAML (Psych). Without it, Psych dumps every instance variable, including the API key and any proxy credentials in the configuration. Clients reach the configuration through their requestor, so this also keeps it out of their YAML dumps.



55
56
57
58
59
# File 'lib/stripe/api_requestor.rb', line 55

def encode_with(coder)
  (instance_variables - [:@config]).each do |ivar|
    coder[ivar.to_s.delete_prefix("@")] = instance_variable_get(ivar)
  end
end

#execute_request(method, path, base_address, params: {}, opts: {}, usage: []) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/stripe/api_requestor.rb', line 221

def execute_request(method, path, base_address,
                    params: {}, opts: {}, usage: [])
  params = params.to_h if params.is_a?(RequestParams)
  http_resp, req_opts = execute_request_internal(
    method, path, base_address, params, opts, usage
  )
  req_opts = RequestOptions.extract_opts_from_hash(req_opts)

  notice = http_resp["stripe-notice"]
  maybe_emit_stripe_notice(notice)

  resp = interpret_response(http_resp)

  api_mode = Util.get_api_mode(path)
  Util.convert_to_stripe_object_with_params(resp.data, params, RequestOptions.persistable(req_opts), resp,
                                            api_mode: api_mode, requestor: self,
                                            v2_deleted_object: method == :delete && api_mode == :v2)
end

#execute_request_initialize_from(method, path, base_address, object, params: {}, opts: {}, usage: []) ⇒ Object

Execute request without instantiating a new object if the relevant object's name matches the class

For internal use only. Does not provide a stable API and may be broken with future non-major changes.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/stripe/api_requestor.rb', line 258

def execute_request_initialize_from(method, path, base_address, object,
                                    params: {}, opts: {}, usage: [])
  opts = RequestOptions.combine_opts(object.instance_variable_get(:@opts) || {}, opts)
  opts = Util.normalize_opts(opts)

  params = params.to_h if params.is_a?(RequestParams)

  http_resp, req_opts = execute_request_internal(
    method, path, base_address, params, opts, usage
  )
  req_opts = RequestOptions.extract_opts_from_hash(req_opts)

  resp = interpret_response(http_resp)

  if Util.object_name_matches_class?(resp.data[:object], object.class)
    object.send(:initialize_from,
                resp.data, RequestOptions.persistable(req_opts), resp,
                api_mode: :v1, requestor: self)
  else
    Util.convert_to_stripe_object_with_params(resp.data, params,
                                              RequestOptions.persistable(req_opts),
                                              resp, api_mode: :v1, requestor: self)
  end
end

#execute_request_stream(method, path, base_address, params: {}, opts: {}, usage: [], &read_body_chunk_block) ⇒ Object

Executes a request and returns the body as a stream instead of converting it to a StripeObject. This should be used for any request where we expect an arbitrary binary response.

A read_body_chunk block can be passed, which will be called repeatedly with the body chunks read from the socket.

If a block is passed, a StripeHeadersOnlyResponse is returned as the block is expected to do all the necessary body processing. If no block is passed, then a StripeStreamResponse is returned containing an IO stream with the response body.



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/stripe/api_requestor.rb', line 302

def execute_request_stream(method, path,
                           base_address,
                           params: {}, opts: {}, usage: [],
                           &read_body_chunk_block)
  unless block_given?
    raise ArgumentError,
          "execute_request_stream requires a read_body_chunk_block"
  end

  params = params.to_h if params.is_a?(RequestParams)
  http_resp, api_key = execute_request_internal(
    method, path, base_address, params, opts, usage, &read_body_chunk_block
  )

  # When the read_body_chunk_block is given, we no longer have access to the
  # response body at this point and so return a response object containing
  # only the headers. This is because the body was consumed by the block.
  resp = StripeHeadersOnlyResponse.from_net_http(http_resp)
  store_last_response(resp)

  [resp, api_key]
end

#inspect ⇒ Object

The default inspect renders every instance variable, which would include the API key held by the configuration.



47
48
49
# File 'lib/stripe/api_requestor.rb', line 47

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)}>"
end

#interpret_response(http_resp) ⇒ Object



283
284
285
286
287
288
289
# File 'lib/stripe/api_requestor.rb', line 283

def interpret_response(http_resp)
  resp = StripeResponse.from_net_http(http_resp)
  store_last_response(resp)
  resp
rescue JSON::ParserError
  raise general_api_error(http_resp.code.to_i, http_resp.body)
end

#request ⇒ Object

Executes the API call within the given block. Usage looks like:

client = APIRequestor.new
charge, resp = client.request { Charge.create }


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/stripe/api_requestor.rb', line 199

def request
  context = self.class.current_thread_context
  old_api_requestor = context.active_requestor
  block_id = Object.new
  context.request_block_stack ||= []
  context.last_responses ||= {}
  context.request_block_stack.push(block_id)
  context.last_responses[block_id] = nil
  context.active_requestor = self

  begin
    res = yield
    [res, context.last_responses[block_id]]
  ensure
    context.active_requestor = old_api_requestor
    context.last_responses.delete(block_id)
    context.request_block_stack.pop
  end
end

#store_last_response(resp) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/stripe/api_requestor.rb', line 325

def store_last_response(resp)
  context = self.class.current_thread_context
  # Responses belong to the innermost request block, regardless of which requestor sent the request.
  block_id = context.request_block_stack&.last
  return unless block_id

  context.last_responses[block_id] = resp
end

#with_authorization_scope(scope) ⇒ Object

Snapshot authorization defaults while retaining the transport's telemetry.



38
39
40
41
42
43
# File 'lib/stripe/api_requestor.rb', line 38

def with_authorization_scope(scope)
  scope = RequestOptions.snapshot_authorization_selectors(scope)
  dup.tap do |requestor|
    requestor.instance_variable_set(:@config, config.reverse_duplicate_merge(scope))
  end
end