use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{client::Sendry, error::Error, Page};
#[derive(Debug, Clone)]
pub struct Events {
client: Sendry,
}
impl Events {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn ingest(&self, params: IngestEvent) -> Result<IngestedEvent, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/events", &[], Some(¶ms)),
)
.await
}
pub async fn list(&self, params: ListEvents) -> Result<Page<IngestedEvent>, Error> {
let q = params.to_query();
self.client
.request(self.client.build::<()>(Method::GET, "/v1/events", &q, None))
.await
}
pub async fn get(&self, id: &str) -> Result<IngestedEvent, Error> {
self.client
.request(self.client.build::<()>(
Method::GET,
&format!("/v1/events/{id}"),
&[],
None,
))
.await
}
}
#[derive(Debug, Clone, Serialize)]
pub struct IngestEvent {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub event_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub contact_email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub contact_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payload: Option<Value>,
}
#[derive(Debug, Clone, Default)]
pub struct ListEvents {
pub limit: Option<u32>,
pub cursor: Option<String>,
pub name: Option<String>,
}
impl ListEvents {
fn to_query(&self) -> Vec<(&'static str, String)> {
let mut q = Vec::new();
if let Some(v) = self.limit {
q.push(("limit", v.to_string()));
}
if let Some(v) = &self.cursor {
q.push(("cursor", v.clone()));
}
if let Some(v) = &self.name {
q.push(("name", v.clone()));
}
q
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct IngestedEvent {
pub id: String,
pub external_id: Option<String>,
pub name: String,
pub contact_email: Option<String>,
pub contact_id: Option<String>,
pub payload: Value,
pub received_at: String,
pub processed_at: Option<String>,
pub triggered_runs: u32,
#[serde(default)]
pub deduped: Option<bool>,
}