Class: Plaid::Internal::Http::RawClient Private

Inherits:
Object
  • Object
show all
Defined in:
lib/plaid/internal/http/raw_client.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Modules: DecodeContent

Constant Summary collapse

RETRYABLE_STATUSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default HTTP status codes that trigger a retry

[408, 429, 502, 503, 504].freeze
INITIAL_RETRY_DELAY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Initial delay between retries in seconds

0.5
MAX_RETRY_DELAY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maximum delay between retries in seconds

60.0
JITTER_FACTOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Jitter factor for randomizing retry delays (20%)

0.2
NETWORK_ERRNOS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Socket-level Errno failures. Other SystemCallErrors, such as file errors raised by a custom HTTP client, are not connection failures and propagate unchanged.

[
  Errno::ECONNREFUSED, Errno::ECONNRESET, Errno::ECONNABORTED, Errno::EPIPE,
  Errno::EHOSTUNREACH, Errno::ENETUNREACH, Errno::ENETDOWN, Errno::EHOSTDOWN,
  Errno::EADDRNOTAVAIL
].freeze
LOCALHOST_HOSTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%w[localhost 127.0.0.1 [::1]].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, max_retries: 0, timeout: 60.0, headers: {}, overridable_headers: [], auth_provider: nil, http_client: nil) ⇒ RawClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RawClient.

