Skip to main content

FetchAndSave

Trait FetchAndSave 

Source
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§

Source

type Item: Serialize + DeserializeOwned + Send + Sync + 'static

The item type produced by this builder.

Required Methods§

Source

fn resource_label() -> &'static str

A human-readable label used in log messages (e.g. "top tracks").

Provided Methods§

Source

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.

Source

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 in
  • filename_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", so this trait is not object safe.

Implementors§