acosmi/notifications/
notifications.rs1use super::types::{
5 DeviceRegistration, NotificationList, NotificationPreference, NotificationUnreadCount,
6};
7use crate::billing::entitlements::urlencoding;
8use crate::core::client::Client;
9use crate::core::http::DEFAULT_JSON_TIMEOUT_MS;
10use crate::shared::{ApiResponse, Error, Result};
11use tokio_util::sync::CancellationToken;
12
13impl Client {
14 pub async fn list_notifications(
16 &self,
17 page: i64,
18 page_size: i64,
19 type_filter: &str,
20 signal: Option<CancellationToken>,
21 ) -> Result<NotificationList> {
22 let mut path = format!("/notifications?page={page}&pageSize={page_size}");
23 if !type_filter.is_empty() {
24 path.push_str(&format!("&type={}", urlencoding(type_filter)));
25 }
26 self.billing_get(&path, signal).await
27 }
28
29 pub async fn get_unread_count(&self, signal: Option<CancellationToken>) -> Result<i64> {
31 let resp: NotificationUnreadCount = self
32 .billing_get("/notifications/unread-count", signal)
33 .await?;
34 Ok(resp.unread_count)
35 }
36
37 pub async fn mark_notification_read(
39 &self,
40 id: &str,
41 signal: Option<CancellationToken>,
42 ) -> Result<()> {
43 self.notify_void(
44 http::Method::PUT,
45 &format!("/notifications/{}/read", urlencoding(id)),
46 None,
47 signal,
48 )
49 .await
50 }
51
52 pub async fn mark_all_notifications_read(
54 &self,
55 signal: Option<CancellationToken>,
56 ) -> Result<()> {
57 self.notify_void(http::Method::PUT, "/notifications/read-all", None, signal)
58 .await
59 }
60
61 pub async fn delete_notification(
63 &self,
64 id: &str,
65 signal: Option<CancellationToken>,
66 ) -> Result<()> {
67 self.notify_void(
68 http::Method::DELETE,
69 &format!("/notifications/{}", urlencoding(id)),
70 None,
71 signal,
72 )
73 .await
74 }
75
76 pub async fn register_device(
81 &self,
82 reg: &DeviceRegistration,
83 signal: Option<CancellationToken>,
84 ) -> Result<()> {
85 let body = serde_json::to_string(reg)
86 .map_err(|e| Error::other(format!("serialize device registration: {e}")))?;
87 self.notify_void(http::Method::POST, "/devices/register", Some(&body), signal)
88 .await
89 }
90
91 pub async fn unregister_device(
95 &self,
96 token: &str,
97 signal: Option<CancellationToken>,
98 ) -> Result<()> {
99 self.notify_void(
100 http::Method::DELETE,
101 &format!("/devices/{}", urlencoding(token)),
102 None,
103 signal,
104 )
105 .await
106 }
107
108 pub async fn list_notification_preferences(
112 &self,
113 signal: Option<CancellationToken>,
114 ) -> Result<Vec<NotificationPreference>> {
115 self.billing_get("/notification-preferences", signal).await
116 }
117
118 pub async fn update_notification_preference(
122 &self,
123 type_code: &str,
124 pref: &NotificationPreference,
125 signal: Option<CancellationToken>,
126 ) -> Result<()> {
127 let body = serde_json::to_string(pref)
128 .map_err(|e| Error::other(format!("serialize preference: {e}")))?;
129 self.notify_void(
130 http::Method::PUT,
131 &format!("/notification-preferences/{}", urlencoding(type_code)),
132 Some(&body),
133 signal,
134 )
135 .await
136 }
137
138 async fn notify_void(
141 &self,
142 method: http::Method,
143 path: &str,
144 body: Option<&str>,
145 signal: Option<CancellationToken>,
146 ) -> Result<()> {
147 let (bytes, _) = self
148 .do_json_full_raw(method, path, body, signal, DEFAULT_JSON_TIMEOUT_MS, None)
149 .await?;
150 if bytes.is_empty() {
152 return Ok(());
153 }
154 let env: ApiResponse<serde_json::Value> = serde_json::from_slice(&bytes)
155 .map_err(|e| Error::other(format!("{path}: decode: {e}")))?;
156 if let Some(err) = env.business_error() {
157 return Err(err);
158 }
159 Ok(())
160 }
161}