use reqwest::Method;
use crate::client::{Client, object_or_null, segment};
use crate::error::Result;
use crate::models::TrackedEvent;
use crate::paging::{ItemStream, ListCall, Page, Paging, filter, paging_setters};
#[derive(Debug, Clone, Copy)]
pub struct Events<'a> {
client: &'a Client,
}
impl<'a> Events<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Events { client }
}
pub async fn list(&self, params: ListEvents) -> Result<Page<TrackedEvent>> {
params.call().page(self.client).await
}
pub fn list_all(&self, params: ListEvents) -> ItemStream<TrackedEvent> {
params.call().walk(self.client)
}
pub async fn get(&self, event_id: &str) -> Result<TrackedEvent> {
let path = format!("/events/{}", segment(event_id, "An event id")?);
let payload = self.client.request(Method::GET, &path, &[], None).await?;
Ok(TrackedEvent::from_value(object_or_null(payload)))
}
}
#[derive(Debug, Clone, Default)]
pub struct ListEvents {
identity: Option<String>,
group: Option<String>,
group_type: Option<String>,
device: Option<String>,
session: Option<String>,
term: Option<String>,
paging: Paging,
}
impl ListEvents {
pub fn new() -> Self {
ListEvents::default()
}
pub fn identity(mut self, identity_id: impl Into<String>) -> Self {
self.identity = Some(identity_id.into());
self
}
pub fn group(mut self, group_id: impl Into<String>) -> Self {
self.group = Some(group_id.into());
self
}
pub fn group_type(mut self, group_type: impl Into<String>) -> Self {
self.group_type = Some(group_type.into());
self
}
pub fn device(mut self, fingerprint: impl Into<String>) -> Self {
self.device = Some(fingerprint.into());
self
}
pub fn session(mut self, session: impl Into<String>) -> Self {
self.session = Some(session.into());
self
}
pub fn term(mut self, term: impl Into<String>) -> Self {
self.term = Some(term.into());
self
}
paging_setters!();
fn call(self) -> ListCall<TrackedEvent> {
let mut filters = Vec::new();
filter(&mut filters, "identity", self.identity);
filter(&mut filters, "group", self.group);
filter(&mut filters, "groupType", self.group_type);
filter(&mut filters, "device", self.device);
filter(&mut filters, "session", self.session);
filter(&mut filters, "term", self.term);
ListCall {
path: "/events",
filters,
paging: self.paging,
parse: TrackedEvent::from_value,
}
}
}