Dowser. Client. Context
(Dowser.Client v0.3.0)
View Source
One search backend to talk to: its :endpoint, :auth, the :httpc profile
to send through, and how to cast the responses it returns.
The name follows elastic/cli's notion of a context — a named cluster you
address by name instead of repeating its coordinates at every call site.
Build one inline with new/1, or configure named ones at compile time:
config :dowser_client,
contexts: [
default: [endpoint: "http://localhost:9200", auth: {:basic, "user", "changeme"}],
logs: [endpoint: "https://logs.internal:9200", profile: :logs]
]Fields
:endpoint— the backend's base URL (required).:auth— credentials, applied as anauthorizationrequest header:{:basic, "dXNlcjpwYXNz"} # Basic <value>, already encoded {:basic, "user", "changeme"} # Basic <base64("user:changeme")> {:api_key, "VnVhQ2ZHY0..."} # ApiKey <value> {:api_key, "id", "api_key"} # ApiKey <base64("id:api_key")> {:bearer, "token"} # Bearer <token> {:header, "x-custom-auth", "..."} # any header you like:profile— the:httpcprofile requests go through, giving this backend its own connection pool and session settings. Defaults to:dowser_client; seeDowser.Client.HTTP.Profile.:profile_opts— that profile's:httpc.set_options/2settings (max_sessions,max_keep_alive_length,keep_alive_timeout,pipeline_timeout,cookies, ...). Applied once, when the profile starts.:http_opts— per-request transport options::headers,:connect_timeout,:timeout,:ssl, ...; seeDowser.Client.HTTP.:keys—:strings(default),:atoms,:atoms!or a function; how a decoded response body's keys are cast.:decoder— a module exportingdecode/2, a(body, opts -> term)function, or either paired with the options it needs ({MyDecoder, mapping: mapping}), owning the decoded response body: the backend package's own casting, whatever its envelope looks like. With neither:keysnor:decoderset, a response stays the plain, string-keyed termDowser.Client.JSONproduced. SeeDowser.Client.Decoder.:encoder— a module exportingencode/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. It only runs when a request's:encodesays where that source is; a query is never touched. SeeDowser.Client.Encoder.
resolve/1 — used by Dowser.Client.request/4 on opts[:context] — turns
any of the following into a %Context{}:
nil— resolves the:defaultentry fromconfig :dowser_client, contexts: [...]. There is no built-in fallback; a:defaultentry must be configured, or every call site must pass:contextexplicitly.- an atom — looks up that name in the same
contextslist/map. - a map or keyword list — builds an ad-hoc context inline via
new/1. - a
%Context{}— returned as-is.
Every field but :endpoint defaults to nil/[] on the struct itself; when
unset, Dowser.Client.Request resolves it at request time from
config :dowser_client, <the same key>, falling back to the :dowser_client
profile, string keys and no decoder — so they can be changed at runtime (e.g.
in config/runtime.exs), not just at compile time.
Summary
Functions
Fetches the named context from config :dowser_client, contexts: [...],
returning {:ok, %Context{}} or :error.
Builds a context from a map or keyword list, ignoring keys that aren't fields.
Resolves anything a :context option accepts into a %Context{}.
Types
Anything resolve/1 accepts: a context, a name, or attributes for new/1.
@type t() :: %Dowser.Client.Context{ auth: auth() | nil, decoder: Dowser.Client.Decoder.decoder() | nil, encoder: Dowser.Client.Encoder.encoder() | nil, endpoint: String.t(), http_opts: keyword(), keys: Dowser.Client.Decoder.keys() | nil, profile: Dowser.Client.HTTP.Profile.t() | nil, profile_opts: keyword() }
Functions
Fetches the named context from config :dowser_client, contexts: [...],
returning {:ok, %Context{}} or :error.
Builds a context from a map or keyword list, ignoring keys that aren't fields.
Raises ArgumentError when :endpoint is missing.
Resolves anything a :context option accepts into a %Context{}.
See the module documentation for what each shape means.