gitea_rust_sdk/user/
emails.rs

1use serde::{Deserialize, Serialize};
2
3use crate::client::Client;
4
5/// Abstaction of an email response containing the email address and whether the is verified / the
6/// primary email or not
7#[derive(Deserialize, Serialize, Clone)]
8pub struct Email
9{
10    pub email: String,
11    pub verified: bool,
12    pub primary: bool
13}
14
15/// Get all registered emails of the currently logged in user
16///
17/// ```rust
18/// use gitea_rust_sdk::{client::Client, user::emails::get_emails, URL, TOKEN};
19/// use assert_str::assert_str_eq;
20/// use tokio;
21///
22/// #[tokio::main]
23/// async fn main()
24/// {
25///     let client = Client::new(&URL, &TOKEN, "schorch/gitea_rust_sdk").unwrap();
26///     let emails = get_emails(client).await;
27///
28///     assert_str_eq!(emails[0].email, "rene@schaar.priv.at");
29///     assert!(emails[0].verified);
30///     assert!(emails[0].primary);
31/// }
32/// ````
33pub async fn get_emails(client: Client) -> Vec<Email>
34{
35    let emails = client
36        .cli
37        .get(&format!("{}/user/emails", client.base_url))
38        .send()
39        .await
40        .unwrap()
41        .error_for_status()
42        .unwrap()
43        .json::<Vec<Email>>()
44        .await
45        .unwrap();
46
47    return emails;
48}
49
50/// A struct to post and delete email addresses
51#[derive(Serialize)]
52struct RequestEmail
53{
54    emails: [String; 1]
55}
56
57/// Add an email address to the currently logged in user
58///
59/// ```no_run
60/// use gitea_rust_sdk::{client::Client, user::emails::add_email, URL, TOKEN};
61/// use tokio;
62///
63/// #[tokio::main]
64/// async fn main()
65/// {
66///     let client = Client::new(&URL, &TOKEN, "schorch/gitea_rust_sdk").unwrap();
67///     let email = add_email(client.clone(), "foo@bar.at").await;
68/// }
69/// ```
70pub async fn add_email(client: Client, email: &str) -> Email
71{
72    let email = client
73        .cli
74        .post(&format!("{}/user/emails", client.base_url))
75        .json(&RequestEmail{emails: [email.to_owned()]})
76        .send()
77        .await
78        .unwrap()
79        .error_for_status()
80        .unwrap()
81        .json::<Vec<Email>>()
82        .await
83        .unwrap()[0]
84        .clone();
85    
86    return email;
87}
88
89/// Delete an email addres of the currently logged in user
90///
91/// ```no_run
92/// use gitea_rust_sdk::{client::Client, user::emails::delete_email, URL, TOKEN};
93/// use tokio;
94///
95/// #[tokio::main]
96/// async fn main()
97/// {
98///     let client = Client::new(&URL, &TOKEN, "schorch/gitea_rust_sdk").unwrap();
99///     let success = delete_email(client, "foo@bar.at").await;
100/// }
101/// ```
102pub async fn delete_email(client: Client, email: &str) -> bool
103{
104    let response = client
105        .cli
106        .delete(&format!("{}/user/emails", client.base_url))
107        .json(&RequestEmail{emails: [email.to_owned()]})
108        .send()
109        .await
110        .unwrap()
111        .error_for_status();
112
113    return match response
114    {
115        Ok(_) => true,
116        Err(e) => {
117            println!("Error handling delete_email response: {}", e);
118            false
119        }
120    }
121}
122
123#[cfg(test)]
124mod tests
125{
126    use crate::{client::Client, URL, TOKEN, user::emails::{add_email, delete_email}};
127    use assert_str::assert_str_eq;
128
129    #[tokio::test]
130    async fn test_add_and_delete_email()
131    {
132        let client = Client::new(&URL, &TOKEN, "schorsch/gitea_rust_sdk").unwrap();
133        // Create an email to delete
134        let email = add_email(client.clone(), "foo@bar.at").await;
135        // Delete this particular email
136        let success = delete_email(client, "foo@bar.at").await;
137
138        // The email address should be "foo@bar.at" while it should neither be verified, nor the
139        // primary one.
140        assert_str_eq!(email.email, "foo@bar.at");
141        assert!(!email.verified);
142        assert!(!email.primary);
143
144        // The deletion of the email should be successful :)
145        assert!(success);
146    }
147}
148