Dowser.Client.Request (Dowser.Client v0.1.1)

View Source

A fully-resolved HTTP request, ready to hand to an HTTP adapter.

new/5 turns a Dowser.Client.Config plus a method, path, body and per-request options into a %Request{}. All option resolution happens here, so an adapter only ever sees a finished request:

  • headers — merge of global (config :dowser_client, :http_opts, headers: ...), config (auth + config http_opts[:headers]) and per-request (opts[:http_opts][:headers]) headers, plus a content-type derived from the request format. Later sources win on name clashes. Each source accepts either a map or a list of {name, value} pairs.
  • url — the config's endpoint joined with the request path and, when given, opts[:params] encoded as a query string. An absolute path is used verbatim.
  • body — cast through :codec_adapter (see Casting below), then encoded according to the request format (:json, :ndjson or :raw) using the resolved JSON adapter and :json_opts.
  • adapter / http_opts — the resolved :http_adapter and :http_opts (minus :headers, forwarded separately — see above), merged global → config → request.
  • retry — the resolved retry policy, from opts[:retry]; see Dowser.Client.Retry.

Formats

The request and response bodies each have a format (:json, :ndjson or :raw), resolved from the options:

  • :format — sets both, and is mutually exclusive with :req_format / :resp_format. Defaults to :json.
  • :req_format / :resp_format — set each direction independently; a missing one falls back to :format (so :json).

Casting

Response/request bodies are cast in two independent ways:

  • :keys:strings (default), :atoms or :atoms!; how a decoded response body's keys are cast. Falls back to the config's :keys, then config :dowser_client, :keys, then :strings. Resolved into a :key_fn function and passed to :codec_adapter via :codec_opts — the default Dowser.Client.Codec.Default applies it; a custom :codec_adapter is responsible for applying it itself if it wants the same behavior.
  • :codec_adapter — a module implementing Dowser.Client.Codec, falling back to the config's :codec_adapter, then config :dowser_client, :codec_adapter, then Dowser.Client.Codec.Default (dowser_client has no backend-specific knowledge of its own beyond that). When set, codec_adapter.decode/2 casts the response body in one pass, and codec_adapter.encode/2 casts the request body before JSON encoding. :codec_opts (merged global → config → request) is forwarded to both, alongside :key_fn and :config (the resolved Dowser.Client.Config).

All casting is skipped when the format is :raw or the body is empty.

Summary

Types

format()

@type format() :: :json | :ndjson | :raw

keys()

@type keys() :: :strings | :atoms | :atoms!

Functions

key_fn(atom)

new(config, method, path, body, opts)

run(request)