pub struct Pexels { /* private fields */ }
Expand description
Client for interacting with the Pexels API
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
}
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use pexels_api::SearchBuilder;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.search_photos(SearchBuilder::new().query("mountains").per_page(15).page(1)).await.expect("Failed to get photos");
println!("{:?}", response);
}
Implementations§
Source§impl Pexels
impl Pexels
Sourcepub fn new(api_key: String) -> Self
pub fn new(api_key: String) -> Self
Create a new Pexels client.
§Arguments
api_key
- The API key for the Pexels API.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
}
Sourcepub async fn search_photos(
&self,
builder: SearchBuilder<'_>,
) -> Result<PhotosResponse, PexelsError>
pub async fn search_photos( &self, builder: SearchBuilder<'_>, ) -> Result<PhotosResponse, PexelsError>
Retrieves a list of photos from the Pexels API based on the search criteria.
§Arguments
builder
- ASearchBuilder
instance with the search parameters.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use pexels_api::SearchBuilder;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.search_photos(SearchBuilder::new().query("mountains").per_page(15).page(1)).await.expect("Failed to get photos");
println!("{:?}", response);
}
Sourcepub async fn get_photo(&self, id: usize) -> Result<Photo, PexelsError>
pub async fn get_photo(&self, id: usize) -> Result<Photo, PexelsError>
Retrieves a photo by its ID from the Pexels API.
§Arguments
id
- The ID of the photo to retrieve.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.get_photo(10967).await.expect("Failed to get photo");
println!("{:?}", response);
}
Sourcepub async fn curated_photo(
&self,
builder: CuratedBuilder,
) -> Result<PhotosResponse, PexelsError>
pub async fn curated_photo( &self, builder: CuratedBuilder, ) -> Result<PhotosResponse, PexelsError>
Retrieves a random photo from the Pexels API.
§Arguments
builder
- ACuratedBuilder
instance with the search parameters.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use pexels_api::CuratedBuilder;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.curated_photo(CuratedBuilder::new().per_page(1).page(1)).await.expect("Failed to get random photo");
println!("{:?}", response);
}
Sourcepub async fn search_videos(
&self,
builder: VideoSearchBuilder<'_>,
) -> Result<VideoResponse, PexelsError>
pub async fn search_videos( &self, builder: VideoSearchBuilder<'_>, ) -> Result<VideoResponse, PexelsError>
Retrieves a list of videos from the Pexels API based on the search criteria.
§Arguments
builder
- AVideoSearchBuilder
instance with the search parameters.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use pexels_api::VideoSearchBuilder;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.search_videos(VideoSearchBuilder::new().query("nature").per_page(15).page(1)).await.expect("Failed to get videos");
println!("{:?}", response);
}
Sourcepub async fn popular_videos(
&self,
builder: PopularBuilder,
) -> Result<VideoResponse, PexelsError>
pub async fn popular_videos( &self, builder: PopularBuilder, ) -> Result<VideoResponse, PexelsError>
Retrieves a list of popular videos from the Pexels API.
§Arguments
builder
- APopularBuilder
instance with the search parameters.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use pexels_api::PopularBuilder;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.popular_videos(PopularBuilder::new().per_page(15).page(1)).await.expect("Failed to get popular videos");
println!("{:?}", response);
}
Sourcepub async fn get_video(&self, id: usize) -> Result<Video, PexelsError>
pub async fn get_video(&self, id: usize) -> Result<Video, PexelsError>
Retrieves a video by its ID from the Pexels API.
§Arguments
id
- The ID of the video to retrieve.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.get_video(25460961).await.expect("Failed to get video");
println!("{:?}", response);
}
Sourcepub async fn search_collections(
&self,
per_page: usize,
page: usize,
) -> Result<CollectionsResponse, PexelsError>
pub async fn search_collections( &self, per_page: usize, page: usize, ) -> Result<CollectionsResponse, PexelsError>
Retrieves a list of collections from the Pexels API.
§Arguments
per_page
- The number of collections to retrieve per page.page
- The page number to retrieve.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.search_collections(15, 1).await.expect("Failed to get collections");
println!("{:?}", response);
}
Sourcepub async fn featured_collections(
&self,
per_page: usize,
page: usize,
) -> Result<CollectionsResponse, PexelsError>
pub async fn featured_collections( &self, per_page: usize, page: usize, ) -> Result<CollectionsResponse, PexelsError>
Retrieves a list of featured collections from the Pexels API.
§Arguments
per_page
- The number of collections to retrieve per page.page
- The page number to retrieve.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let response = client.featured_collections(15, 1).await.expect("Failed to get collections");
println!("{:?}", response);
}
Sourcepub async fn search_media(
&self,
builder: MediaBuilder,
) -> Result<MediaResponse, PexelsError>
pub async fn search_media( &self, builder: MediaBuilder, ) -> Result<MediaResponse, PexelsError>
Retrieves all media (photos and videos) within a single collection.
§Arguments
builder
- AMediaBuilder
instance with the search parameters.
§Errors
Returns a PexelsError
if the request fails or the response cannot be parsed as JSON.
§Example
use dotenvy::dotenv;
use pexels_api::Pexels;
use pexels_api::MediaBuilder;
use std::env;
#[tokio::main]
async fn main() {
dotenv().ok();
let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
let client = Pexels::new(api_key);
let builder = MediaBuilder::new().id("tszhfva".to_string()).per_page(15).page(1);
let response = client.search_media(builder).await.expect("Failed to get media");
println!("{:?}", response);
}