Class: Fizzy::Http

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(config:, token_provider: nil, auth_strategy: nil, hooks: nil) ⇒ Http

Returns a new instance of Http.

Parameters:

  • config (Config)

    configuration settings

  • token_provider (TokenProvider, nil) (defaults to: nil)

    token provider (deprecated, use auth_strategy)

  • auth_strategy (AuthStrategy, nil) (defaults to: nil)

    authentication strategy

  • hooks (Hooks) (defaults to: nil)

    observability hooks



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_urlString

Returns the configured base URL.

Returns:

  • (String)

    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.

Parameters:

  • path (String)

    URL path

  • retryable (Boolean, nil) (defaults to: nil)

    override retry behavior

Returns:



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.

Parameters:

  • path (String)

    URL path

  • params (Hash) (defaults to: {})

    query parameters

Returns:



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.

Parameters:

  • url (String)

    absolute URL

  • params (Hash) (defaults to: {})

    query parameters

Returns:



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.

Parameters:

  • path (String)

    initial URL path

  • params (Hash) (defaults to: {})

    query parameters

Yields:

  • (Hash)

    each item from the response

Returns:

  • (Enumerator)

    if no block given



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.message)}"
      )
    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.

Parameters:

  • path (String)

    URL path

  • body (Hash, nil) (defaults to: nil)

    request body

Returns:



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.

Parameters:

  • path (String)

    URL path

  • body (Hash, nil) (defaults to: nil)

    request body

  • retryable (Boolean, nil) (defaults to: nil)

    override retry behavior (true for idempotent POSTs)

Returns:



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.

Parameters:

  • path (String)

    URL path

  • body (String, IO)

    raw binary data

  • content_type (String)

    MIME content type

Returns:



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.

Parameters:

  • path (String)

    URL path

  • body (Hash, nil) (defaults to: nil)

    request body

Returns:



62
63
64
# File 'lib/fizzy/http.rb', line 62

def put(path, body: nil)
  request(:put, path, body: body)
end