dream_http_client/matching
Request matching logic
Provides helpers for building stable request match keys.
A match key is a function that converts a recording.RecordedRequest into a
string. Requests that produce the same key are considered equivalent for
recording lookup and playback.
Keys + transformers
For many use cases, a key alone is enough. For cases like “ignore one query
parameter” or “scrub secrets but still match”, pair a key with a
recorder.request_transformer(...) so both matching and persistence see the same
normalized request.
Types
A function that produces a stable match key for a request.
A MatchKey is the core of playback matching: it maps a RecordedRequest to a
deterministic string. Requests that produce the same key are considered the
“same request” for playback lookup.
Tip: If your requests contain secrets (Authorization headers, tokens) or
volatile fields (timestamps, request IDs), use a recorder.request_transformer
to normalize/scrub the request before the key is computed.
Example
import dream_http_client/matching
import dream_http_client/recorder.{key}
let key_fn =
matching.request_key(method: True, url: True, headers: False, body: False)
let builder = recorder.new() |> key(key_fn)
pub type MatchKey =
fn(recording.RecordedRequest) -> String
Values
pub fn request_key(
method method: Bool,
url url: Bool,
headers headers: Bool,
body body: Bool,
) -> fn(recording.RecordedRequest) -> String
Create a request key function from simple include/exclude flags.
This is intended for the most common matching policies. For more advanced
matching (ignore specific headers, normalize paths, drop query params, etc.)
compose a recorder.request_transformer(...) with a custom key function.
Parameters
method: Include the HTTP method in the keyurl: Include the full URL (scheme + host + port + path + query) in the keyheaders: Include request headers in the key (sorted by header name)body: Include the request body in the key
Returns
A MatchKey function.
Example
import dream_http_client/matching
import dream_http_client/recording
import dream_http_client/recorder.{directory, key, mode, request_transformer, start}
import gleam/list
// Build a key that ignores headers and body.
let key =
matching.request_key(method: True, url: True, headers: False, body: False)
// Drop Authorization before keying and before writing to disk.
fn drop_auth(req: recording.RecordedRequest) -> recording.RecordedRequest {
let headers =
req.headers
|> list.filter(fn(h) { h.0 != "Authorization" })
recording.RecordedRequest(..req, headers: headers)
}
let assert Ok(rec) =
recorder.new()
|> mode("record")
|> directory("mocks/api")
|> key(key)
|> request_transformer(drop_auth)
|> start()