emailit 2.0.3

The official Rust SDK for the Emailit Email API
Documentation
//! Event log retrieval service.

use std::sync::Arc;

use crate::client::BaseClient;
use crate::collection::Collection;
use crate::error::Error;
use crate::types::{Event, ListEventsParams};

/// Service for retrieving events from the Emailit event log.
///
/// Accessed via [`Emailit::events`](crate::Emailit::events).
pub struct EventService {
    pub(crate) client: Arc<BaseClient>,
}

impl EventService {
    /// Lists events with optional pagination and type filter.
    ///
    /// `GET /v2/events`
    pub async fn list(&self, params: Option<ListEventsParams>) -> Result<Collection<Event>, Error> {
        let query = params.map(|p| {
            let mut q = Vec::new();
            if let Some(page) = p.page {
                q.push(("page", page.to_string()));
            }
            if let Some(limit) = p.limit {
                q.push(("limit", limit.to_string()));
            }
            if let Some(event_type) = p.event_type {
                q.push(("type", event_type));
            }
            q
        });
        self.client
            .request::<Collection<Event>>("GET", "/v2/events", None, query.as_deref())
            .await
    }

    /// Retrieves an event by ID.
    ///
    /// `GET /v2/events/:id`
    pub async fn get(&self, id: &str) -> Result<Event, Error> {
        let path = format!("/v2/events/{}", urlencoding::encode(id));
        self.client.request("GET", &path, None, None).await
    }
}