7个版本

0.1.6 2022年12月28日
0.1.5 2022年12月24日
0.1.3 2022年4月18日
0.1.2 2022年1月28日

#239 in HTTP客户端

每月29次下载

MIT许可证

64KB
1.5K SLoC

阿莱瑟亚

阿莱瑟亚是真理或揭露(...)“阿莱瑟亚”的直译意为“未被隐藏的状态;显而易见的状态”。

阿莱瑟亚是Guardian内容API的Rust编写HTTP客户端库。

如何使用

Aletheia需要一个依赖项Tokio来执行异步代码。
只需将aletheiatokio添加到你的Cargo.toml文件中的依赖项列表中。

[dependencies]
aletheia = "0.1.6"
tokio = { version = "1", features = ["full"] }

您还需要一个API密钥才能进行请求。密钥可以通过这里请求。

示例

假设您想找到2022年1月至12月间发表的包含“政治”一词且评分为5星的电影、戏剧或专辑评论。代码将类似于以下示例,并包含以下三个步骤

  1. 构造HTTP客户端
  2. 构建查询
  3. 解析响应 *
use aletheia::enums::*;
use aletheia::{GuardianContentClient, Result};

#[tokio::main]
async fn main() -> Result<()> {
    // The client is constructed by passing your API key
    // as the only parameter
    let mut client = GuardianContentClient::new("your-api-key");

    // Query parameters are built incrementally
    let response = client
        .search("politics")
        .date_from(2022, 1, 1)
        .date_to(2022, 12, 31)
        .star_rating(5)
        .page_size(5)
        .show_fields(vec![Field::Byline])
        .order_by(OrderBy::Newest)
        .send()
        .await?;

    // Parsing the response.
    // The response objects are deserialized, for the most part,
    // into Option values that need to be handled safely with
    // `let else` or `if let`.
    if let Some(results) = response.results {
        for result in results {
            let Some(pub_date) = result.web_publication_date else { continue };
            let Some(fields) = result.fields else { continue };
            let Some(byline) = fields.byline else { continue };

            println!(
                "[{}] {} ({})\n{}\n",
                pub_date.format("%Y-%m-%d"),
                result.web_title.trim(),
                byline,
                result.web_url,
            )
        }
    }

    Ok(())
}

以上将返回以下结果。

[2022-12-15] Children of the Taliban review – this beautiful documentary is an absolute must-watch (Rebecca Nicholson)
https://www.theguardian.com/tv-and-radio/2022/dec/15/children-of-the-taliban-review-this-beautiful-documentary-is-an-absolute-must-watch

[2022-10-25] The White Lotus season two review – this immaculate show’s writing is utterly unrivalled (Lucy Mangan)
https://www.theguardian.com/tv-and-radio/2022/oct/25/the-white-lotus-season-two-review-this-immaculate-seriess-writing-is-utterly-unrivalled

[2022-10-09] The Doctor review – a repeat prescription for acute intellectual stimulation (Arifa Akbar)
https://www.theguardian.com/stage/2022/oct/10/the-doctor-review-duke-of-yorks-theatre-robert-icke-juliet-stevenson

[2022-09-27] Make Me Prime Minister review – absolute, exquisite agony (Lucy Mangan)
https://www.theguardian.com/tv-and-radio/2022/sep/27/make-me-prime-minister-review-absolute-exquisite-agony

[2022-09-02] Bones and All review – cannibal romance is a heartbreaking banquet of brilliance (Peter Bradshaw)
https://www.theguardian.com/film/2022/sep/02/bones-and-all-review-luca-guadagnino-timothee-chalamet-venice-film-festival

调试

[*] 您可以使用格式说明符 #?

println!("{response:#?}");

或使用 dbg! 宏来格式化打印整个输出响应。

dbg!(response);

依赖项

~8–19MB
~298K SLoC