fluidattacks-tracks 0.1.0

Rust library for usage analytics
Documentation
//! Tracks client.

use reqwest::blocking::Client;

use crate::resources::event::EventResource;

const DEFAULT_API_URL: &str = "https://tracks.fluidattacks.com/";
const RETRY_ATTEMPTS: u32 = 5;

/// Tracks SDK interface.
pub struct Tracks {
    pub event: EventResource,
}

impl Tracks {
    /// Create a new Tracks client.
    ///
    /// The API URL is read from the `FT_API_URL` environment variable,
    /// defaulting to `https://tracks.fluidattacks.com/`.
    pub fn new() -> Self {
        let base_url = std::env::var("FT_API_URL").unwrap_or_else(|_| DEFAULT_API_URL.to_owned());
        let http = Client::new();
        Self {
            event: EventResource::new(base_url, http, RETRY_ATTEMPTS),
        }
    }
}

impl Default for Tracks {
    fn default() -> Self {
        Self::new()
    }
}

impl Tracks {
    /// Block until all in-flight events have been sent or exhausted their retries.
    ///
    /// Call this before process exit to avoid dropping events that are still
    /// being delivered in background threads.
    pub fn flush(&self) {
        self.event.flush();
    }
}