Struct Pexels

Source
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

Source

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

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

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

pub async fn curated_photo( &self, builder: CuratedBuilder, ) -> Result<PhotosResponse, PexelsError>

Retrieves a random photo from the Pexels API.

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

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
Source§

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