pub struct Pexels { /* private fields */ }Expand description
用于与 Pexels API 交互的客户端
§示例
use dotenvy::dotenv;
use pexels_sdk::PexelsClient;
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 = PexelsClient::new(api_key);
}§错误
如果请求失败或响应无法解析为 JSON,则返回 PexelsError。
§示例
use dotenvy::dotenv;
use pexels_sdk::PexelsClient;
use pexels_sdk::SearchParams;
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 = PexelsClient::new(api_key);
let params = SearchParams::new().page(1).per_page(15);
let response = client.search_photos("mountains", ¶ms).await.expect("Failed to get photos");
println!("{:?}", response);
}Implementations§
Source§impl Pexels
impl Pexels
Sourcepub async fn search_photos(
&self,
builder: SearchBuilder<'_>,
) -> Result<PhotosResponse, PexelsError>
pub async fn search_photos( &self, builder: SearchBuilder<'_>, ) -> Result<PhotosResponse, PexelsError>
根据搜索条件从 Pexels API 检索照片列表。
§参数
builder- 带有搜索参数的SearchBuilder实例。
§错误
如果请求失败或响应无法解析为 JSON,则返回 PexelsError。
§示例
use dotenvy::dotenv;
use pexels_sdk::PexelsClient;
use pexels_sdk::SearchParams;
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 = PexelsClient::new(api_key);
let params = SearchParams::new().page(1).per_page(15);
let response = client.search_photos("mountains", ¶ms).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>
根据 ID 从 Pexels API 检索照片。
§参数
id- 要检索的照片的 ID。
§错误
如果请求失败或响应无法解析为 JSON,则返回 PexelsError。
§示例
use dotenvy::dotenv;
use pexels_sdk::PexelsClient;
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 = PexelsClient::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>
从 Pexels API 检索随机照片。
§参数
builder- 带有搜索参数的CuratedBuilder实例。
§错误
如果请求失败或响应无法解析为 JSON,则返回 PexelsError。
§Example
use dotenvy::dotenv;
use pexels_sdk::Pexels;
use pexels_sdk::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- AVideoSearchBuilderinstance 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_sdk::Pexels;
use pexels_sdk::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- APopularBuilderinstance 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_sdk::Pexels;
use pexels_sdk::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_sdk::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_sdk::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_sdk::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- AMediaBuilderinstance 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_sdk::Pexels;
use pexels_sdk::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("your_collection_id".to_string()).per_page(15).page(1);
let response = client.search_media(builder).await.expect("Failed to get media");
println!("Found {} media items", response.total_results);
}