1 use crate::{
2 request::request_empty,
3 url_builder::{build_mail_app_url, build_mail_apps_url},
4 };
5 use reqwest::Client;
6 use serde::Serialize;
7 use smbcloud_model::{
8 error_codes::ErrorResponse,
9 mail::{MailApp, MailAppCreate, MailAppUpdate},
10 };
11 use smbcloud_network::{environment::Environment, network::request};
12 use smbcloud_networking::{constants::SMB_USER_AGENT, smb_client::SmbClient};
13
14 #[derive(Serialize)]
15 struct MailAppEnvelope<T> {
16 mail_app: T,
17 }
18
19 pub async fn get_mail_apps(
20 env: Environment,
21 client: (&SmbClient, &str),
22 access_token: String,
23 project_id: Option<String>,
24 ) -> Result<Vec<MailApp>, ErrorResponse> {
25 let builder = Client::new()
26 .get(build_mail_apps_url(env, client, project_id.as_deref()))
27 .header("Authorization", access_token)
28 .header("User-agent", SMB_USER_AGENT);
29 request(builder).await
30 }
31
32 pub async fn get_mail_app(
33 env: Environment,
34 client: (&SmbClient, &str),
35 access_token: String,
36 mail_app_id: String,
37 ) -> Result<MailApp, ErrorResponse> {
38 let builder = Client::new()
39 .get(build_mail_app_url(env, client, &mail_app_id))
40 .header("Authorization", access_token)
41 .header("User-agent", SMB_USER_AGENT);
42 request(builder).await
43 }
44
45 pub async fn create_mail_app(
46 env: Environment,
47 client: (&SmbClient, &str),
48 access_token: String,
49 mail_app: MailAppCreate,
50 ) -> Result<MailApp, ErrorResponse> {
51 let builder = Client::new()
52 .post(build_mail_apps_url(env, client, None))
53 .json(&MailAppEnvelope { mail_app })
54 .header("Authorization", access_token)
55 .header("User-agent", SMB_USER_AGENT);
56 request(builder).await
57 }
58
59 pub async fn update_mail_app(
60 env: Environment,
61 client: (&SmbClient, &str),
62 access_token: String,
63 mail_app_id: String,
64 mail_app: MailAppUpdate,
65 ) -> Result<MailApp, ErrorResponse> {
66 let builder = Client::new()
67 .put(build_mail_app_url(env, client, &mail_app_id))
68 .json(&MailAppEnvelope { mail_app })
69 .header("Authorization", access_token)
70 .header("User-agent", SMB_USER_AGENT);
71 request(builder).await
72 }
73
74 pub async fn delete_mail_app(
75 env: Environment,
76 client: (&SmbClient, &str),
77 access_token: String,
78 mail_app_id: String,
79 ) -> Result<(), ErrorResponse> {
80 let builder = Client::new()
81 .delete(build_mail_app_url(env, client, &mail_app_id))
82 .header("Authorization", access_token)
83 .header("User-agent", SMB_USER_AGENT);
84 request_empty(builder).await
85 }