pub trait FetchAndSave: Sized {
type Item: Serialize + DeserializeOwned + Send + Sync + 'static;
// Required method
fn resource_label() -> &'static str;
// Provided methods
fn latest_timestamp(_items: &[Self::Item]) -> Option<u32> { ... }
async fn fetch_and_save(
self,
format: FileFormat,
filename_prefix: &str,
) -> Result<String> { ... }
}Expand description
Extension trait providing fetch_and_save and fetch_and_save_sqlite for request builders.
Import this trait to save fetched data directly to a file or SQLite database.
§Example
ⓘ
use lastfm_client::{LastFmClient, FetchAndSave};
let path = client.top_tracks("username")
.fetch_and_save(FileFormat::Json, "top_tracks")
.await?;Required Associated Types§
Required Methods§
Sourcefn resource_label() -> &'static str
fn resource_label() -> &'static str
A human-readable label used in log messages (e.g. "top tracks").
Provided Methods§
Sourcefn latest_timestamp(_items: &[Self::Item]) -> Option<u32>
fn latest_timestamp(_items: &[Self::Item]) -> Option<u32>
Return the most recent timestamp from the given items, used to write a sidecar file
after saving. Return None (the default) if the item type has no timestamp.
Sourceasync fn fetch_and_save(
self,
format: FileFormat,
filename_prefix: &str,
) -> Result<String>
async fn fetch_and_save( self, format: FileFormat, filename_prefix: &str, ) -> Result<String>
Fetch items and save them to a file.
§Arguments
format- The file format to save the items infilename_prefix- Prefix for the generated filename
§Errors
Returns an error if the HTTP request fails, the response cannot be parsed, or the file cannot be saved.
§Returns
Result<String>- The filename of the saved file
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".