Class: FinchAPI::BaseClient Abstract Private

Inherits:
Object
  • Object
show all
Defined in:
lib/finch-api/base_client.rb

Overview

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.

This class is abstract.

Direct Known Subclasses

Client

Constant Summary collapse

MAX_REDIRECTS =

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.

from whatwg fetch spec

20
PLATFORM_HEADERS =

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.

rubocop:disable Style/MutableConstant

{
  "x-stainless-arch" => FinchAPI::Util.arch,
  "x-stainless-lang" => "ruby",
  "x-stainless-os" => FinchAPI::Util.os,
  "x-stainless-package-version" => FinchAPI::VERSION,
  "x-stainless-runtime" => ::RUBY_ENGINE,
  "x-stainless-runtime-version" => ::RUBY_ENGINE_VERSION
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, timeout: 0.0, max_retries: 0, initial_retry_delay: 0.0, max_retry_delay: 0.0, headers: {}, idempotency_header: nil) ⇒ BaseClient

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 BaseClient.

Parameters:

  • base_url (String)
  • timeout (Float) (defaults to: 0.0)
  • max_retries (Integer) (defaults to: 0)
  • initial_retry_delay (Float) (defaults to: 0.0)
  • max_retry_delay (Float) (defaults to: 0.0)
  • headers (Hash{String=>String, Integer, Array<String, Integer, nil>, nil}) (defaults to: {})
  • idempotency_header (String, nil) (defaults to: nil)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/finch-api/base_client.rb', line 157

def initialize(
  base_url:,
  timeout: 0.0,
  max_retries: 0,
  initial_retry_delay: 0.0,
  max_retry_delay: 0.0,
  headers: {},
  idempotency_header: nil
)
  @requester = FinchAPI::PooledNetRequester.new
  @headers = FinchAPI::Util.normalized_headers(
    self.class::PLATFORM_HEADERS,
    {
      "accept" => "application/json",
      "content-type" => "application/json"
    },
    headers
  )
  @base_url = FinchAPI::Util.parse_uri(base_url)
  @idempotency_header = idempotency_header&.to_s&.downcase
  @max_retries = max_retries
  @timeout = timeout
  @initial_retry_delay = initial_retry_delay
  @max_retry_delay = max_retry_delay
end

Instance Attribute Details

#requesterFinchAPI::PooledNetRequester

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.



146
147
148
# File 'lib/finch-api/base_client.rb', line 146

def requester
  @requester
end

Class Method Details

