#alert #api #notifications #api-client #api-bindings

apialerts

A Rust client for the API Alerts service

2 releases (1 stable)

1.0.0 Aug 30, 2024
0.1.0 Jul 4, 2024

#799 in Web programming

MIT license

10KB
179 lines

API Alerts - Rust

This is a simple API for sending alerts to the API Alerts service.

Installation

Add this to your Cargo.toml:

[dependencies]
apialerts = "1.0.0"

Usage

use apialerts::{event::ApiAlertsEvent, ApiAlertsClient};

#[tokio::main]
async fn main() {
    let client = ApiAlertsClient::new("{API-KEY}".to_string());

    let event = ApiAlertsEvent::new(
        "rust channel".to_string(),
        "I've caught the rust bug".to_string(),
    );

    // Blocking - using futures
    match client.send(event) {
        Ok(_) => {}
        Err(e) => println!("Error sending alert: {:?}", e),
    }

    // OR

    // Async - using async-std
    match client.send_async(event).await {
        Ok(_) => {}
        Err(e) => println!("Error sending alert: {:?}", e),
    }
}

You can also use the update_config and update_api_key methods to update the configuration of the client.

ApiAlertsConfig is exposed as a struct, so you can easily create a new config with the default values.

use apialerts::{event::ApiAlertsEvent, ApiAlertsClient};

#[tokio::main]
async fn main() {
    let client = ApiAlertsClient::new("{API-KEY}".to_string())
        .update_config(ApiAlertsConfig::new_default_config())
        .update_api_key("{API-KEY}".to_string());

    //...
}

You can also set the tags and links on the alert event.

use apialerts::{event::ApiAlertsEvent, ApiAlertsClient};

#[tokio::main]
async fn main() {
    //...

    let event = ApiAlertsEvent::new(
        "rust channel".to_string(),
        "I've caught the rust bug".to_string(),
    )
    .set_tags(vec!["rust", "bug".to_string()])
    .set_links(vec!["https://github.com/apialerts/apialerts-rust".to_string()]);

    //...
}

Dependencies

~8–25MB
~346K SLoC