Class: Fizzy::Http
- Inherits:
-
Object
- Object
- Fizzy::Http
- Defined in:
- lib/fizzy/http.rb
Overview
HTTP client layer with retry, backoff, and caching support. This is an internal class used by Client; you typically don’t use it directly.
Constant Summary collapse
- USER_AGENT =
Default User-Agent header
"fizzy-sdk-ruby/#{VERSION} (api:#{API_VERSION})".freeze
Instance Method Summary collapse
-
#base_url ⇒ String
The configured base URL.
-
#delete(path, retryable: nil) ⇒ Response
Performs a DELETE request.
-
#get(path, params: {}) ⇒ Response
Performs a GET request.
-
#get_absolute(url, params: {}) ⇒ Response
Performs a GET request to an absolute URL.
-
#initialize(config:, token_provider: nil, auth_strategy: nil, hooks: nil) ⇒ Http
constructor
A new instance of Http.
-
#paginate(path, params: {}) {|Hash| ... } ⇒ Enumerator
Fetches all pages of a paginated resource.
-
#patch(path, body: nil) ⇒ Response
Performs a PATCH request.
-
#post(path, body: nil, retryable: nil) ⇒ Response
Performs a POST request.
-
#post_raw(path, body:, content_type:) ⇒ Response
Performs a POST request with raw binary data.
-
#put(path, body: nil) ⇒ Response
Performs a PUT request.
Constructor Details
#initialize(config:, token_provider: nil, auth_strategy: nil, hooks: nil) ⇒ Http
Returns a new instance of Http.
19 20 21 22 23 24 25 |
# File 'lib/fizzy/http.rb', line 19 def initialize(config:, token_provider: nil, auth_strategy: nil, hooks: nil) @config = config @auth_strategy = auth_strategy || BearerAuth.new(token_provider) @token_provider = token_provider || (@auth_strategy.is_a?(BearerAuth) ? @auth_strategy.token_provider : nil) @hooks = hooks || NoopHooks.new @faraday = build_faraday_client end |
Instance Method Details
#base_url ⇒ String
Returns the configured base URL.
28 29 30 |
# File 'lib/fizzy/http.rb', line 28 def base_url @config.base_url end |
#delete(path, retryable: nil) ⇒ Response
Performs a DELETE request.
78 79 80 |
# File 'lib/fizzy/http.rb', line 78 def delete(path, retryable: nil) request(:delete, path, retryable: retryable) end |
#get(path, params: {}) ⇒ Response
Performs a GET request.
36 37 38 |
# File 'lib/fizzy/http.rb', line 36 def get(path, params: {}) request(:get, path, params: params) end |
#get_absolute(url, params: {}) ⇒ Response
Performs a GET request to an absolute URL. Used for endpoints not on the base API.
45 46 47 |
# File 'lib/fizzy/http.rb', line 45 def get_absolute(url, params: {}) request(:get, url, params: params) end |
#paginate(path, params: {}) {|Hash| ... } ⇒ Enumerator
Fetches all pages of a paginated resource.
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 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/fizzy/http.rb', line 98 def paginate(path, params: {}, &block) return to_enum(:paginate, path, params: params) unless block base_url = build_url(path) url = base_url page = 0 loop do page += 1 break if page > @config.max_pages @hooks.on_paginate(url, page) response = get(url, params: page == 1 ? params : {}) Security.check_body_size!(response.body, Security::MAX_RESPONSE_BODY_BYTES) begin items = JSON.parse(response.body) rescue JSON::ParserError => e raise Fizzy::APIError.new( "Failed to parse paginated response (page #{page}): #{Security.truncate(e.)}" ) end items.each(&block) next_url = parse_next_link(response.headers["Link"]) break if next_url.nil? next_url = Security.resolve_url(url, next_url) unless Security.same_origin?(next_url, base_url) raise Fizzy::APIError.new( "Cross-origin pagination link rejected; refusing to follow " \ "from #{Security.truncate(url.to_s)} to #{Security.truncate(next_url.to_s)}" ) end url = next_url end end |
#patch(path, body: nil) ⇒ Response
Performs a PATCH request.
70 71 72 |
# File 'lib/fizzy/http.rb', line 70 def patch(path, body: nil) request(:patch, path, body: body) end |
#post(path, body: nil, retryable: nil) ⇒ Response
Performs a POST request.
54 55 56 |
# File 'lib/fizzy/http.rb', line 54 def post(path, body: nil, retryable: nil) request(:post, path, body: body, retryable: retryable) end |
#post_raw(path, body:, content_type:) ⇒ Response
Performs a POST request with raw binary data. Used for file uploads.
88 89 90 91 |
# File 'lib/fizzy/http.rb', line 88 def post_raw(path, body:, content_type:) url = build_url(path) single_request_raw(:post, url, body: body, content_type: content_type, attempt: 1) end |
#put(path, body: nil) ⇒ Response
Performs a PUT request.
62 63 64 |
# File 'lib/fizzy/http.rb', line 62 def put(path, body: nil) request(:put, path, body: body) end |