openleadr_client/
event.rs1use std::sync::Arc;
2
3use crate::{
4 ClientKind, ClientRef, ReportClient,
5 error::{Error, Result},
6};
7use openleadr_wire::{Event, Report, event::EventRequest, report::ReportRequest};
8
9#[derive(Debug, Clone)]
29pub struct EventClient<K> {
30 client: Arc<ClientRef<K>>,
31 data: Event,
32}
33
34impl<K: ClientKind> EventClient<K> {
35 pub(super) fn from_event(client: Arc<ClientRef<K>>, event: Event) -> Self {
36 Self {
37 client,
38 data: event,
39 }
40 }
41
42 pub fn id(&self) -> &openleadr_wire::event::EventId {
44 &self.data.id
45 }
46
47 pub fn created_date_time(&self) -> chrono::DateTime<chrono::Utc> {
49 self.data.created_date_time
50 }
51
52 pub fn modification_date_time(&self) -> chrono::DateTime<chrono::Utc> {
54 self.data.modification_date_time
55 }
56
57 pub fn content(&self) -> &EventRequest {
59 &self.data.content
60 }
61
62 pub fn content_mut(&mut self) -> &mut EventRequest {
66 &mut self.data.content
67 }
68
69 pub async fn update(&mut self) -> Result<()> {
72 self.data = self
73 .client
74 .put(&format!("events/{}", self.id()), &self.data.content)
75 .await?;
76 Ok(())
77 }
78
79 pub async fn delete(self) -> Result<Event> {
81 self.client.delete(&format!("events/{}", self.id())).await
82 }
83
84 pub fn new_report(&self, client_name: String) -> ReportRequest {
86 ReportRequest {
87 event_id: self.id().clone(),
88 client_name,
89 report_name: None,
90 payload_descriptors: None,
91 resources: vec![],
92 }
93 }
94
95 pub async fn create_report(&self, report_data: ReportRequest) -> Result<ReportClient<K>> {
99 if &report_data.event_id != self.id() {
100 return Err(Error::InvalidParentObject);
101 }
102
103 let report = self.client.post("reports", &report_data).await?;
104 Ok(ReportClient::from_report(self.client.clone(), report))
105 }
106
107 async fn get_reports_req(
108 &self,
109 client_name: Option<&str>,
110 skip: usize,
111 limit: usize,
112 ) -> Result<Vec<ReportClient<K>>> {
113 let skip_str = skip.to_string();
114 let limit_str = limit.to_string();
115
116 let mut query = vec![
117 ("programID", self.content().program_id.as_str()),
118 ("eventID", self.id().as_str()),
119 ("skip", &skip_str),
120 ("limit", &limit_str),
121 ];
122
123 if let Some(client_name) = client_name {
124 query.push(("clientName", client_name));
125 }
126
127 let reports: Vec<Report> = self.client.get("reports", &query).await?;
128 Ok(reports
129 .into_iter()
130 .map(|report| ReportClient::from_report(self.client.clone(), report))
131 .collect())
132 }
133
134 pub async fn get_report_list(&self, client_name: Option<&str>) -> Result<Vec<ReportClient<K>>> {
136 self.client
137 .iterate_pages(|skip, limit| self.get_reports_req(client_name, skip, limit))
138 .await
139 }
140}