Class: Increase::Internal::Transport::BaseClient Abstract Private
- Inherits:
-
Object
- Object
- Increase::Internal::Transport::BaseClient
- Extended by:
- Util::SorbetRuntimeSupport
- Defined in:
- lib/increase/internal/transport/base_client.rb,
sig/increase/internal/transport/base_client.rbs
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.
Direct Known Subclasses
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- Increase =
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.
Instance Attribute Summary collapse
- #base_url ⇒ URI::Generic readonly private
- #headers ⇒ Hash{String=>String} readonly private
- #idempotency_header ⇒ String? readonly private
- #initial_retry_delay ⇒ Float readonly private
- #max_retries ⇒ Integer readonly private
- #max_retry_delay ⇒ Float readonly private
- #requester ⇒ Increase::Internal::Transport::PooledNetRequester readonly private
- #timeout ⇒ Float readonly private
Class Method Summary collapse
- .follow_redirect(request, status:, response_headers:) ⇒ Hash{Symbol=>Object} private
- .reap_connection!(status, stream:) ⇒ Object private
- .should_retry?(status, headers:) ⇒ Boolean private
- .validate!(req) ⇒ Object private
Instance Method Summary collapse
-
#initialize(base_url:, timeout: 0.0, max_retries: 0, initial_retry_delay: 0.0, max_retry_delay: 0.0, headers: {}, idempotency_header: nil) ⇒ BaseClient
constructor
private
A new instance of BaseClient.
- #inspect ⇒ String private
-
#request(method, path, query: {}, headers: {}, body: nil, unwrap: nil, page: nil, stream: nil, model: Increase::Internal::Type::Unknown, options: {}) ⇒ Object
private
Execute the request specified by
req. - #send_request(request, redirect_count:, retry_count:) ⇒ Array(Integer, Net::HTTPResponse, Enumerable<String>) private
Methods included from Util::SorbetRuntimeSupport
const_missing, define_sorbet_constant!, sorbet_constant_defined?, to_sorbet_type, to_sorbet_type
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.
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 |
# File 'lib/increase/internal/transport/base_client.rb', line 178 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 = Increase::Internal::Transport::PooledNetRequester.new @headers = Increase::Internal::Util.normalized_headers( { "accept" => "application/json", "content-type" => "application/json", "user-agent" => user_agent }, headers ) @base_url_components = Increase::Internal::Util.parse_uri(base_url) @base_url = Increase::Internal::Util.unparse_uri(@base_url_components) @idempotency_header = idempotency_header&.to_s&.downcase @timeout = timeout @max_retries = max_retries @initial_retry_delay = initial_retry_delay @max_retry_delay = max_retry_delay end |
Instance Attribute Details
#base_url ⇒ URI::Generic (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.
145 146 147 |
# File 'lib/increase/internal/transport/base_client.rb', line 145 def base_url @base_url end |
#headers ⇒ Hash{String=>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.
160 161 162 |
# File 'lib/increase/internal/transport/base_client.rb', line 160 def headers @headers end |
#idempotency_header ⇒ 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.
163 164 165 |
# File 'lib/increase/internal/transport/base_client.rb', line 163 def idempotency_header @idempotency_header end |
#initial_retry_delay ⇒ Float (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.
154 155 156 |
# File 'lib/increase/internal/transport/base_client.rb', line 154 def initial_retry_delay @initial_retry_delay end |
#max_retries ⇒ Integer (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.
151 152 153 |
# File 'lib/increase/internal/transport/base_client.rb', line 151 def max_retries @max_retries end |
#max_retry_delay ⇒ Float (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.
157 158 159 |
# File 'lib/increase/internal/transport/base_client.rb', line 157 def max_retry_delay @max_retry_delay end |
#requester ⇒ Increase::Internal::Transport::PooledNetRequester (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.
167 168 169 |
# File 'lib/increase/internal/transport/base_client.rb', line 167 def requester @requester end |
#timeout ⇒ Float (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.
148 149 150 |
# File 'lib/increase/internal/transport/base_client.rb', line 148 def timeout @timeout 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.
79 80 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/increase/internal/transport/base_client.rb', line 79 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 = "Server responded with status #{status} but no valid location header." raise Increase::Errors::APIConnectionError.new( url: url, response: response_headers, message: ) end request = {**request, url: location} case [url.scheme, location.scheme] in ["https", "http"] = "Tried to redirect to a insecure URL" raise Increase::Errors::APIConnectionError.new( url: url, response: response_headers, 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 Increase::Internal::Util.uri_origin(url) != Increase::Internal::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.
133 134 135 136 137 138 139 140 141 |
# File 'lib/increase/internal/transport/base_client.rb', line 133 def reap_connection!(status, stream:) case status in (..199) | (300..499) stream&.each { next } in Increase::Errors::APIConnectionError | (500..) Increase::Internal::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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/increase/internal/transport/base_client.rb', line 41 def should_retry?(status, headers:) coerced = Increase::Internal::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.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/increase/internal/transport/base_client.rb', line 21 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
#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.
491 492 493 494 495 |
# File 'lib/increase/internal/transport/base_client.rb', line 491 def inspect # rubocop:disable Layout/LineLength "#<#{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(method, path, query: {}, headers: {}, body: nil, unwrap: nil, page: nil, stream: nil, model: Increase::Internal::Type::Unknown, options: {}) ⇒ 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.
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/increase/internal/transport/base_client.rb', line 452 def request(req) self.class.validate!(req) model = req.fetch(:model) { Increase::Internal::Type::Unknown } opts = req[:options].to_h unwrap = req[:unwrap] Increase::RequestOptions.validate!(opts) request = build_request(req.except(:options), opts) url = request.fetch(:url) status, response, stream = send_request( request, redirect_count: 0, retry_count: 0 ) headers = Increase::Internal::Util.normalized_headers(response.each_header.to_h) decoded = Increase::Internal::Util.decode_content(headers, stream: stream) case req in {stream: Class => st} st.new( model: model, url: url, status: status, headers: headers, response: response, unwrap: unwrap, stream: decoded ) in {page: Class => page} page.new(client: self, req: req, headers: headers, page_data: decoded) else unwrapped = Increase::Internal::Util.dig(decoded, unwrap) Increase::Internal::Type::Converter.coerce(model, unwrapped) end end |
#send_request(request, redirect_count:, retry_count:) ⇒ Array(Integer, Net::HTTPResponse, Enumerable<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.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/increase/internal/transport/base_client.rb', line 354 def send_request(request, redirect_count:, retry_count:) url, max_retries, timeout = request.fetch_values(:url, :max_retries, :timeout) input = {**request.except(:timeout), deadline: Increase::Internal::Util.monotonic_secs + timeout} begin status, response, stream = @requester.execute(input) rescue Increase::Errors::APIConnectionError => e status = e end headers = Increase::Internal::Util.normalized_headers(response&.each_header&.to_h) case status in ..299 [status, response, stream] in 300..399 if redirect_count >= self.class::MAX_REDIRECTS self.class.reap_connection!(status, stream: stream) = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects." raise Increase::Errors::APIConnectionError.new(url: url, response: response, message: ) in 300..399 self.class.reap_connection!(status, stream: stream) request = self.class.follow_redirect(request, status: status, response_headers: headers) send_request( request, redirect_count: redirect_count + 1, retry_count: retry_count ) in Increase::Errors::APIConnectionError if retry_count >= max_retries raise status in (400..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: headers) decoded = Kernel.then do Increase::Internal::Util.decode_content(headers, stream: stream, suppress_error: true) ensure self.class.reap_connection!(status, stream: stream) end raise Increase::Errors::APIStatusError.for( url: url, status: status, headers: headers, body: decoded, request: nil, response: response ) in (400..) | Increase::Errors::APIConnectionError self.class.reap_connection!(status, stream: stream) delay = retry_delay(response || {}, retry_count: retry_count) sleep(delay) send_request( request, redirect_count: redirect_count, retry_count: retry_count + 1 ) end end |