Class: FlyIO::Transport
- Inherits:
-
Object
- Object
- FlyIO::Transport
- Defined in:
- lib/fly_io/transport.rb
Constant Summary collapse
- METHODS =
%i[get head options post put patch delete].freeze
- SAFE_METHODS =
%i[get head options].freeze
- RETRYABLE_STATUSES =
[408, 429, 500, 502, 503, 504].freeze
- ERROR_CLASSES =
{ 400 => ValidationError, 401 => AuthenticationError, 403 => AuthorizationError, 404 => NotFoundError, 408 => RequestTimeoutError, 409 => ValidationError, 410 => NotFoundError, 412 => ValidationError, 422 => ValidationError, 429 => RateLimitError }.freeze
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
- #build_path(template, path_params = {}) ⇒ Object
-
#initialize(configuration) ⇒ Transport
constructor
A new instance of Transport.
- #request(method:, path:, path_params: {}, query: {}, headers: {}, body: FlyIO::UNSET, content_type: "application/json", accept: "application/json", retry_unsafe: nil, operation: nil, timeout: nil) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ Transport
Returns a new instance of Transport.
28 29 30 31 32 |
# File 'lib/fly_io/transport.rb', line 28 def initialize(configuration) @configuration = configuration @connection = build_connection unless configuration.adapter.respond_to?(:call) freeze end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
26 27 28 |
# File 'lib/fly_io/transport.rb', line 26 def configuration @configuration end |
Instance Method Details
#build_path(template, path_params = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fly_io/transport.rb', line 77 def build_path(template, path_params = {}) value = String(template) raise FlyIO::ArgumentError, "path must start with /" unless value.start_with?("/") if value.match?(%r{\A//|[?#\\\x00]}) raise FlyIO::ArgumentError, "path must not contain a URL, query, fragment, backslash, or NUL" end provided = stringify_keys(path_params) result = value.gsub(/\{([^}]+)\}/) do name = Regexp.last_match(1) raise FlyIO::ArgumentError, "missing required path parameter: #{name}" unless provided.key?(name) escape_path_component(provided.fetch(name), name) end unused = provided.keys - value.scan(/\{([^}]+)\}/).flatten raise FlyIO::ArgumentError, "unknown path parameters: #{unused.join(', ')}" unless unused.empty? if result.split("/").any? { |segment| [".", ".."].include?(URI.decode_www_form_component(segment)) } raise FlyIO::ArgumentError, "path traversal segments are not allowed" end result rescue URI::InvalidURIError, ::ArgumentError raise FlyIO::ArgumentError, "path contains invalid encoding" end |
#request(method:, path:, path_params: {}, query: {}, headers: {}, body: FlyIO::UNSET, content_type: "application/json", accept: "application/json", retry_unsafe: nil, operation: nil, timeout: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fly_io/transport.rb', line 34 def request(method:, path:, path_params: {}, query: {}, headers: {}, body: FlyIO::UNSET, content_type: "application/json", accept: "application/json", retry_unsafe: nil, operation: nil, timeout: nil) verb = validate_method(method) = build_path(path, path_params) url = absolute_url(build_url(, query)) request_headers = default_headers(accept).merge(stringify_keys(headers)) encoded_body = encode_body(body, content_type, request_headers) = (verb, , query, request_headers, body, operation) attempts = 0 loop do begin raw_response = perform(verb, url, request_headers, encoded_body, timeout) rescue Faraday::TimeoutError if attempts < configuration.max_retries && retry_allowed?(verb, retry_unsafe) configuration.sleeper.call(retry_delay(attempts, {})) attempts += 1 next end raise RequestTimeoutError.new("Fly.io API request timed out", request: ) rescue Faraday::ConnectionFailed, Faraday::SSLError => e if attempts < configuration.max_retries && retry_allowed?(verb, retry_unsafe) configuration.sleeper.call(retry_delay(attempts, {})) attempts += 1 next end raise TransportError.new("Fly.io API transport failed: #{e.class}", request: ) end if retry_status?(raw_response.status, verb, retry_unsafe) && attempts < configuration.max_retries delay = retry_delay(attempts, raw_response.headers) attempts += 1 configuration.sleeper.call(delay) next end response = build_response(raw_response, , operation) raise_api_error(response) unless response.success? log(:debug, "Fly.io API #{verb.to_s.upcase} #{} -> #{response.status}", ) return response end end |