1use crate::{http, parse_response, utils, Client, Error};
2
3#[derive(serde_derive::Serialize)]
4pub struct AddRequest {
5 pub name: String,
6 pub region: String,
8}
9
10#[derive(serde_derive::Deserialize)]
11pub struct Domain {
12 pub id: String,
13 pub name: String,
14 pub created_at: String,
15 pub status: String,
16 pub records: Option<Vec<Record>>,
17 pub region: Option<String>,
18 pub dns_provider: Option<String>,
19}
20
21#[derive(serde_derive::Deserialize)]
22pub struct Record {
23 pub record: String,
24 pub name: String,
25 pub r#type: String,
26 pub ttl: String,
27 pub status: Option<String>,
28 pub value: String,
29 pub priority: Option<usize>,
30}
31
32#[derive(serde_derive::Deserialize)]
33pub struct VerifyResponse {
34 pub object: String,
35 pub id: String,
36}
37
38#[derive(serde_derive::Deserialize)]
39pub struct DeleteResponse {
40 pub object: String,
42 pub id: String,
43 pub deleted: bool,
44}
45
46pub async fn add(client: &Client, r: AddRequest) -> Result<Domain, Error> {
47 let request_json = serde_json::to_string(&r).map_err(Error::JSON)?;
48
49 let url = utils::url::domains::base(&client.base_url);
50 let request = http::Request::new(http::Method::Post, &url, Some(request_json.to_string()));
51
52 let response = parse_response(client.perform(request).await.map_err(Error::Client)?).await?;
53 serde_json::from_str(&response).map_err(Error::JSON)
54}
55
56pub async fn get(client: &Client, domain_id: &str) -> Result<Domain, Error> {
57 let url = utils::url::domains::with_id(&client.base_url, domain_id);
58 let request = http::Request::new(http::Method::Get, &url, None);
59
60 let response = parse_response(client.perform(request).await.map_err(Error::Client)?).await?;
61 serde_json::from_str(&response).map_err(Error::JSON)
62}
63
64pub async fn verify(client: &Client, domain_id: &str) -> Result<VerifyResponse, Error> {
65 let url = utils::url::domains::with_id(&client.base_url, domain_id);
66 let request = http::Request::new(http::Method::Post, &url, None);
67
68 let response = parse_response(client.perform(request).await.map_err(Error::Client)?).await?;
69 serde_json::from_str(&response).map_err(Error::JSON)
70}
71
72#[derive(serde_derive::Deserialize)]
73pub struct ListDomainsResponse {
74 pub data: Vec<Domain>,
75}
76
77pub async fn list(client: &Client) -> Result<ListDomainsResponse, Error> {
78 let url = utils::url::domains::base(&client.base_url);
79 let request = http::Request::new(http::Method::Get, &url, None);
80
81 let response = parse_response(client.perform(request).await.map_err(Error::Client)?).await?;
82 serde_json::from_str(&response).map_err(Error::JSON)
83}
84
85pub async fn delete(client: &Client, domain_id: &str) -> Result<DeleteResponse, Error> {
86 let url = utils::url::domains::with_id(&client.base_url, domain_id);
87 let request = http::Request::new(http::Method::Delete, &url, None);
88
89 let response = parse_response(client.perform(request).await.map_err(Error::Client)?).await?;
90 serde_json::from_str(&response).map_err(Error::JSON)
91}