dream_http_client/recording
Recording format types and JSON codecs
Defines the structure for storing HTTP request/response recordings in a custom JSON format that supports both blocking and streaming responses.
Types
A streaming chunk with timing information
Represents a single chunk of data from a streaming HTTP response, along with
the delay (in milliseconds) since the previous chunk. Playback uses this
timing to recreate the original streaming behavior: each chunk comes
delay_ms after the previous chunk came. The timeout of the request
limits each wait, as in a live stream. A delay longer than the timeout
ends the stream with a TimedOut failure.
Fields
data: The chunk data as aBitArraydelay_ms: Milliseconds from the arrival of the previous chunk to the arrival of this chunk. Record mode saves 0 for the first chunk. In playback, the delay of the first chunk counts from the start of the stream.
Examples
Describe a chunk 
import dream_http_client/recording
pub fn greeting_chunk() -> recording.Chunk {
recording.Chunk(
data: <<"Hello, ":utf8>>,
// 50ms delay before this chunk
delay_ms: 50,
)
}
pub type Chunk {
Chunk(data: BitArray, delay_ms: Int)
}
Constructors
-
Chunk(data: BitArray, delay_ms: Int)
A recorded HTTP request
Captures all components of an HTTP request for recording and playback. This type is used internally by the recorder to match incoming requests against recorded ones.
Fields
method: HTTP method (GET, POST, PUT, DELETE, etc.)scheme: Protocol scheme (HTTP or HTTPS)host: Server hostname or IP addressport: Optional port number (None uses default: 80 for HTTP, 443 for HTTPS)path: Request path (e.g., “/api/users/123”)query: Optional query string without the leading “?” (e.g., “page=1&limit=10”)headers: List of header name-value pairsbody: Request body as a string
Examples
Describe a request 
import dream_http_client/recording
import gleam/http
import gleam/option
pub fn user_request() -> recording.RecordedRequest {
recording.RecordedRequest(
method: http.Get,
scheme: http.Https,
host: "api.example.com",
port: option.None,
path: "/users/123",
query: option.Some("fields=name,email"),
headers: [#("Authorization", "Bearer token123")],
body: <<>>,
)
}
pub type RecordedRequest {
RecordedRequest(
method: http.Method,
scheme: http.Scheme,
host: String,
port: option.Option(Int),
path: String,
query: option.Option(String),
headers: List(#(String, String)),
body: BitArray,
)
}
Constructors
-
RecordedRequest( method: http.Method, scheme: http.Scheme, host: String, port: option.Option(Int), path: String, query: option.Option(String), headers: List(#(String, String)), body: BitArray, )
A recorded HTTP response
Represents either a blocking (complete) response or a streaming response with chunks. This type captures the response data needed for playback.
Variants
BlockingResponse(status, headers, body): Complete response body received at onceStreamingResponse(status, headers, chunks): Response delivered in chunks with timing. Playback of a status outside 2xx givesHttpFailurewith the status, the headers and all chunks joined as the body, as a live stream does.
Examples
Describe responses 
import dream_http_client/recording
pub fn blocking() -> recording.RecordedResponse {
recording.BlockingResponse(
status: 200,
headers: [#("Content-Type", "application/json")],
body: <<"{\"users\": []}">>,
)
}
pub fn streaming() -> recording.RecordedResponse {
recording.StreamingResponse(
status: 200,
headers: [#("Content-Type", "text/event-stream")],
chunks: [
recording.Chunk(data: <<"chunk1":utf8>>, delay_ms: 100),
recording.Chunk(data: <<"chunk2":utf8>>, delay_ms: 100),
],
)
}
pub type RecordedResponse {
BlockingResponse(
status: Int,
headers: List(#(String, String)),
body: BitArray,
)
StreamingResponse(
status: Int,
headers: List(#(String, String)),
chunks: List(Chunk),
)
}
Constructors
-
BlockingResponse( status: Int, headers: List(#(String, String)), body: BitArray, ) -
StreamingResponse( status: Int, headers: List(#(String, String)), chunks: List(Chunk), )
A complete recording entry (request + response pair)
Represents a single recorded HTTP interaction: one request and its corresponding
response. Multiple Recording values are stored together in a RecordingFile.
Fields
request: The recorded HTTP requestresponse: The recorded HTTP response (blocking or streaming)
Examples
Pair a request with a response 
import dream_http_client/recording
pub fn pair(
recorded_request: recording.RecordedRequest,
recorded_response: recording.RecordedResponse,
) -> recording.Recording {
recording.Recording(request: recorded_request, response: recorded_response)
}
pub type Recording {
Recording(request: RecordedRequest, response: RecordedResponse)
}
Constructors
-
Recording(request: RecordedRequest, response: RecordedResponse)
Recording file format (versioned container)
The top-level structure for storing multiple recordings in a JSON file. Includes a version field for future format compatibility and a list of recording entries.
Fields
version: Format version string (currently “1.0”)entries: List of recording entries (request/response pairs)
Examples
Build a recording file 
import dream_http_client/recording
pub fn file_of(entries: List(recording.Recording)) -> recording.RecordingFile {
recording.RecordingFile(version: "1.0", entries: entries)
}
Notes
- Version field allows future format changes while maintaining backward compatibility
- This type is primarily used internally by
encode_recording_fileanddecode_recording_file
pub type RecordingFile {
RecordingFile(version: String, entries: List(Recording))
}
Constructors
-
RecordingFile(version: String, entries: List(Recording))
Values
pub fn decode_recording_file(
json_string: String,
) -> Result(RecordingFile, String)
Decode a JSON string into a RecordingFile
Parses a JSON string and decodes it into a RecordingFile value. This is
the inverse of encode_recording_file() and is used by the storage module
when loading recordings from disk.
Parameters
json_string: The JSON string to decode (typically read from a file)
Returns
Ok(RecordingFile): Successfully decoded recording fileError(String): Human-readable error message describing why decoding failed
Examples
Decode a recording file 
import dream_http_client/recording
import gleam/int
import gleam/list
import gleam/result
import gleam/string
import simplifile
pub fn count_entries(path: String) -> Result(String, String) {
// Read JSON from one recording file
use json_content <- result.try(
simplifile.read(path) |> result.map_error(string.inspect),
)
// Decode it to a RecordingFile (each file holds one entry)
case recording.decode_recording_file(json_content) {
Ok(file) ->
Ok(
"Loaded " <> int.to_string(list.length(file.entries)) <> " recording(s)",
)
Error(reason) -> Error("Failed to decode: " <> reason)
}
}
Notes
- Most callers should use
storage.load_recordings()instead of calling this directly - This function is used internally by the storage module
- Error messages include field paths to help identify decoding issues
- Handles both blocking and streaming response formats
pub fn encode_recording_file(file: RecordingFile) -> json.Json
Encode a RecordingFile to JSON
Converts an in-memory RecordingFile value into a json.Json tree that can
be rendered to a string and written to disk. This function handles the JSON
encoding of all nested types (requests, responses, chunks, etc.).
Parameters
file: The recording file to encode
Returns
A json.Json value representing the recording file structure, ready to be
serialized with json.to_string().
Examples
Encode a recording file 
import dream_http_client/recording
import gleam/json
pub fn to_json_string(entries: List(recording.Recording)) -> String {
let file = recording.RecordingFile(version: "1.0", entries: entries)
let json_value = recording.encode_recording_file(file)
// Now write the string to disk
json.to_string(json_value)
}
Notes
- Most callers should use
storage.save_recordings()instead of calling this directly - This function is used internally by the storage module
- The JSON format includes a version field for future compatibility