Crate pexels_sdk

Crate pexels_sdk 

Source
Expand description

pexels-sdk crate 提供了 Pexels API 的封装库。基于 Pexels API 文档

要获取 API 密钥,您需要从 申请 API 访问 - Pexels 申请。

本库依赖 serde-json crate 来处理结果。因此,您需要阅读 serde_json - Rust 的文档,特别是 serde_json::Value - Rust

§配置

Cargo.toml 文件中的 [dependencies] 部分添加以下行:

pexels-sdk = "*"

并在您的 crate 根文件(如 main.rs)中添加:

use pexels_sdk;

完成!现在您可以使用此 API 封装库。

§示例

此示例展示了如何获取山脉照片列表。

use dotenvy::dotenv;
use std::env;
use pexels_sdk::{PexelsClient, SearchParams};

#[tokio::main]
async fn main() {
    dotenv().ok();
    let api_key = env::var("PEXELS_API_KEY").expect("PEXELS_API_KEY not set");
    let pexels_client = PexelsClient::new(api_key);
    let params = SearchParams::new()
        .page(1)
        .per_page(15);
    let response = pexels_client.search_photos("mountains", &params).await.expect("Failed to get photos");
    println!("{:?}", response);
}

您可以使用 cargo run 来运行它!就是如此简单。

§随机照片

如果您想获取随机照片,可以使用 curated_photos 函数并将 per_page 设置为 1,page 设置为 1 到 1000 之间的随机数,以获取漂亮的随机照片。如果您想要获取特定主题的随机照片,也可以对热门搜索使用相同的方法。

§图片格式

  • original - 原始图片的大小由宽度和高度属性给出。
  • large - 此图片最大宽度为 940 像素,最大高度为 650 像素。它保持原始图片的宽高比。
  • large2x - 此图片最大宽度为 1880 像素,最大高度为 1300 像素。它保持原始图片的宽高比。
  • medium - 此图片高度为 350 像素,宽度灵活。它保持原始图片的宽高比。
  • small - 此图片高度为 130 像素,宽度灵活。它保持原始图片的宽高比。
  • portrait - 此图片宽度为 800 像素,高度为 1200 像素。
  • landscape - 此图片宽度为 1200 像素,高度为 627 像素。
  • tiny - 此图片宽度为 280 像素,高度为 200 像素。

Structs§

Collection
domain 模块 Represents a Pexels collection.
Collections
Represents a request to fetch a list of collections from the Pexels API.
CollectionsBuilder
Builder for constructing a Collections request.
CollectionsResponse
Represents the response for a list of collections.
Curated
photos 模块 This endpoint enables you to receive real-time photos curated by the Pexels team.
CuratedBuilder
Builder for Curated.
DownloadManager
Featured
collections 模块 Represents a request to fetch all featured collections from the Pexels API.
FeaturedBuilder
Builder for constructing a Featured request.
FetchPhoto
Retrieve a specific Photo from its id.
FetchPhotoBuilder
Builder for FetchPhoto.
FetchVideo
Represents a request to fetch a specific video by its ID from the Pexels API.
FetchVideoBuilder
Builder for FetchVideo.
Hex
Represents a hexadecimal color code. Used as an input value for Color::Hex when specifying a hexadecimal color code.
Media
Represents a request to fetch a specific media item by its ID from the Pexels API. This endpoint returns all media items (photos and videos) within a single collection. Use the type parameter to filter results to only photos or only videos.
MediaBuilder
Builder for constructing a Media request.
MediaPhoto
Represents a photo media object.
MediaResponse
Represents the response for a list of media items.
MediaVideo
Represents a video media object.
Pexels
用于与 Pexels API 交互的客户端
PexelsClient
Pexels API 的主要客户端
Photo
Represents a Pexels photo.
PhotoSrc
Represents different image sizes for a photo.
PhotosResponse
Represents the response for a list of photos.
Popular
videos 模块 Represents a request for popular videos from the Pexels API.
PopularBuilder
Builder for Popular.
Search
Represents a search query to the Pexels API.
SearchBuilder
Builder for Search.
SearchParams
User
Represents a user who created a media item.
Video
Represents a Pexels video.
VideoFile
Represents a video file with different qualities.
VideoPicture
Represents a preview picture of a video.
VideoResponse
Represents the response for a list of videos.
VideoSearch
Represents a search request to the Pexels API for videos.
VideoSearchBuilder
Builder for Search.

Enums§

Color
Represents the desired photo color.
Locale
指定搜索查询的语言环境。 支持的值:en-USpt-BRes-ESca-ESde-DEit-ITfr-FRsv-SEid-IDpl-PLja-JPzh-TWzh-CNko-KRth-THnl-NLhu-HUvi-VNcs-CZda-DKfi-FIuk-UAel-GRro-ROnb-NOsk-SKtr-TRru-RU。 默认值:en-US
MediaSort
指定媒体集合中的项目顺序。 支持的值:ascdesc。默认值:asc
MediaType
指定要请求的媒体类型。 如果未提供或无效,将返回所有媒体类型。 支持的值:photosvideos
MediaTypeResponse
Enum representing the type of media. Supported values are photos and videos.
Orientation
期望的照片方向。 支持的值:landscapeportraitsquare。 默认值:landscape
PexelsError
与 Pexels API 交互时可能发生的错误。 此枚举作为与 API 交互的函数的返回类型。
Size
指定视频或照片的最小尺寸。 支持的值:largemediumsmall

Type Aliases§

ProgressCallback
The type of progress callback function