Tinkex.API.Headers (Tinkex v0.3.4)

View Source

HTTP header building and management for Tinkex API requests.

Handles:

  • Standard headers (accept, content-type, user-agent, etc.)
  • Authentication headers (API key)
  • Stainless SDK headers
  • Cloudflare Access headers
  • Request-specific headers (idempotency, sampling, retry count)
  • OpenTelemetry trace context propagation (opt-in)
  • Header deduplication

Summary

Functions

Builds complete headers list for a request.

Deduplicates headers, keeping the last occurrence of each header name.

Finds a header value by name (case-insensitive).

Retrieves a header value by name (case-insensitive).

Adds or replaces a header in the headers list.

Adds or updates retry-related headers to a request.

Redacts sensitive header values for logging.

Converts a headers list to a map with lowercased keys.

Functions

build(method, config, opts, timeout_ms)

@spec build(atom(), Tinkex.Config.t(), keyword(), timeout()) :: [
  {String.t(), String.t()}
]

Builds complete headers list for a request.

Combines headers from multiple sources in priority order:

  1. Standard headers (accept, content-type, etc.)
  2. Stainless SDK headers
  3. Cloudflare Access headers
  4. Config default headers
  5. Request-specific headers
  6. Method-specific headers (idempotency, sampling)
  7. User-provided headers from opts
  8. OpenTelemetry trace context (if enabled)

Lower priority headers are overridden by higher priority ones.

dedupe(headers)

@spec dedupe([{String.t(), String.t()}]) :: [{String.t(), String.t()}]

Deduplicates headers, keeping the last occurrence of each header name.

Comparison is case-insensitive, but the original case of the last occurrence is preserved.

find_value(headers, target)

@spec find_value([{String.t(), String.t()}], String.t()) :: String.t() | nil

Finds a header value by name (case-insensitive).

Returns the original value (not lowercased) if found, nil otherwise.

get_normalized(headers, name)

@spec get_normalized([{String.t(), String.t()}], String.t()) :: String.t() | nil

Retrieves a header value by name (case-insensitive).

Returns the lowercased and trimmed value if found, nil otherwise.

put(headers, name, value)

@spec put([{String.t(), String.t()}], String.t(), String.t()) :: [
  {String.t(), String.t()}
]

Adds or replaces a header in the headers list.

Case-insensitive replacement - if a header with the same name exists (regardless of case), it will be replaced.

put_retry_headers(request, attempt, timeout_ms)

@spec put_retry_headers(Finch.Request.t(), non_neg_integer(), timeout()) ::
  Finch.Request.t()

Adds or updates retry-related headers to a request.

Sets:

  • x-stainless-retry-count: Current retry attempt number
  • x-stainless-read-timeout: Request timeout (if not already set)

redact(headers)

@spec redact([{String.t(), String.t()}]) :: [{String.t(), String.t()}]

Redacts sensitive header values for logging.

Redacts:

  • x-api-key
  • cf-access-client-secret
  • authorization
  • proxy-authorization

to_map(headers)

@spec to_map([{String.t(), String.t()}]) :: map()

Converts a headers list to a map with lowercased keys.