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 for inspection
case storage.load_recordings("mocks/api") {
Ok(recordings) -> {
io.println("Loaded " <> int.to_string(list.length(recordings)) <> " recordings")
}
Error(reason) -> io.println_error("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 filename includes human-readable parts (method/host/path) plus a short hash of the match key and a short hash of the file content. This avoids overwriting when multiple recordings share the same key.
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
let rec = recording.Recording(request: req, response: resp)
// Compute a key string using 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(rec.request)
case storage.save_recording_immediately("mocks/api", rec, key) {
Ok(_) -> io.println("Saved recording")
Error(reason) -> io.println_error("Failed to save: " <> reason)
}
Notes
- Each recording is saved to its own file for O(1) write performance
- Filenames include human-readable parts (method, host, path) plus a hash for uniqueness
- 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
let recordings = [
create_test_recording(),
create_another_recording(),
]
let key_fn = matching.request_key(method: True, url: True, headers: False, body: False)
case storage.save_recordings("mocks/api", recordings, key_fn) {
Ok(_) -> io.println("Saved recordings successfully")
Error(reason) -> io.println_error("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