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,
matching_config: matching.MatchingConfig,
) -> Result(Nil, String)
Save a single recording immediately to its own file
Writes a single recording to an individual file based on the request signature. The filename is generated from the request method, host, path, and a hash for uniqueness.
Parameters
directory: The directory where the recording file will be writtenrec: The recording to savematching_config: The matching configuration used to generate the filename signature
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)
let config = matching.match_url_only()
case storage.save_recording_immediately("mocks/api", rec, config) {
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),
matching_config: matching.MatchingConfig,
) -> 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 savematching_config: The matching configuration used to generate filenames
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 config = matching.match_url_only()
case storage.save_recordings("mocks/api", recordings, config) {
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