Class: Weft::FetchApi

Inherits:
Object
  • Object
show all
Defined in:
lib/weft/generated/api/fetch_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client = ApiClient.default) ⇒ FetchApi

Returns a new instance of FetchApi.



19
20
21
# File 'lib/weft/generated/api/fetch_api.rb', line 19

def initialize(api_client = ApiClient.default)
  @api_client = api_client
end

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client.



17
18
19
# File 'lib/weft/generated/api/fetch_api.rb', line 17

def api_client
  @api_client
end

Instance Method Details

#fetch(fetch_request, opts = {}) ⇒ FetchResponse

Pay-and-fetch any URL (x402 proxy) Universal x402 fetch proxy. The caller provides a target url, a hard max_cost_usd ceiling, and optional method / body / headers. Weft: 1. Issues the request. 2. On 402 Payment Required, parses the merchant's challenge. 3. Compares the asking price to max_cost_usd and the buyer's policy (max_tx_usd, daily/weekly limits). 4. Signs an EIP-3009 transfer from the buyer's wallet. 5. Replays the request with the X-Payment header. 6. Streams the upstream artifact back, base64-encoded under body_base64, with paid_usd, held_usd, payment_status, tx_hash, and the merchant's reputation snapshot. paid_usd is "0.00" until the charge is CONFIRMED settled — a signed-but- unsettled hold (the common case for x402, which settles asynchronously) reports its amount in held_usd instead, never in paid_usd. Errors are structured with a stable error code, and each error response carries the buyer's policy, balance, and a dashboard_url so a CLI can render an actionable message without a second round-trip. Account-scoped: the bearer must be a buyer-scoped API key. Forwarded headers: the caller's headers are passed through to the upstream, except a denylist of hop-by-hop and Weft-internal headers (host, authorization, cookie, proxy-authorization, x-forwarded-*, x-real-ip, x-payment, connection, upgrade). Up to 32 headers, 4 KB of combined value bytes.

Parameters:

  • fetch_request (FetchRequest)
  • opts (Hash) (defaults to: {})

    the optional parameters

Options Hash (opts):

  • :idempotency_key (String)

    Opaque caller-generated retry key. Reusing the same key for the same buyer converges on one paid fetch; keys are hashed and namespaced by buyer before storage. Send this header for every unattended or retryable paid request.

Returns:



28
29
30
31
# File 'lib/weft/generated/api/fetch_api.rb', line 28

def fetch(fetch_request, opts = {})
  data, _status_code, _headers = fetch_with_http_info(fetch_request, opts)
  data
end

#fetch_with_http_info(fetch_request, opts = {}) ⇒ Array<(FetchResponse, Integer, Hash)>

Pay-and-fetch any URL (x402 proxy) Universal x402 fetch proxy. The caller provides a target `url`, a hard `max_cost_usd` ceiling, and optional `method` / `body` / `headers`. Weft: 1. Issues the request. 2. On `402 Payment Required`, parses the merchant's challenge. 3. Compares the asking price to `max_cost_usd` and the buyer's policy (`max_tx_usd`, daily/weekly limits). 4. Signs an EIP-3009 transfer from the buyer's wallet. 5. Replays the request with the `X-Payment` header. 6. Streams the upstream artifact back, base64-encoded under `body_base64`, with `paid_usd`, `held_usd`, `payment_status`, `tx_hash`, and the merchant's reputation snapshot. `paid_usd` is &quot;0.00&quot; until the charge is CONFIRMED settled — a signed-but- unsettled hold (the common case for x402, which settles asynchronously) reports its amount in `held_usd` instead, never in `paid_usd`. Errors are structured with a stable `error` code, and each error response carries the buyer's `policy`, `balance`, and a `dashboard_url` so a CLI can render an actionable message without a second round-trip. Account-scoped: the bearer must be a buyer-scoped API key. Forwarded headers: the caller's `headers` are passed through to the upstream, except a denylist of hop-by-hop and Weft-internal headers (`host`, `authorization`, `cookie`, `proxy-authorization`, `x-forwarded-*`, `x-real-ip`, `x-payment`, `connection`, `upgrade`). Up to 32 headers, 4 KB of combined value bytes.

Parameters:

  • fetch_request (FetchRequest)
  • opts (Hash) (defaults to: {})

    the optional parameters

Options Hash (opts):

  • :idempotency_key (String)

    Opaque caller-generated retry key. Reusing the same key for the same buyer converges on one paid fetch; keys are hashed and namespaced by buyer before storage. Send this header for every unattended or retryable paid request.

Returns:

  • (Array<(FetchResponse, Integer, Hash)>)

    FetchResponse data, response status code and response headers



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
76
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
102
103
104
# File 'lib/weft/generated/api/fetch_api.rb', line 39

def fetch_with_http_info(fetch_request, opts = {})
  if @api_client.config.debugging
    @api_client.config.logger.debug 'Calling API: FetchApi.fetch ...'
  end
  # verify the required parameter 'fetch_request' is set
  if @api_client.config.client_side_validation && fetch_request.nil?
    fail ArgumentError, "Missing the required parameter 'fetch_request' when calling FetchApi.fetch"
  end
  if @api_client.config.client_side_validation && !opts[:'idempotency_key'].nil? && opts[:'idempotency_key'].to_s.length > 255
    fail ArgumentError, 'invalid value for "opts[:"idempotency_key"]" when calling FetchApi.fetch, the character length must be smaller than or equal to 255.'
  end

  if @api_client.config.client_side_validation && !opts[:'idempotency_key'].nil? && opts[:'idempotency_key'].to_s.length < 1
    fail ArgumentError, 'invalid value for "opts[:"idempotency_key"]" when calling FetchApi.fetch, the character length must be greater than or equal to 1.'
  end

  pattern = Regexp.new(/^[!-~]+$/)
  if @api_client.config.client_side_validation && !opts[:'idempotency_key'].nil? && opts[:'idempotency_key'] !~ pattern
    fail ArgumentError, "invalid value for 'opts[:\"idempotency_key\"]' when calling FetchApi.fetch, must conform to the pattern #{pattern}."
  end

  # resource path
  local_var_path = '/api/v1/fetch'

  # query parameters
  query_params = opts[:query_params] || {}

  # header parameters
  header_params = opts[:header_params] || {}
  # HTTP header 'Accept' (if needed)
  header_params['Accept'] = @api_client.select_header_accept(['application/json']) unless header_params['Accept']
  # HTTP header 'Content-Type'
  content_type = @api_client.select_header_content_type(['application/json'])
  if !content_type.nil?
      header_params['Content-Type'] = content_type
  end
  header_params[:'Idempotency-Key'] = opts[:'idempotency_key'] if !opts[:'idempotency_key'].nil?

  # form parameters
  form_params = opts[:form_params] || {}

  # http body (model)
  post_body = opts[:debug_body] || @api_client.object_to_http_body(fetch_request)

  # return_type
  return_type = opts[:debug_return_type] || 'FetchResponse'

  # auth_names
  auth_names = opts[:debug_auth_names] || ['bearerAuth']

  new_options = opts.merge(
    :operation => :"FetchApi.fetch",
    :header_params => header_params,
    :query_params => query_params,
    :form_params => form_params,
    :body => post_body,
    :auth_names => auth_names,
    :return_type => return_type
  )

  data, status_code, headers = @api_client.call_api(:POST, local_var_path, new_options)
  if @api_client.config.debugging
    @api_client.config.logger.debug "API called: FetchApi#fetch\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
  end
  return data, status_code, headers
end