Parameters:

  • base_url (String) —

    The base url for the request.

  • max_retries (Integer) (defaults to: 0) —

    The number of times to retry a failed request, defaults to 0.

  • timeout (Float) (defaults to: 60.0) —

    The timeout for the request, defaults to 60.0 seconds.

  • headers (Hash) (defaults to: {}) —

    The headers for the request.

  • overridable_headers (Array<String>) (defaults to: []) —

    The names of the client-level headers a request may replace via additional_headers. Holds the API's global headers; SDK metadata and auth headers are absent from it and so stay protected.

  • auth_provider (Object, nil) (defaults to: nil) —

    An optional auth provider responding to auth_headers. When present its headers are resolved on every request so token-based schemes (e.g. OAuth) can refresh an expired token mid-session.

  • http_client (#request, nil) (defaults to: nil) —

    An optional HTTP transport that replaces the built-in Net::HTTP connection. It must respond to request(url, http_request), where url is a URI and http_request a Net::HTTPGenericRequest, and return a Net::HTTPResponse. Use it to configure proxies, TLS, connection reuse, or to intercept requests and responses; retries still wrap each call. The transport owns its connection settings, including timeouts: timeout: only applies to the built-in Net::HTTP connection.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/plaid/internal/http/raw_client.rb', line 37

def initialize(base_url:, max_retries: 0, timeout: 60.0, headers: {}, overridable_headers: [], auth_provider: nil, http_client: nil)
  @base_url = base_url
  @max_retries = max_retries
  @timeout = timeout
  @auth_provider = auth_provider
  @http_client = http_client
  @default_headers = {
    "X-Fern-Language": "Ruby",
    "X-Fern-SDK-Name": "plaid",
    "X-Fern-SDK-Version": Plaid::VERSION
  }.merge(headers)
  @overridable_headers = overridable_headers.to_set { |name| name.to_s.downcase }
end

Instance Attribute Details

#base_url ⇒ String (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The base URL for requests.

Returns:

  • (String) —

    The base URL for requests



18
19
20
# File 'lib/plaid/internal/http/raw_client.rb', line 18

def base_url
  @base_url
end

Instance Method Details

#add_jitter(delay) ⇒ Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds random jitter to a delay value.

Parameters:

  • delay (Float) —

    The base delay in seconds.

Returns:

  • (Float) —

    The delay with jitter applied.



171
172
173
174
# File 'lib/plaid/internal/http/raw_client.rb', line 171

def add_jitter(delay)
  jitter = delay * JITTER_FACTOR * (rand - 0.5) * 2
  [delay + jitter, 0].max
end

#build_http_request(url:, method:, headers: {}, body: nil, auth_headers: {}) ⇒ HTTP::Request

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The HTTP request.

Parameters:

  • url (URI::Generic) —

    The url to the resource.

  • method (String) —

    The HTTP method to use.

  • headers (Hash) (defaults to: {}) —

    The headers for the request.

  • body (String, nil) (defaults to: nil) —

    The body for the request.

  • auth_headers (Hash) (defaults to: {}) —

    The auth headers resolved for this request. These take precedence over the static default headers but not over per-request headers, mirroring the previous baked-header precedence.

Returns:

  • (HTTP::Request) —

    The HTTP request.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/plaid/internal/http/raw_client.rb', line 234

def build_http_request(url:, method:, headers: {}, body: nil, auth_headers: {})
  request = Net::HTTPGenericRequest.new(
    method,
    !body.nil?,
    method != "HEAD",
    url
  )

  request_headers = @default_headers.merge(auth_headers).merge(headers)
  request_headers.each { |name, value| request[name] = value == false ? "false" : value }
  request.body = body if body

  # Net::HTTP disables its transparent gzip/deflate decoding as soon as an
  # Accept-Encoding header is set explicitly on the request. Re-enable it so
  # that compressed response bodies are still inflated.
  request.extend(DecodeContent) if request_headers.keys.any? { |name| name.to_s.casecmp("accept-encoding").zero? }

  request
end

#build_url(request) ⇒ URI::Generic

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The URL.

Parameters:

Returns:

  • (URI::Generic) —

    The URL.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/plaid/internal/http/raw_client.rb', line 180

def build_url(request)
  # A nil value means the parameter is omitted (e.g. the cursor of a first page), not sent as a bare key.
  encoded_query = request.encode_query&.compact

  # If the path is already an absolute URL, use it directly
  if request.path.start_with?("http://", "https://")
    url = request.path
    url = "#{url}?#{encode_query(encoded_query)}" if encoded_query&.any?
    parsed = URI.parse(url)
    validate_https!(parsed)
    return parsed
  end

  path = request.path.start_with?("/") ? request.path[1..] : request.path
  base = request.base_url || @base_url
  url = "#{base.chomp("/")}/#{path}"
  url = "#{url}?#{encode_query(encoded_query)}" if encoded_query&.any?
  parsed = URI.parse(url)
  validate_https!(parsed)
  parsed
end

#connect(url) ⇒ Net::HTTP

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The HTTP connection.

Parameters:

  • url (URI::Generic) —

    The url to connect to.

Returns:

  • (Net::HTTP) —

    The HTTP connection.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/plaid/internal/http/raw_client.rb', line 288

def connect(url)
  is_https = (url.scheme == "https")

  port = if url.port
           url.port
         elsif is_https
           Net::HTTP.https_default_port
         else
           Net::HTTP.http_default_port
         end

  http = Net::HTTP.new(url.host, port)
  http.use_ssl = is_https
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER if is_https
  # NOTE: We handle retries at the application level with HTTP status code awareness,
  # so we set max_retries to 0 to disable Net::HTTP's built-in network-level retries.
  http.max_retries = 0
  http
end

#encode_query(query) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The encoded query.

Parameters:

  • query (Hash) —

    The query for the request.

Returns:

  • (String, nil) —

    The encoded query.



265
266
267
# File 'lib/plaid/internal/http/raw_client.rb', line 265

def encode_query(query)
  query.to_h.empty? ? nil : URI.encode_www_form(query)
end

#inspect ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


309
310
311
# File 'lib/plaid/internal/http/raw_client.rb', line 309

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

#parse_retry_after(value) ⇒ Float?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses the Retry-After header value.

Parameters:

  • value (String) —

    The Retry-After header value (seconds or HTTP date).

Returns:

  • (Float, nil) —

    The delay in seconds, or nil if parsing fails.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/plaid/internal/http/raw_client.rb', line 153

def parse_retry_after(value)
  # Try parsing as integer (seconds)
  seconds = Integer(value, exception: false)
  return seconds.to_f if seconds

  # Try parsing as HTTP date
  begin
    retry_time = Time.httpdate(value)
    delay = retry_time - Time.now
    delay.positive? ? delay : nil
  rescue ArgumentError
    nil
  end
end

#perform_request(url, http_request, timeout) ⇒ Net::HTTPResponse

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sends a single attempt of the request through the custom http_client when one was supplied, or through a fresh Net::HTTP connection otherwise.

Parameters:

  • url (URI::Generic) —

    The url of the resource.

  • http_request (Net::HTTPGenericRequest) —

    The HTTP request.

  • timeout (Float) —

    The timeout for the built-in Net::HTTP connection.

Returns:

  • (Net::HTTPResponse) —

    The HTTP response.



275
276
277
278
279
280
281
282
283
284
# File 'lib/plaid/internal/http/raw_client.rb', line 275

def perform_request(url, http_request, timeout)
  return @http_client.request(url, http_request) unless @http_client.nil?

  conn = connect(url)
  conn.open_timeout = timeout
  conn.read_timeout = timeout
  conn.write_timeout = timeout
  conn.continue_timeout = timeout
  conn.request(http_request)
end

#protected_header_keys ⇒ Array<Symbol, String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The client-level header names that additional_headers must not replace: every default header except the API's global headers, which are overridable per request.

Returns:

  • (Array<Symbol, String>) —

    The protected header names.



115
116
117
118
119
# File 'lib/plaid/internal/http/raw_client.rb', line 115

def protected_header_keys
  return @default_headers.keys if @overridable_headers.empty?

  @default_headers.keys.reject { |name| @overridable_headers.include?(name.to_s.downcase) }
end

#request_timeout(request) ⇒ Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The request's timeout_in_seconds option, or the client-level timeout.

Parameters:

Returns:

  • (Float) —

    The request's timeout_in_seconds option, or the client-level timeout.



106
107
108
109
110
# File 'lib/plaid/internal/http/raw_client.rb', line 106

def request_timeout(request)
  options = request.request_options || {}
  timeout = options.key?(:timeout_in_seconds) ? options[:timeout_in_seconds] : options["timeout_in_seconds"]
  (timeout.nil? ? @timeout : timeout).to_f
end

#resolve_auth_headers ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves the auth headers to send with the next request. Delegates to the configured auth provider (if any) on every call so that token-based providers (e.g. OAuth client-credentials) can refresh an expired token before the request is sent. Returns an empty hash when no provider is set, which keeps the api-key / basic / bearer / no-auth paths unchanged.

Returns:

  • (Hash) —

    The auth headers for the current request.



220
221
222
223
224
# File 'lib/plaid/internal/http/raw_client.rb', line 220

def resolve_auth_headers
  return {} if @auth_provider.nil?

  @auth_provider.auth_headers
end

#retry_delay(response, attempt) ⇒ Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculates the delay before the next retry attempt using exponential backoff with jitter. Respects Retry-After header if present.

Parameters:

  • response (Net::HTTPResponse) —

    The HTTP response.

  • attempt (Integer) —

    The current retry attempt (0-indexed).

Returns:

  • (Float) —

    The delay in seconds before the next retry.



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/plaid/internal/http/raw_client.rb', line 137

def retry_delay(response, attempt)
  # Check for Retry-After header (can be seconds or HTTP date)
  retry_after = response["Retry-After"]
  if retry_after
    delay = parse_retry_after(retry_after)
    return [delay, MAX_RETRY_DELAY].min if delay&.positive?
  end

  # Exponential backoff with jitter: base_delay * 2^attempt
  base_delay = INITIAL_RETRY_DELAY * (2**attempt)
  add_jitter([base_delay, MAX_RETRY_DELAY].min)
end

#send(request) ⇒ HTTP::Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The HTTP response.

Parameters:

Returns:

  • (HTTP::Response) —

    The HTTP response.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/plaid/internal/http/raw_client.rb', line 53

def send(request)
  url = build_url(request)
  # Resolve auth headers once per request (not per retry) so token-based
  # providers refresh at most once here; static providers are cheap.
  auth_headers = resolve_auth_headers
  timeout = request_timeout(request)
  attempt = 0
  response = nil

  loop do
    http_request = build_http_request(
      url:,
      method: request.method,
      headers: request.encode_headers(protected_keys: protected_header_keys + auth_headers.keys),
      body: request.encode_body,
      auth_headers: auth_headers
    )

    response = wrap_transport_errors { perform_request(url, http_request, timeout) }

    break unless should_retry?(response, attempt)

    delay = retry_delay(response, attempt)
    sleep(delay)
    attempt += 1
  end

  response
end

#should_retry?(response, attempt) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if a request should be retried based on the response status code.

Parameters:

  • response (Net::HTTPResponse) —

    The HTTP response.

  • attempt (Integer) —

    The current retry attempt (0-indexed).

Returns:

  • (Boolean) —

    Whether the request should be retried.



125
126
127
128
129
130
# File 'lib/plaid/internal/http/raw_client.rb', line 125

def should_retry?(response, attempt)
  return false if attempt >= @max_retries

  status = response.code.to_i
  RETRYABLE_STATUSES.include?(status)
end

#validate_https!(url) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises if the URL uses http:// for a non-localhost host, which would send authentication credentials in plaintext.

Parameters:

  • url (URI::Generic) —

    The parsed URL.

Raises:

  • (ArgumentError)


205
206
207
208
209
210
211
212
# File 'lib/plaid/internal/http/raw_client.rb', line 205

def validate_https!(url)
  return if url.scheme != "http"
  return if LOCALHOST_HOSTS.include?(url.host)

  raise ArgumentError,
        "Refusing to send request to non-HTTPS URL: #{url}. " \
        "HTTP is only allowed for localhost. Use HTTPS or pass a localhost URL."
end

#wrap_transport_errors ⇒ Net::HTTPResponse

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs a single request attempt, re-raising transport failures as SDK errors so that rescuing Errors::ApiError covers them. The original exception is kept as cause. These failures are not retried: the server may already have processed the request.

Returns:

  • (Net::HTTPResponse) —

    The HTTP response.



95
96
97
98
99
100
101
102
# File 'lib/plaid/internal/http/raw_client.rb', line 95

def wrap_transport_errors
  yield
rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, Errno::ETIMEDOUT => e
  raise Plaid::Errors::TimeoutError, e.message
rescue EOFError, SocketError, OpenSSL::SSL::SSLError, *NETWORK_ERRNOS,
       Net::ProtocolError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError => e
  raise Plaid::Errors::ConnectionError, e.message
end