Skip to main content

fluidattacks_tracks/
client.rs

1//! Tracks client.
2
3use reqwest::blocking::Client;
4
5use crate::resources::event::EventResource;
6
7const DEFAULT_API_URL: &str = "https://tracks.fluidattacks.com/";
8const RETRY_ATTEMPTS: u32 = 5;
9
10/// Tracks SDK interface.
11pub struct Tracks {
12    pub event: EventResource,
13}
14
15impl Tracks {
16    /// Create a new Tracks client.
17    ///
18    /// The API URL is read from the `FT_API_URL` environment variable,
19    /// defaulting to `https://tracks.fluidattacks.com/`.
20    pub fn new() -> Self {
21        let base_url = std::env::var("FT_API_URL").unwrap_or_else(|_| DEFAULT_API_URL.to_string());
22        let http = Client::new();
23        Tracks {
24            event: EventResource::new(base_url, http, RETRY_ATTEMPTS),
25        }
26    }
27}
28
29impl Default for Tracks {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl Tracks {
36    /// Block until all in-flight events have been sent or exhausted their retries.
37    ///
38    /// Call this before process exit to avoid dropping events that are still
39    /// being delivered in background threads.
40    pub fn flush(&self) {
41        self.event.flush();
42    }
43}