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", ¶ms).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.
- Collections
Builder - Builder for constructing a
Collectionsrequest. - Collections
Response - Represents the response for a list of collections.
- Curated
- photos 模块 This endpoint enables you to receive real-time photos curated by the Pexels team.
- Curated
Builder - Builder for
Curated. - Download
Manager - Featured
- collections 模块 Represents a request to fetch all featured collections from the Pexels API.
- Featured
Builder - Builder for constructing a
Featuredrequest. - Fetch
Photo - Retrieve a specific Photo from its id.
- Fetch
Photo Builder - Builder for
FetchPhoto. - Fetch
Video - Represents a request to fetch a specific video by its ID from the Pexels API.
- Fetch
Video Builder - Builder for
FetchVideo. - Hex
- Represents a hexadecimal color code.
Used as an input value for
Color::Hexwhen 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
typeparameter to filter results to only photos or only videos. - Media
Builder - Builder for constructing a
Mediarequest. - Media
Photo - Represents a photo media object.
- Media
Response - Represents the response for a list of media items.
- Media
Video - Represents a video media object.
- Pexels
- 用于与 Pexels API 交互的客户端
- Pexels
Client - Pexels API 的主要客户端
- Photo
- Represents a Pexels photo.
- Photo
Src - Represents different image sizes for a photo.
- Photos
Response - Represents the response for a list of photos.
- Popular
- videos 模块 Represents a request for popular videos from the Pexels API.
- Popular
Builder - Builder for
Popular. - Search
- Represents a search query to the Pexels API.
- Search
Builder - Builder for
Search. - Search
Params - User
- Represents a user who created a media item.
- Video
- Represents a Pexels video.
- Video
File - Represents a video file with different qualities.
- Video
Picture - Represents a preview picture of a video.
- Video
Response - Represents the response for a list of videos.
- Video
Search - Represents a search request to the Pexels API for videos.
- Video
Search Builder - Builder for
Search.
Enums§
- Color
- Represents the desired photo color.
- Locale
- 指定搜索查询的语言环境。
支持的值:
en-US、pt-BR、es-ES、ca-ES、de-DE、it-IT、fr-FR、sv-SE、id-ID、pl-PL、ja-JP、zh-TW、zh-CN、ko-KR、th-TH、nl-NL、hu-HU、vi-VN、cs-CZ、da-DK、fi-FI、uk-UA、el-GR、ro-RO、nb-NO、sk-SK、tr-TR、ru-RU。 默认值:en-US。 - Media
Sort - 指定媒体集合中的项目顺序。
支持的值:
asc、desc。默认值:asc。 - Media
Type - 指定要请求的媒体类型。
如果未提供或无效,将返回所有媒体类型。
支持的值:
photos、videos。 - Media
Type Response - Enum representing the type of media.
Supported values are
photosandvideos. - Orientation
- 期望的照片方向。
支持的值:
landscape、portrait、square。 默认值:landscape。 - Pexels
Error - 与 Pexels API 交互时可能发生的错误。 此枚举作为与 API 交互的函数的返回类型。
- Size
- 指定视频或照片的最小尺寸。
支持的值:
large、medium、small。
Type Aliases§
- Progress
Callback - The type of progress callback function