dream_http_client/recorder
Recorder process and state management
Manages HTTP request/response recordings using a process to store state. Supports recording, playback, and passthrough modes.
Types
One part that differs between a recording and the request that no
recording matches. recorded is the value in the recording and
requested is the value in the request, both after the request
transformer. None is a part that is not there: no query, no such
header, or an empty body.
pub type Difference {
Difference(
part: RequestPart,
recorded: option.Option(String),
requested: option.Option(String),
)
}
Constructors
-
Difference( part: RequestPart, recorded: option.Option(String), requested: option.Option(String), )
What a recorder does with each request.
Record: sends the request to the network, and saves the request and its response to a file.Playback: answers the request from a recording, and sends nothing.Passthrough: sends the request to the network, and saves nothing.
Set it with mode(). For a mode in a text, for example from an
environment variable, use parse_mode().
Build a playback recorder 
import dream_http_client/recorder.{directory, mode}
pub fn playback_builder() -> recorder.RecorderBuilder {
recorder.new()
|> directory("mocks/api")
|> mode(recorder.Playback)
}
pub type Mode {
Record
Playback
Passthrough
}
Constructors
-
Record -
Playback -
Passthrough
A recording near a request that no recording matches: the path of its file and the parts that differ, in the order origin, query, headers by name, body.
pub type NearestRecording {
NearestRecording(path: String, differences: List(Difference))
}
Constructors
-
NearestRecording(path: String, differences: List(Difference))
Opaque recorder handle for managing HTTP request/response recordings
A Recorder is a handle to an OTP actor process that manages recording state.
Multiple HTTP requests can share the same recorder by passing the same handle.
The recorder handles saving recordings to disk, loading them for playback,
and matching requests to recorded responses.
Thread Safety
Recorders are safe to use concurrently - multiple requests can use the same recorder handle simultaneously. The internal actor processes messages sequentially.
Lifecycle
- Build with
recorder.new() |> ... |> start() - Attach to requests with
client.recorder(rec) - Optionally cleanup with
recorder.stop(rec)(recordings already saved)
Examples
Record and stop 
import dream_http_client/client
import dream_http_client/recorder.{directory, mode, start}
import gleam/http
import gleam/result
import gleam/string
pub fn record_and_stop(mocks_directory: String) -> Result(Nil, String) {
// Record mode
use rec <- result.try(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> start(),
)
// Use the recorder with requests
use _response <- result.try(
client.new()
|> client.scheme(http.Http)
|> client.host("localhost")
|> client.port(9876)
|> client.path("/text")
|> client.recorder(rec)
|> client.send()
|> result.map_error(string.inspect),
)
// Cleanup (optional: the recordings are already saved)
recorder.stop(rec)
}
pub opaque type Recorder
Builder for configuring and starting a recorder.
Recorder configuration uses a builder so options can evolve without adding a
combinatorial set of start_with_* functions.
Fields
mode:Record,PlaybackorPassthrough, as set bymode(). The default isPassthrough.directory: the recordings directory. It isNoneuntildirectory()is called.key: theMatchKeythat matches requests to recordings. The default uses the method and the URL, as set bykey().request_transformer: changes each request before the key is computed and before it is stored, as set byrequest_transformer(). The default returns the request unchanged.response_transformer: changes each response before it is stored in Record mode, as set byresponse_transformer(). The default returns the response unchanged.
Build a playback recorder 
import dream_http_client/recorder.{directory, mode}
pub fn playback_builder() -> recorder.RecorderBuilder {
recorder.new()
|> directory("mocks/api")
|> mode(recorder.Playback)
}
pub type RecorderBuilder {
RecorderBuilder(
mode: Mode,
directory: option.Option(String),
key: fn(recording.RecordedRequest) -> String,
request_transformer: fn(recording.RecordedRequest) -> recording.RecordedRequest,
response_transformer: fn(
recording.RecordedRequest,
recording.RecordedResponse,
) -> recording.RecordedResponse,
)
}
Constructors
-
RecorderBuilder( mode: Mode, directory: option.Option(String), key: fn(recording.RecordedRequest) -> String, request_transformer: fn(recording.RecordedRequest) -> recording.RecordedRequest, response_transformer: fn( recording.RecordedRequest, recording.RecordedResponse, ) -> recording.RecordedResponse, )
A part of a request that a playback miss compares with a recording.
Origin— the scheme, the host and the port, for examplehttp://localhost:19801.Query— the query string, without the?.Header(name)— the values of one header, joined with", ".Body— the body: its text when it is UTF-8, otherwisebase64:and the base64 form.
pub type RequestPart {
Origin
Query
Header(name: String)
Body
}
Constructors
-
Origin -
Query -
Header(name: String) -
Body
A request transformer applied before keying and persistence.
A transformer is a pure function RecordedRequest -> RecordedRequest that
runs in two places:
- Before computing the match key (so matching uses the transformed request)
- Before persisting recordings to disk (so secrets can be removed)
This is how you implement “scrub secrets and still match”: normalize the request (remove auth headers, drop query params, normalize paths, etc.), then choose a key that matches on the remaining stable fields.
Example (drop Authorization header)
Drop the Authorization header 
import dream_http_client/recorder.{directory, mode, request_transformer}
import dream_http_client/recording
import gleam/list
pub fn drop_auth_header(
request: recording.RecordedRequest,
) -> recording.RecordedRequest {
let headers =
request.headers
|> list.filter(fn(header) { header.0 != "Authorization" })
recording.RecordedRequest(..request, headers: headers)
}
pub fn scrubbing_builder() -> recorder.RecorderBuilder {
recorder.new()
|> mode(recorder.Record)
|> directory("mocks")
|> request_transformer(drop_auth_header)
}
pub type RequestTransformer =
fn(recording.RecordedRequest) -> recording.RecordedRequest
A response transformer applied before persistence in Record mode.
This hook is for scrubbing secrets out of recorded responses (set-cookie, authorization echoes, PII, etc.) before they are written to disk.
Order of operations in Record mode:
- The request transformer runs (request normalization + request scrubbing)
- The key is computed from the transformed request
- The response transformer runs (response scrubbing)
- The scrubbed recording is stored in memory and persisted to disk
Note: this transformer runs only in Record mode. Playback returns whatever is in the saved fixtures.
pub type ResponseTransformer =
fn(recording.RecordedRequest, recording.RecordedResponse) -> recording.RecordedResponse
Values
pub fn add_recording(
recorder: Recorder,
rec: recording.Recording,
) -> Result(Nil, String)
Add a recording to the recorder
Manually adds a recording to the recorder’s in-memory state and saves it to disk immediately if in Record mode. This function is typically called automatically by the HTTP client when a request completes, but can be used directly for testing or manual recording creation.
Parameters
recorder: The recorder to add the recording torec: The recording (request/response pair) to add
Behavior by Mode
- Record mode: Writes the recording to disk, keeps it in memory, and
returns
Ok(Nil)when the file is on disk. The file replaces the files that held the same key - Playback mode: No-op, returns
Ok(Nil)(recordings loaded from disk at startup) - Passthrough mode: No-op, returns
Ok(Nil)(no recording functionality)
Returns
Ok(Nil): the recording is on disk (or the mode keeps no recordings)Error(String): the write failed ("Failed to save recording: ..."), or the recorder did not answer within 5 seconds
Examples
Add a recording by hand 
import dream_http_client/recorder.{directory, mode, start}
import dream_http_client/recording
import gleam/http
import gleam/list
import gleam/option
import gleam/result
pub fn add_by_hand(mocks_directory: String) -> Result(Int, String) {
use rec <- result.try(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> start(),
)
// Manually add a recording
let manual_recording =
recording.Recording(
request: recording.RecordedRequest(
method: http.Get,
scheme: http.Https,
host: "api.example.com",
port: option.None,
path: "/users",
query: option.None,
headers: [],
body: <<>>,
),
response: recording.BlockingResponse(status: 200, headers: [], body: <<
"[]",
>>),
)
// Returns when the recording is on disk, or with the write error
use Nil <- result.map(recorder.add_recording(rec, manual_recording))
list.length(recorder.get_recordings(rec))
}
Notes
- The recording is on disk when this function returns
Ok(Nil): no need to callstop(), and a playback recorder started next finds it - If the write fails, the recording is not kept in memory either, so the same recording can be added again
- An equal recording (the same key and an equal response) is not written twice
- A recording with the same match key and a different response replaces the one before, in memory and on disk
pub fn directory(
builder: RecorderBuilder,
directory: String,
) -> RecorderBuilder
Set the recording directory used by Record and Playback modes.
The directory is required for Record and Playback (validated in
start()), and ignored for Passthrough.
Example
Build a record recorder 
import dream_http_client/matching
import dream_http_client/recorder.{
directory, key, mode, request_transformer, response_transformer,
}
import dream_http_client/recording
pub fn drop_headers(
request: recording.RecordedRequest,
) -> recording.RecordedRequest {
recording.RecordedRequest(..request, headers: [])
}
pub fn drop_body(
_request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse {
case response {
recording.BlockingResponse(status, headers, _body) ->
recording.BlockingResponse(status: status, headers: headers, body: <<>>)
recording.StreamingResponse(..) -> response
}
}
pub fn record_builder(mocks_directory: String) -> recorder.RecorderBuilder {
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> key(matching.request_key(
method: True,
url: True,
headers: False,
body: False,
))
|> request_transformer(drop_headers)
|> response_transformer(drop_body)
}
pub fn find_recording(
recorder: Recorder,
request: recording.RecordedRequest,
) -> Result(option.Option(recording.Recording), String)
Find a matching recording for a request
Searches for a recording that matches the given request based on the recorder’s configured match key and request transformer. This is used internally by the HTTP client during playback, but can be called directly to check if a recording exists.
Parameters
recorder: The recorder to search inrequest: The request to find a matching recording for
Returns
Ok(Some(Recording)): Matching recording found (unambiguous)Ok(None): No matching recording found (or not in Playback mode)Error(String): Playback lookup was ambiguous (multiple recordings share the same key)
Examples
Find a recording 
import dream_http_client/recorder.{directory, mode, start}
import dream_http_client/recording
import gleam/http
import gleam/option
import gleam/result
pub fn look_up_text(mocks_directory: String) -> Result(String, String) {
use rec <- result.map(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Playback)
|> start(),
)
let request =
recording.RecordedRequest(
method: http.Get,
scheme: http.Http,
host: "localhost",
port: option.Some(9876),
path: "/text",
query: option.None,
headers: [],
body: <<>>,
)
case recorder.find_recording(rec, request) {
Ok(option.Some(_recording)) -> "Found recording"
Ok(option.None) -> "No recording found"
Error(reason) -> "Ambiguous match: " <> reason
}
}
Notes
- Only works in Playback mode (returns
Nonein other modes) - When it returns
Nonein Playback mode, the client does not send the request:sendand the stream functions fail withclient.NoRecording(method, url, directory, nearest). In Record and Passthrough mode the client sends the request to the network. - Matching uses the recorder’s configured key + request transformer
- Returns
Noneif recorder doesn’t respond (safe default) - Timeout is 1 second - recorder should respond quickly
pub fn get_recordings(
recorder: Recorder,
) -> List(recording.Recording)
Get all recordings from the recorder
Retrieves all recordings currently stored in the recorder’s in-memory state. In Playback mode, this returns all recordings loaded from disk at startup. In Record mode, this returns all recordings captured so far (including unsaved ones).
Parameters
recorder: The recorder to get recordings from
Returns
List(Recording): All recordings in the recorder (empty list if none or error)
Examples
Read the captured recordings 
import dream_http_client/client
import dream_http_client/recorder.{directory, mode, start}
import gleam/http
import gleam/int
import gleam/list
import gleam/result
import gleam/string
pub fn count_captured(mocks_directory: String) -> Result(String, String) {
use rec <- result.try(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> start(),
)
use _response <- result.map(
client.new()
|> client.scheme(http.Http)
|> client.host("localhost")
|> client.port(9876)
|> client.path("/text")
|> client.recorder(rec)
|> client.send()
|> result.map_error(string.inspect),
)
let recordings = recorder.get_recordings(rec)
"Captured " <> int.to_string(list.length(recordings)) <> " recordings"
}
Notes
- Returns empty list if recorder doesn’t respond (safe default)
- In Record mode, every recording in the list is on disk
- Timeout is 1 second - recorder should respond quickly
pub fn is_record_mode(recorder: Recorder) -> Bool
Check if recorder is in Record mode
Determines whether the recorder is configured to capture and save real HTTP requests/responses. Useful for conditional logic that only applies during recording.
Parameters
recorder: The recorder to check
Returns
True: Recorder is in Record modeFalse: Recorder is in Playback or Passthrough mode
Examples
Check the mode 
import dream_http_client/recorder.{directory, mode, start}
import gleam/result
pub fn recording_mode_active(mocks_directory: String) -> Result(Bool, String) {
use rec <- result.map(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> start(),
)
recorder.is_record_mode(rec)
}
Notes
- Returns
Falseif the recorder process doesn’t respond (safe default) - Timeout is 1 second - recorder should respond quickly
pub fn key(
builder: RecorderBuilder,
key: fn(recording.RecordedRequest) -> String,
) -> RecorderBuilder
Set the match key function.
The key function determines how requests are grouped for playback lookup. Keys should be stable and should generally not include secrets.
The key also names the recording file, so a record run that records a
key again replaces the file of that key. If two files in the directory
hold different recordings with the same key (for example, a file written
by hand), playback lookup becomes ambiguous and find_recording() will
return Error(...).
Example
Build a record recorder 
import dream_http_client/matching
import dream_http_client/recorder.{
directory, key, mode, request_transformer, response_transformer,
}
import dream_http_client/recording
pub fn drop_headers(
request: recording.RecordedRequest,
) -> recording.RecordedRequest {
recording.RecordedRequest(..request, headers: [])
}
pub fn drop_body(
_request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse {
case response {
recording.BlockingResponse(status, headers, _body) ->
recording.BlockingResponse(status: status, headers: headers, body: <<>>)
recording.StreamingResponse(..) -> response
}
}
pub fn record_builder(mocks_directory: String) -> recorder.RecorderBuilder {
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> key(matching.request_key(
method: True,
url: True,
headers: False,
body: False,
))
|> request_transformer(drop_headers)
|> response_transformer(drop_body)
}
pub fn mode(
builder: RecorderBuilder,
mode: Mode,
) -> RecorderBuilder
Set the recorder mode: Record, Playback or Passthrough.
For a mode in a text, use parse_mode() first.
Example
Build a record recorder 
import dream_http_client/matching
import dream_http_client/recorder.{
directory, key, mode, request_transformer, response_transformer,
}
import dream_http_client/recording
pub fn drop_headers(
request: recording.RecordedRequest,
) -> recording.RecordedRequest {
recording.RecordedRequest(..request, headers: [])
}
pub fn drop_body(
_request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse {
case response {
recording.BlockingResponse(status, headers, _body) ->
recording.BlockingResponse(status: status, headers: headers, body: <<>>)
recording.StreamingResponse(..) -> response
}
}
pub fn record_builder(mocks_directory: String) -> recorder.RecorderBuilder {
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> key(matching.request_key(
method: True,
url: True,
headers: False,
body: False,
))
|> request_transformer(drop_headers)
|> response_transformer(drop_body)
}
pub fn new() -> RecorderBuilder
Create a new recorder builder with safe defaults.
Defaults:
mode:Passthroughdirectory: unsetkey:matching.request_key(method: True, url: True, headers: False, body: False)request_transformer: identityresponse_transformer: identity
Example
Build a record recorder 
import dream_http_client/matching
import dream_http_client/recorder.{
directory, key, mode, request_transformer, response_transformer,
}
import dream_http_client/recording
pub fn drop_headers(
request: recording.RecordedRequest,
) -> recording.RecordedRequest {
recording.RecordedRequest(..request, headers: [])
}
pub fn drop_body(
_request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse {
case response {
recording.BlockingResponse(status, headers, _body) ->
recording.BlockingResponse(status: status, headers: headers, body: <<>>)
recording.StreamingResponse(..) -> response
}
}
pub fn record_builder(mocks_directory: String) -> recorder.RecorderBuilder {
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> key(matching.request_key(
method: True,
url: True,
headers: False,
body: False,
))
|> request_transformer(drop_headers)
|> response_transformer(drop_body)
}
pub fn parse_mode(text: String) -> Result(Mode, String)
Read a mode from a text, for example from an environment variable or a command line.
The texts are "record", "playback" and "passthrough". They are
case-sensitive. Any other text gives
Error("Unknown recorder mode: <text>. Expected one of: record, playback, passthrough").
Start a recorder from a mode text 
import dream_http_client/recorder.{
type Recorder, directory, mode, parse_mode, start,
}
import gleam/result
pub fn start_from_text(
mode_text: String,
mocks_directory: String,
) -> Result(Recorder, String) {
use recorder_mode <- result.try(parse_mode(mode_text))
recorder.new()
|> directory(mocks_directory)
|> mode(recorder_mode)
|> start()
}
pub fn request_transformer(
builder: RecorderBuilder,
request_transformer: fn(recording.RecordedRequest) -> recording.RecordedRequest,
) -> RecorderBuilder
Set the request transformer.
The transformer is applied before keying and before persistence, so it’s the right place to normalize requests and scrub secrets.
Example
Build a record recorder 
import dream_http_client/matching
import dream_http_client/recorder.{
directory, key, mode, request_transformer, response_transformer,
}
import dream_http_client/recording
pub fn drop_headers(
request: recording.RecordedRequest,
) -> recording.RecordedRequest {
recording.RecordedRequest(..request, headers: [])
}
pub fn drop_body(
_request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse {
case response {
recording.BlockingResponse(status, headers, _body) ->
recording.BlockingResponse(status: status, headers: headers, body: <<>>)
recording.StreamingResponse(..) -> response
}
}
pub fn record_builder(mocks_directory: String) -> recorder.RecorderBuilder {
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> key(matching.request_key(
method: True,
url: True,
headers: False,
body: False,
))
|> request_transformer(drop_headers)
|> response_transformer(drop_body)
}
pub fn response_transformer(
builder: RecorderBuilder,
response_transformer: fn(
recording.RecordedRequest,
recording.RecordedResponse,
) -> recording.RecordedResponse,
) -> RecorderBuilder
Set the response transformer.
This transformer is applied only in Record mode and runs before persistence so recorded fixtures can be safely committed/shared.
Example
Build a record recorder 
import dream_http_client/matching
import dream_http_client/recorder.{
directory, key, mode, request_transformer, response_transformer,
}
import dream_http_client/recording
pub fn drop_headers(
request: recording.RecordedRequest,
) -> recording.RecordedRequest {
recording.RecordedRequest(..request, headers: [])
}
pub fn drop_body(
_request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse {
case response {
recording.BlockingResponse(status, headers, _body) ->
recording.BlockingResponse(status: status, headers: headers, body: <<>>)
recording.StreamingResponse(..) -> response
}
}
pub fn record_builder(mocks_directory: String) -> recorder.RecorderBuilder {
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> key(matching.request_key(
method: True,
url: True,
headers: False,
body: False,
))
|> request_transformer(drop_headers)
|> response_transformer(drop_body)
}
pub fn start(
builder: RecorderBuilder,
) -> Result(Recorder, String)
Start a new recorder from a builder.
Creates an OTP actor process to manage recorder state. The recorder handles saving recordings to disk (Record mode), loading them for playback (Playback mode), or passing requests through unchanged (Passthrough mode).
Parameters
builder: Recorder configuration builder
Returns
Ok(Recorder): Successfully started recorderError(String): Error message if startup fails (e.g., cannot load recordings in Playback mode)
Examples
Start a playback recorder 
import dream_http_client/recorder.{directory, mode, start}
import gleam/result
pub fn start_playback(mocks_directory: String) -> Result(Bool, String) {
use rec <- result.map(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Playback)
|> start(),
)
recorder.is_record_mode(rec)
}
Notes
- In Playback mode, recordings are loaded from disk at startup
- In Record mode, the recorder reads the files in the directory at startup. The first recording of a key in the run removes the files that held that key, and writes one file with a name from the key. Files of keys that the run does not record stay. A file that cannot be read or decoded stops the start with an error
- In Record mode, a recorded call returns only when its recording is on
disk (the end of the stream for a stream), so there is no need to call
stop(). A failed write is the error of the call - Multiple requests can share the same recorder handle safely
- The recorder process runs until
stop()is called or the VM shuts down
pub fn stop(recorder: Recorder) -> Result(Nil, String)
Stop the recorder and cleanup
Stops the recorder’s OTP actor process and releases resources. In Record mode,
recordings are already saved to disk immediately when captured, so this function
only performs cleanup. Calling stop() is optional but recommended for proper
resource management.
Parameters
recorder: The recorder to stop
Returns
Ok(Nil): Successfully stopped the recorderError(String): Error message if the recorder doesn’t respond within 5 seconds
Examples
Stop a recorder 
import dream_http_client/recorder.{directory, mode, start}
import gleam/result
pub fn start_and_stop(mocks_directory: String) -> Result(String, String) {
use rec <- result.try(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> start(),
)
// Cleanup (optional: the recordings are already saved)
case recorder.stop(rec) {
Ok(_) -> Ok("Recorder stopped")
Error(reason) -> Error("Failed to stop: " <> reason)
}
}
Notes
- A recorded call returns only when its recording is on disk -
stop()is not required for persistence - This function is optional but recommended for proper resource cleanup
- Timeout is 5 seconds - recorder should stop quickly
- After calling
stop(), the recorder handle is no longer valid
pub fn transform_response(
recorder: Recorder,
request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse
Apply the recorder’s response transformer to a response.
Example
Scrub a response 
import dream_http_client/recorder.{directory, mode, response_transformer, start}
import dream_http_client/recording
import gleam/result
fn drop_body(
_request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> recording.RecordedResponse {
case response {
recording.BlockingResponse(status, headers, _body) ->
recording.BlockingResponse(status: status, headers: headers, body: <<>>)
recording.StreamingResponse(..) -> response
}
}
pub fn scrubbed(
mocks_directory: String,
request: recording.RecordedRequest,
response: recording.RecordedResponse,
) -> Result(recording.RecordedResponse, String) {
use rec <- result.map(
recorder.new()
|> directory(mocks_directory)
|> mode(recorder.Record)
|> response_transformer(drop_body)
|> start(),
)
recorder.transform_response(rec, request, response)
}