Skip to content

westy92/holiday-event-api-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Official Holiday and Event API for Rust

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

Industry-leading Holiday and Event API for Rust. Over 5,000 holidays and thousands of descriptions. Trusted by the World’s leading companies. Built by developers for developers since 2011.

Authentication

Access to the Holiday and Event API requires an API Key. You can get for one for FREE here, no credit card required! Note that free plans are limited. To access more data and have more requests, a paid plan is required.

Installation

cargo add holiday_event_api

Example

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