dream_http_client/storage
File I/O for recordings
Handles loading and saving recording files to/from the filesystem.
Values
pub fn load_recordings(
directory: String,
) -> Result(List(recording.Recording), String)
Load recordings from a directory
Scans the directory for all .json files and loads them as individual recordings.
This function is used internally by the recorder when starting in Playback mode, but
can also be called directly to inspect or manipulate recordings.
Parameters
directory: The directory containing individual recording files
Returns
Ok(List(Recording)): Successfully loaded recordings (empty list if directory doesn’t exist)Error(String): Error message if directory exists but files cannot be read or decoded
Examples
Load recordings 
import dream_http_client/storage
import gleam/int
import gleam/list
pub fn describe_recordings(mocks_directory: String) -> String {
// Load recordings for inspection
case storage.load_recordings(mocks_directory) {
Ok(recordings) ->
"Loaded " <> int.to_string(list.length(recordings)) <> " recordings"
Error(reason) -> "Failed to load: " <> reason
}
}
Notes
- Returns an empty list (not an error) if the directory doesn’t exist
- This is the expected behavior for Playback mode when no recordings have been created yet
- Each file contains a single recording in the versioned JSON format
pub fn save_recording_immediately(
directory: String,
rec: recording.Recording,
key: String,
) -> Result(Nil, String)
Save a single recording immediately to its own file
Writes a single recording to an individual file.
The file name has human-readable parts (method, host and path) and a hash of the match key. The name does not depend on the response, so a request keeps its file name, and a new recording of the same key replaces the file.
Parameters
directory: The directory where the recording file will be writtenrec: The recording to savekey: The match key string for this request (should match the recorder’s key function)
Returns
Ok(Nil): Successfully saved the recordingError(String): Error message if directory creation or file write fails
Examples
Save one recording 
import dream_http_client/matching
import dream_http_client/recording
import dream_http_client/storage
pub fn save_one(
mocks_directory: String,
recorded: recording.Recording,
) -> String {
// Compute the key with the same key policy you use for playback
let key_fn =
matching.request_key(method: True, url: True, headers: False, body: False)
let key = key_fn(recorded.request)
case storage.save_recording_immediately(mocks_directory, recorded, key) {
Ok(_) -> "Saved recording"
Error(reason) -> "Failed to save: " <> reason
}
}
Notes
- Each recording is saved to its own file for O(1) write performance
- File names have human-readable parts (method, host, path) and a hash of the key
- A file with the same name is replaced
- Concurrent tests can record safely without file contention
pub fn save_recordings(
directory: String,
recordings: List(recording.Recording),
key_fn: fn(recording.RecordedRequest) -> String,
) -> Result(Nil, String)
Save multiple recordings to individual files
Writes each recording to its own file in the directory. Creates the directory if it doesn’t exist. Each file is named based on the request signature for easy identification.
Parameters
directory: The directory where recording files will be writtenrecordings: List of recordings to savekey_fn: Match key function used to compute each recording’s key string
Returns
Ok(Nil): Successfully saved all recordingsError(String): Error message if directory creation or file write fails
Examples
Save many recordings 
import dream_http_client/matching
import dream_http_client/recording
import dream_http_client/storage
pub fn save_all(
mocks_directory: String,
recordings: List(recording.Recording),
) -> String {
let key_fn =
matching.request_key(method: True, url: True, headers: False, body: False)
case storage.save_recordings(mocks_directory, recordings, key_fn) {
Ok(_) -> "Saved recordings successfully"
Error(reason) -> "Failed to save: " <> reason
}
}
Notes
- Directory is created automatically if it doesn’t exist
- Each recording is saved to its own file (no file contention)
- Existing files with the same name are overwritten, so of two recordings with the same key, the last one stays