oci_api/services/email/
client.rs1use async_trait::async_trait;
4use bytes::Bytes;
5use reqwest::Method;
6
7use crate::client::Oci;
8use crate::client::request_executor::{RequestPayload, RequestTarget};
9use crate::error::Result;
10use crate::services::email::models::*;
11use crate::services::email::sender_trait::EmailSender;
12
13#[derive(Clone)]
15pub struct EmailDelivery {
16 oci_client: Oci,
18
19 submit_endpoint: String,
21}
22
23impl EmailDelivery {
24 fn control_host_for(region: &str, realm_domain: &str) -> String {
25 format!("ctrl.email.{region}.oci.{realm_domain}")
26 }
27
28 pub async fn new(oci_client: Oci) -> Result<Self> {
35 let tenancy_id = oci_client.tenancy_id().to_string();
37 let region = oci_client.region().to_string();
38
39 let config =
41 Self::get_email_configuration_internal(&oci_client, &tenancy_id, ®ion).await?;
42
43 Ok(Self {
44 oci_client,
45 submit_endpoint: config.http_submit_endpoint,
46 })
47 }
48
49 async fn get_email_configuration_internal(
51 oci_client: &Oci,
52 compartment_id: &str,
53 region: &str,
54 ) -> Result<EmailConfiguration> {
55 let path = format!("/20170907/configuration?compartmentId={compartment_id}");
56 let host = Self::control_host_for(region, oci_client.realm_domain());
57 let response = oci_client
58 .executor()
59 .execute(
60 Method::GET,
61 RequestTarget {
62 scheme: "https",
63 host: &host,
64 path: &path,
65 },
66 RequestPayload {
67 body: None,
68 content_type: None,
69 extra_headers: Vec::new(),
70 },
71 )
72 .await?;
73 response.json().await.map_err(Into::into)
74 }
75
76 pub async fn get_email_configuration(
81 &self,
82 compartment_id: impl Into<String>,
83 ) -> Result<EmailConfiguration> {
84 let compartment_id = compartment_id.into();
85 let region = self.oci_client.region().to_string();
86 Self::get_email_configuration_internal(&self.oci_client, &compartment_id, ®ion).await
87 }
88
89 pub async fn send(&self, email: Email) -> Result<SubmitEmailResponse> {
97 self.send_impl(email).await
98 }
99
100 async fn send_impl(&self, mut email: Email) -> Result<SubmitEmailResponse> {
102 let compartment_id = self.oci_client.compartment_id().to_string();
104
105 if email.sender.compartment_id.is_empty() {
107 email.sender.set_compartment_id(&compartment_id);
108 }
109
110 let path = "/20220926/actions/submitEmail";
111 let body_json = serde_json::to_string(&email)?;
112 let response = self
113 .oci_client
114 .executor()
115 .execute(
116 Method::POST,
117 RequestTarget {
118 scheme: "https",
119 host: &self.submit_endpoint,
120 path,
121 },
122 RequestPayload {
123 body: Some(Bytes::from(body_json)),
124 content_type: Some("application/json"),
125 extra_headers: Vec::new(),
126 },
127 )
128 .await?;
129 response.json().await.map_err(Into::into)
130 }
131
132 pub async fn list_senders(
139 &self,
140 compartment_id: impl Into<String>,
141 lifecycle_state: Option<&str>,
142 email_address: Option<&str>,
143 ) -> Result<Vec<SenderSummary>> {
144 let compartment_id = compartment_id.into();
145
146 let mut query_params = vec![format!("compartmentId={}", compartment_id)];
148
149 if let Some(state) = lifecycle_state {
150 query_params.push(format!("lifecycleState={state}"));
151 }
152
153 if let Some(email) = email_address {
154 query_params.push(format!("emailAddress={email}"));
155 }
156
157 let query_string = query_params.join("&");
158 let path = format!("/20170907/senders?{query_string}");
159 let host = Self::control_host_for(self.oci_client.region(), self.oci_client.realm_domain());
160 let response = self
161 .oci_client
162 .executor()
163 .execute(
164 Method::GET,
165 RequestTarget {
166 scheme: "https",
167 host: &host,
168 path: &path,
169 },
170 RequestPayload {
171 body: None,
172 content_type: None,
173 extra_headers: Vec::new(),
174 },
175 )
176 .await?;
177 response.json().await.map_err(Into::into)
178 }
179}
180
181#[async_trait]
182impl EmailSender for EmailDelivery {
183 async fn send(&self, email: Email) -> Result<SubmitEmailResponse> {
184 self.send_impl(email).await
185 }
186}
187
188#[cfg(test)]
189mod tests {
190 use super::EmailDelivery;
191 use crate::client::{AuthMode, Oci};
192
193 fn instance_principal_client(region: &str, realm_domain: &str) -> Oci {
194 Oci::builder()
195 .auth_mode(AuthMode::InstancePrincipal)
196 .tenancy_id("ocid1.tenancy.oc1..example")
197 .region(region)
198 .realm_domain_component(realm_domain)
199 .build()
200 .unwrap()
201 }
202
203 #[test]
204 fn email_control_host_uses_commercial_realm_for_instance_principal() {
205 let oci = instance_principal_client("ap-chuncheon-1", "oraclecloud.com");
206 assert_eq!(
207 EmailDelivery::control_host_for(oci.region(), oci.realm_domain()),
208 "ctrl.email.ap-chuncheon-1.oci.oraclecloud.com"
209 );
210 }
211
212 #[test]
213 fn email_control_host_uses_gov_realm_for_instance_principal() {
214 let oci = instance_principal_client("us-langley-1", "oraclegovcloud.com");
215 assert_eq!(
216 EmailDelivery::control_host_for(oci.region(), oci.realm_domain()),
217 "ctrl.email.us-langley-1.oci.oraclegovcloud.com"
218 );
219 }
220}