Pexels

Struct Pexels 

Source
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", &params).await.expect("Failed to get photos");
    println!("{:?}", response);
}

Implementations§

Source§

impl Pexels

Source

pub fn new(api_key: String) -> Self

创建新的 Pexels 客户端。

§参数
  • api_key - Pexels API 的 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);
}
Source

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", &params).await.expect("Failed to get photos");
    println!("{:?}", response);
}
Source

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);
}
Source

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);
}
Source

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 - A VideoSearchBuilder 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_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);
}
Source

pub async fn popular_videos( &self, builder: PopularBuilder, ) -> Result<VideoResponse, PexelsError>

Retrieves a list of popular videos from the Pexels API.

§Arguments
  • builder - A PopularBuilder 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_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);
}
Source

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);
}
Source

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);
}      
Source

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);
}
Source

pub async fn search_media( &self, builder: MediaBuilder, ) -> Result<MediaResponse, PexelsError>

Retrieves all media (photos and videos) within a single collection.

§Arguments
  • builder - A MediaBuilder 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_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);
}                 

Auto Trait Implementations§

§

impl Freeze for Pexels

§

impl !RefUnwindSafe for Pexels

§

impl Send for Pexels

§

impl Sync for Pexels

§

impl Unpin for Pexels

§

impl !UnwindSafe for Pexels

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> 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, 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