1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::PigRabbitError;
use serde::Serialize;
use crate::types::*;
const API_URL: &str = "https://porkbun.com/api/json/v3/";
pub struct PRClient {
pub key: Keys,
client: reqwest::Client,
}
impl PRClient {
pub fn new(keys: Keys) -> Self {
let client = reqwest::Client::new();
Self { key: keys, client }
}
async fn send_request<T: Serialize>(
client: &mut reqwest::Client,
url: &str,
body: T,
) -> Result<serde_json::Value, PigRabbitError> {
let res: serde_json::Value = client.post(url).json(&body).send().await?.json().await?;
Ok(res)
}
pub async fn add_record(
&mut self,
domain: &str,
record_struct: &Record,
) -> Result<(), PigRabbitError> {
let url = format!("{API_URL}dns/create/{domain}");
let body = ComplicatedBody {
secretapikey: &self.key.secretapikey,
apikey: &self.key.apikey,
name: &record_struct.name,
dtype: &record_struct.dtype,
content: &record_struct.content,
ttl: record_struct.ttl,
};
let res = Self::send_request(&mut self.client, &url, &body).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
pub async fn edit_by_domain_and_id(
&mut self,
domain: &str,
id: &str,
record_struct: &Record,
) -> Result<(), PigRabbitError> {
let url = format!("{API_URL}dns/edit/{domain}/{id}");
let body = ComplicatedBody {
secretapikey: &self.key.secretapikey,
apikey: &self.key.apikey,
name: &record_struct.name,
dtype: &record_struct.dtype,
content: &record_struct.content,
ttl: record_struct.ttl,
};
let res = Self::send_request(&mut self.client, &url, &body).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
pub async fn edit_by_domain_subdomain_and_type(
&mut self,
domain: &str,
subdomain: &str,
dtype: &str,
record_struct: Record,
) -> Result<(), PigRabbitError> {
let url = format!("{API_URL}dns/editByNameType/{domain}/{dtype}/{subdomain}");
let body = SimpleBody {
secretapikey: &self.key.secretapikey,
apikey: &self.key.apikey,
content: &record_struct.content,
ttl: record_struct.ttl,
};
let res = Self::send_request(&mut self.client, &url, &body).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
pub async fn del_by_type_with_subdomain(
&mut self,
dtype: &str,
domain: &str,
subdomain: &str,
) -> Result<(), PigRabbitError> {
let url = format!("{API_URL}dns/deleteByNameType/{domain}/{dtype}/{subdomain}");
let res = Self::send_request(&mut self.client, &url, &self.key).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
pub async fn del_by_id(&mut self, domain: &str, id: &str) -> Result<(), PigRabbitError> {
let url = format!("{API_URL}dns/delete/{domain}/{id}");
let res = Self::send_request(&mut self.client, &url, &self.key).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
pub async fn retreive_by_type_with_subdomain(
&mut self,
dtype: &str,
domain: &str,
subdomain: &str,
) -> Result<Vec<RecordInfo>, PigRabbitError> {
let url = format!("{API_URL}dns/retrieveByNameType/{domain}/{dtype}/{subdomain}");
let res = Self::send_request(&mut self.client, &url, &self.key).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(res["records"]
.as_array()
.map(|c| {
c.iter()
.map(|m| serde_json::from_value(m.to_owned()).unwrap())
.collect()
})
.unwrap()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
pub async fn retreive_by_domain_with_id(
&mut self,
domain: &str,
id: &str,
) -> Result<Vec<RecordInfo>, PigRabbitError> {
let url = format!("{API_URL}dns/retrieve/{domain}/{id}");
let res = Self::send_request(&mut self.client, &url, &self.key).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(res["records"]
.as_array()
.map(|c| {
c.iter()
.map(|m| serde_json::from_value(m.to_owned()).unwrap())
.collect()
})
.unwrap()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
pub async fn retreive_ssl_by_domain(
&mut self,
domain: &str,
) -> Result<Certificate, PigRabbitError> {
let url = format!("{API_URL}ssl/retrieve/{domain}");
let body = serde_json::to_string(&self.key).unwrap();
let res = Self::send_request(&mut self.client, &url, &body).await?;
match res["status"].as_str().unwrap() {
"SUCCESS" => Ok(serde_json::from_value(res).unwrap()),
_ => Err(PigRabbitError::ResponseError(res)),
}
}
}