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
impl FreesoundClient
Sourcepub fn new(api_key: String, base_url: Option<String>) -> Self
pub fn new(api_key: String, base_url: Option<String>) -> Self
Creates a new Freesound API client
§Arguments
api_key
- Your Freesound API keybase_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");
Sourcepub fn api_key(&self) -> &str
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);
Sourcepub fn base_url(&self) -> &str
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);
Sourcepub fn request(&self, method: Method, path: &str) -> RequestBuilder
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 usepath
- 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");
Sourcepub async fn test_api_key(&self) -> Result<()>
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!");
Sourcepub async fn search(&self, query: &[(String, String)]) -> Result<SearchResponse>
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(())
}
Sourcepub async fn get_sound(
&self,
sound_id: i32,
descriptors: Option<&[&str]>,
normalized: Option<bool>,
) -> Result<Sound>
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 sounddescriptors
- Optional list of descriptors to include in the responsenormalized
- 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
impl Clone for FreesoundClient
Source§fn clone(&self) -> FreesoundClient
fn clone(&self) -> FreesoundClient
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for FreesoundClient
impl !RefUnwindSafe for FreesoundClient
impl Send for FreesoundClient
impl Sync for FreesoundClient
impl Unpin for FreesoundClient
impl !UnwindSafe for FreesoundClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more