gandi_v5_livedns_api/api/
domains.rs

1//! A subset of LiveDNS Api relative to domains queries
2
3use std::error::Error;
4
5use crate::api::Api;
6use serde::{Deserialize, Serialize};
7
8/// Type representing a Domain
9#[derive(Debug, Deserialize, Serialize)]
10pub struct Domain {
11    /// Domain name
12    pub fqdn: String,
13}
14
15/// Type representing Domain's properties
16#[derive(Debug, Deserialize, Serialize)]
17pub struct DomainInfo {
18    /// Domain name
19    pub fqdn: String,
20    /// True if new snapshots are automatically created when a modification is made to this domain's records
21    pub automatic_snapshot: Option<bool>,
22}
23
24impl Api {
25    /// List of domains handled by LiveDNS
26    ///
27    /// GET on <https://api.gandi.net/v5/livedns/domains>
28    ///
29    /// # Examples:
30    ///
31    /// ```no_run
32    /// let api = Api::build(Endpoint::Prod, "token")?;
33    ///
34    /// let domains = api.domains().await?;
35    ///
36    /// println!("{:?}", domains);
37    /// ```
38    pub async fn domains(&self) -> Result<Vec<Domain>, Box<dyn Error>> {
39        self.engine.get("/livedns/domains").await
40    }
41
42    /// Show domain's properties
43    ///
44    /// GET on <https://api.gandi.net/v5/livedns/domains/{fqdn}>
45    ///
46    /// # Example:
47    ///
48    /// ```no_run
49    /// let api = Api::build(Endpoint::Prod, "token")?;
50    ///
51    /// let domain_info = api.domain("example.org").await?;
52    ///
53    /// println!("{:?}", domain_info);
54    /// ```
55    pub async fn domain(&self, fqdn: &str) -> Result<DomainInfo, Box<dyn Error>> {
56        self.engine.get(&format!("/livedns/domains/{}", fqdn)).await
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use std::env;
63
64    use crate::Api;
65
66    #[tokio::test]
67    async fn domains_empty() {
68        let pat = env::var("GANDI_V5_SANDBOX_PAT").unwrap();
69
70        let api = Api::build(crate::Endpoint::Sandbox, &pat);
71
72        assert!(api.is_ok());
73
74        let api = api.unwrap();
75
76        let res = api.domains().await;
77
78        assert!(res.is_ok());
79
80        let res = res.unwrap();
81
82        assert!(res.is_empty());
83    }
84
85    #[tokio::test]
86    async fn domain_404() {
87        let pat = env::var("GANDI_V5_SANDBOX_PAT").unwrap();
88
89        let api = Api::build(crate::Endpoint::Sandbox, &pat);
90
91        assert!(api.is_ok());
92
93        let api = api.unwrap();
94
95        let res = api.domain("pygoscelis-sandbox.org").await;
96
97        assert!(res.is_err());
98
99        assert_eq!(res.unwrap_err().as_ref().to_string(), "HTTP status client error (404 Not Found) for url (https://api.sandbox.gandi.net/v5/livedns/domains/pygoscelis-sandbox.org)");
100    }
101
102    #[tokio::test]
103    async fn domain_403() {
104        let api = Api::build(crate::Endpoint::Sandbox, "INVALID");
105
106        assert!(api.is_ok());
107
108        let api = api.unwrap();
109
110        let res = api.domain("pygoscelis-sandbox.org").await;
111
112        assert!(res.is_err());
113
114        assert_eq!(res.unwrap_err().as_ref().to_string(), "HTTP status client error (403 Forbidden) for url (https://api.sandbox.gandi.net/v5/livedns/domains/pygoscelis-sandbox.org)");
115    }
116}