Dowser.Client (Dowser.Client v0.3.0)

View Source

Low-level entry point for querying a search backend over HTTP.

request/4 resolves every option into a Dowser.Client.Request, performs it over Dowser.Client.HTTP (OTP's :httpc), and decodes the response body according to :format, returning {:ok, %Dowser.Client.Response{}} or {:error, exception}.

All option resolution — header merging, URL/query building and body encoding — happens in Dowser.Client.Request.new/5 before the transport is called; response decoding follows the same :format.

Errors

Errors are always Dowser exceptions, never a dependency's own exception type:

Options

Every option below can be set globally (config :dowser_client, ...), per context (Dowser.Client.Context), or per request — most specific wins, except :http_opts, which merges across all three (most specific wins per key).

  • :context — the search backend to query (see Dowser.Client.Context); absent resolves the :default entry from config :dowser_client, contexts: [...] (there is no other built-in fallback).
  • :params — query-string parameters appended to the URL.
  • :format — :json (default), :ndjson or :raw; selects the codec used to encode the request body and decode the response body (:raw leaves both untouched). Mutually exclusive with :req_format/:resp_format.
  • :req_format / :resp_format — set the request and response formats independently; each falls back to :format (so :json) when omitted.
  • :keys — :strings (default), :atoms, :atoms!, or a (String.t() -> term) function; how a decoded response body's keys are cast.
  • :decoder — a module exporting decode/2, a (body, opts -> term) function, or either paired with the options it needs ({MyDecoder, mapping: mapping}), owning the decoded response body. It receives its own options plus :key_fn and :context, and does the backend-specific casting dowser_client can't: finding documents in the envelope, resolving each one's mapping, casting its fields.
  • :encoder — a module exporting encode/2, a (source, opts -> term) function, or either paired with the options it needs ({MyEncoder, index: "articles"}), casting a document source in a request body against the mapping of the index it is going to. A query is never encoded.
  • :encode — where that source is in this request's body: false (default), true (the body is the source), a path (["doc"] for an update), or a list of paths. Per request only — the call site is the only place that knows. See Dowser.Client.Encoder.
  • :keys and :decoder drive one optional second pass over the decoded body; with neither set there is no second pass and the body stays the plain, string-keyed term Dowser.Client.JSON/Dowser.Client.NDJSON produced. See Dowser.Client.Decoder.
  • :http_opts — transport options forwarded to Dowser.Client.HTTP.request/5 — :connect_timeout, :timeout, :ssl, :profile, :profile_opts, ... — including :headers (a map or list of {name, value} pairs, merged over global and context headers plus a derived content-type). :ssl and :profile_opts merge per key across the three tiers; see Dowser.Client.HTTP.
  • :retry — retry policy for transient failures; see Dowser.Client.Retry.

Summary

Types

method()

@type method() :: Dowser.Client.HTTP.method()

result()

@type result() :: {:ok, Dowser.Client.Response.t()} | {:error, Exception.t()}

Functions

delete(path, body \\ nil, opts \\ [])

@spec delete(String.t(), term(), keyword()) :: result()

Runs a DELETE; see request/4.

get(path, opts \\ [])

@spec get(String.t(), keyword()) :: result()

Runs a GET; see request/4.

patch(path, body \\ nil, opts \\ [])

@spec patch(String.t(), term(), keyword()) :: result()

Runs a PATCH; see request/4.

post(path, body \\ nil, opts \\ [])

@spec post(String.t(), term(), keyword()) :: result()

Runs a POST; see request/4.

put(path, body \\ nil, opts \\ [])

@spec put(String.t(), term(), keyword()) :: result()

Runs a PUT; see request/4.

request(method, path, body \\ nil, opts \\ [])

@spec request(method(), String.t(), term(), keyword()) :: result()

Runs an HTTP request against the context given in opts[:context].

Returns {:ok, %Dowser.Client.Response{}} on a completed exchange (any HTTP status), or {:error, exception} when the context cannot be resolved, the body cannot be encoded/decoded, or the transport fails.