.follow_redirect(request, status:, response_headers:) ⇒ Hash{Symbol=>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.

Parameters:

  • request (Hash{Symbol=>Object})

    .

    @option request [Symbol] :method

    @option request [URI::Generic] :url

    @option request [HashString=>String] :headers

    @option request [Object] :body

    @option request [Integer] :max_retries

    @option request [Float] :timeout

  • status (Integer)
  • response_headers (Hash{String=>String}, Net::HTTPHeader)

Returns:

  • (Hash{Symbol=>Object})


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/finch-api/base_client.rb', line 87

def follow_redirect(request, status:, response_headers:)
  method, url, headers = request.fetch_values(:method, :url, :headers)
  location =
    Kernel.then do
      URI.join(url, response_headers["location"])
    rescue ArgumentError
      message = "Server responded with status #{status} but no valid location header."
      raise FinchAPI::APIConnectionError.new(url: url, message: message)
    end

  request = {**request, url: location}

  case [url.scheme, location.scheme]
  in ["https", "http"]
    message = "Tried to redirect to a insecure URL"
    raise FinchAPI::APIConnectionError.new(url: url, message: message)
  else
    nil
  end

  # from whatwg fetch spec
  case [status, method]
  in [301 | 302, :post] | [303, _]
    drop = %w[content-encoding content-language content-length content-location content-type]
    request = {
      **request,
      method: method == :head ? :head : :get,
      headers: headers.except(*drop),
      body: nil
    }
  else
  end

  # from undici
  if FinchAPI::Util.uri_origin(url) != FinchAPI::Util.uri_origin(location)
    drop = %w[authorization cookie host proxy-authorization]
    request = {**request, headers: request.fetch(:headers).except(*drop)}
  end

  request
end

.reap_connection!(status, stream:) ⇒ 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.

Parameters:



133
134
135
136
137
138
139
140
141
# File 'lib/finch-api/base_client.rb', line 133

def reap_connection!(status, stream:)
  case status
  in (..199) | (300..499)
    stream&.each { next }
  in FinchAPI::APIConnectionError | (500..)
    FinchAPI::Util.close_fused!(stream)
  else
  end
end

.should_retry?(status, headers:) ⇒ 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.

Parameters:

  • status (Integer)
  • headers (Hash{String=>String}, Net::HTTPHeader)

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/finch-api/base_client.rb', line 49

def should_retry?(status, headers:)
  coerced = FinchAPI::Util.coerce_boolean(headers["x-should-retry"])
  case [coerced, status]
  in [true | false, _]
    coerced
  in [_, 408 | 409 | 429 | (500..)]
    # retry on:
    # 408: timeouts
    # 409: locks
    # 429: rate limits
    # 500+: unknown errors
    true
  else
    false
  end
end

.validate!(req) ⇒ 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.

Parameters:

  • req (Hash{Symbol=>Object})

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/finch-api/base_client.rb', line 29

def validate!(req)
  keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :stream, :model, :options]
  case req
  in Hash
    req.each_key do |k|
      unless keys.include?(k)
        raise ArgumentError.new("Request `req` keys must be one of #{keys}, got #{k.inspect}")
      end
    end
  else
    raise ArgumentError.new("Request `req` must be a Hash or RequestOptions, got #{req.inspect}")
  end
end

Instance Method Details

#inspectString

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)


450
451
452
453
454
455
# File 'lib/finch-api/base_client.rb', line 450

def inspect
  # rubocop:disable Layout/LineLength
  base_url = FinchAPI::Util.unparse_uri(@base_url)
  "#<#{self.class.name}:0x#{object_id.to_s(16)} base_url=#{base_url} max_retries=#{@max_retries} timeout=#{@timeout}>"
  # rubocop:enable Layout/LineLength
end

#request(req) ⇒ 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.

Execute the request specified by ‘req`. This is the method that all resource

methods call into.

Parameters:

  • req (Hash{Symbol=>Object})

    .

    @option req [Symbol] :method

    @option req [String, Array<String>] :path

    @option req [HashString, nil, nil] :query

    @option req [HashInteger, Array<String, Integer, nil>, nil, nil] :headers

    @option req [Object, nil] :body

    @option req [Symbol, nil] :unwrap

    @option req [Class, nil] :page

    @option req [Class, nil] :stream

    @option req [FinchAPI::Converter, Class, nil] :model

    @option req [FinchAPI::RequestOptions, HashSymbol=>Object, nil] :options

Returns:

  • (Object)

Raises:



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/finch-api/base_client.rb', line 420

def request(req)
  self.class.validate!(req)
  model = req.fetch(:model) { FinchAPI::Unknown }
  opts = req[:options].to_h
  FinchAPI::RequestOptions.validate!(opts)
  request = build_request(req.except(:options), opts)
  url = request.fetch(:url)

  # Don't send the current retry count in the headers if the caller modified the header defaults.
  send_retry_header = request.fetch(:headers)["x-stainless-retry-count"] == "0"
  status, response, stream = send_request(
    request,
    redirect_count: 0,
    retry_count: 0,
    send_retry_header: send_retry_header
  )

  decoded = FinchAPI::Util.decode_content(response, stream: stream)
  case req
  in { stream: Class => st }
    st.new(model: model, url: url, status: status, response: response, stream: decoded)
  in { page: Class => page }
    page.new(client: self, req: req, headers: response, page_data: decoded)
  else
    unwrapped = FinchAPI::Util.dig(decoded, req[:unwrap])
    FinchAPI::Converter.coerce(model, unwrapped)
  end
end