Struct FreesoundClient

Source
pub struct FreesoundClient { /* private fields */ }
Expand description

Client for interacting with the Freesound API

§Examples

let client = FreesoundClient::new(api_key.clone(), None);
assert_eq!(client.base_url(), DEFAULT_BASE_URL);

Implementations§

Source§

impl FreesoundClient

Source

pub fn new(api_key: String, base_url: Option<String>) -> Self

Creates a new Freesound API client

§Arguments
  • api_key - Your Freesound API key
  • base_url - Optional custom base URL for the API. If None, uses the default Freesound API URL.
§Examples
// With default URL
let client = FreesoundClient::new(api_key.clone(), None);
assert_eq!(client.base_url(), DEFAULT_BASE_URL);

// With custom URL
let client = FreesoundClient::new(api_key, Some("https://custom.api.url".to_string()));
assert_eq!(client.base_url(), "https://custom.api.url");
Source

pub fn api_key(&self) -> &str

Returns the API key used by the client

§Examples
let client = FreesoundClient::new(api_key.clone(), None);
assert_eq!(client.api_key(), api_key);
Source

pub fn base_url(&self) -> &str

Returns the base URL used by the client

§Examples
let client = FreesoundClient::new(api_key, None);
assert_eq!(client.base_url(), DEFAULT_BASE_URL);
Source

pub fn request(&self, method: Method, path: &str) -> RequestBuilder

Creates a new authenticated request to the Freesound API

This method adds the API key as a query parameter to requests

§Arguments
  • method - HTTP method to use
  • path - API endpoint path (without the base URL)
§Returns

A reqwest RequestBuilder with the API key included

§Examples
let request = client.request(Method::GET, "sounds/1234");
Source

pub async fn test_api_key(&self) -> Result<()>

Performs a test request to verify the API key is valid

§Returns

A Result indicating whether the API key is valid

§Examples
dotenvy::dotenv().ok();
let api_key = env::var("FREESOUND_API_KEY").expect("FREESOUND_API_KEY must be set");
let client = FreesoundClient::new(api_key, None);

// Test avec une clé valide
client.test_api_key().await?;
println!("API key is valid!");

// Test avec une clé invalide
let invalid_client = FreesoundClient::new("invalid_key".to_string(), None);
let result = invalid_client.test_api_key().await;
assert!(result.is_err());
println!("Invalid API key correctly detected!");
Source

pub async fn search(&self, query: &[(String, String)]) -> Result<SearchResponse>

Search for sounds using text query

§Examples
use std::env;
use freesound_rs::{FreesoundClient,SearchResponse, SortOption, SearchQueryBuilder};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv()?;
let api_key = env::var("FREESOUND_API_KEY").expect("FREESOUND_API_KEY must be set");
let client = FreesoundClient::new(api_key, None);
// Simple search
let query = SearchQueryBuilder::new()
    .query("piano")
    .build();
let results = client.search(&query).await?;
println!("Found {} sounds", results.count);

// Advanced search
let query = SearchQueryBuilder::new()
    .query("music")
    .filter("tag:guitar")
    .sort(SortOption::RatingDesc)
    .page(1)
    .page_size(15)
    .fields(["id", "name", "tags"])
    .build();
let results = client.search(&query).await?;
 Ok(())
 }
Source

pub async fn get_sound( &self, sound_id: i32, descriptors: Option<&[&str]>, normalized: Option<bool>, ) -> Result<Sound>

Get detailed information about a specific sound

§Arguments
  • sound_id - The unique identifier of the sound
  • descriptors - Optional list of descriptors to include in the response
  • normalized - Whether to normalize the descriptor values (only applies if descriptors are requested)
§Examples
use std::env;
use freesound_rs::FreesoundClient;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    dotenvy::dotenv().ok();
    let api_key = env::var("FREESOUND_API_KEY").expect("FREESOUND_API_KEY must be set");
    let client = FreesoundClient::new(api_key, None);
// Get basic sound information
let sound = client.get_sound(1234, None, None).await?;
println!("Sound name: {}", sound.name);

// Get sound with specific descriptors
let sound = client
    .get_sound(
        1234,
        Some(&["lowlevel.mfcc", "rhythm.bpm"]),
        Some(true)
    )
    .await?;
     Ok(())
 }

Trait Implementations§

Source§

impl Clone for FreesoundClient

Source§

fn clone(&self) -> FreesoundClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FreesoundClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,