3个版本 (稳定)

1.2.0 2023年8月12日
1.0.0 2023年4月28日
0.1.0 2023年4月23日

234日期和时间

MIT 许可证

54KB
1K SLoC

Rust官方假日和事件API

Crates.io Build Status docs.rs Code Coverage Funding Status

Rust行业领先的假日和事件API。超过5,000个假日和数千个描述。被世界领先公司信任。自2011年以来由开发者为开发者构建。

身份验证

访问假日和事件API需要API密钥。您可以在这里免费获取一个,无需信用卡!请注意,免费计划有限。要访问更多数据和进行更多请求,需要付费计划。

安装

cargo add holiday_event_api

示例

use holiday_event_api::{
    model::{GetEventInfoRequest, GetEventsRequest, SearchRequest},
    HolidayEventApi,
};

#[tokio::main]
async fn main() {
    // Get a FREE API key from https://apilayer.com/marketplace/checkiday-api#pricing
    let client = HolidayEventApi::new("<your API key>");

    if client.is_err() {
        println!("{}", client.unwrap_err());
        return;
    }

    let client = client.unwrap();
    // Get Events for a given Date
    let events = client
        .get_events(GetEventsRequest {
            // These parameters are all optional. These are their defaults:
            date: Some("today".into()),
            adult: Some(false),
            timezone: Some("America/Chicago".into()),
        })
        .await;

    if events.is_err() {
        println!("{}", events.unwrap_err());
        return;
    }

    let events = events.unwrap();
    let event = events.events.get(0).unwrap();
    println!(
        "Today is {}! Find more information at: {}.",
        event.name, event.url
    );
    println!(
        "Rate limit remaining: {}/{} (month).",
        events.rate_limit.remaining_month, events.rate_limit.limit_month
    );

    // Get Event Information
    let event_info = client
        .get_event_info(GetEventInfoRequest {
            id: event.id.to_string(),
            // These parameters can be specified to calculate the range of event_info.event.occurrences
            start: None, // Some(2020),
            end: None,   // Some(2030),
        })
        .await;

    if event_info.is_err() {
        println!("{}", event_info.unwrap_err());
        return;
    }

    let event_info = event_info.unwrap();

    println!("The Event's hashtags are {:?}.", event_info.event.hashtags);

    // Search for Events
    let query = "pizza day";
    let search = client
        .search(SearchRequest {
            query: query.into(),
            // These parameters are the defaults but can be specified:
            adult: None, // Some(true),
        })
        .await;

    if search.is_err() {
        println!("{}", search.unwrap_err());
        return;
    }

    let search = search.unwrap();
    println!(
        "Found {} events, including {}, that match the query \"{}\".",
        search.events.len(),
        search.events.first().unwrap().name,
        query
    )
}

依赖项

~6–19MB
~287K SLoC