refactor: Some renaming and using the status of the update operation
This commit is contained in:
parent
5c9ec1029b
commit
8e938e3299
|
@ -2,7 +2,7 @@ use serde::{Serialize, Deserialize};
|
|||
use failure::{Error, format_err};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ListResponse {
|
||||
struct CloudflareResponse {
|
||||
success: bool,
|
||||
errors: Vec<String>,
|
||||
result: Vec<ObjectWithId>,
|
||||
|
@ -24,7 +24,7 @@ struct UpdateIpData {
|
|||
pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> Result<String, Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://api.cloudflare.com/client/v4/zones?name={}", zone);
|
||||
let response: ListResponse = client
|
||||
let response: CloudflareResponse = client
|
||||
.get(&url)
|
||||
.header("X-Auth-Email", email)
|
||||
.header("X-Auth-Key", key)
|
||||
|
@ -33,7 +33,7 @@ pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> Result<String,
|
|||
.json()?;
|
||||
|
||||
if !response.success {
|
||||
let err: String = response.errors.iter().map(|s| s.to_owned()).collect();
|
||||
let err: String = response.errors.iter().map(|s| format!("{}\n", s.to_owned())).collect();
|
||||
return Err(format_err!("API Error: {}", err));
|
||||
}
|
||||
|
||||
|
@ -43,7 +43,7 @@ pub fn get_zone_identifier(zone: &str, email: &str, key: &str) -> Result<String,
|
|||
pub fn get_dns_record_id(zone_id: &str, domain: &str, email: &str, key: &str) -> Result<String, Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://api.cloudflare.com/client/v4/zones/{}/dns_records?name={}", zone_id, domain);
|
||||
let response: ListResponse = client
|
||||
let response: CloudflareResponse = client
|
||||
.get(&url)
|
||||
.header("X-Auth-Email", email)
|
||||
.header("X-Auth-Key", key)
|
||||
|
@ -52,7 +52,7 @@ pub fn get_dns_record_id(zone_id: &str, domain: &str, email: &str, key: &str) ->
|
|||
.json()?;
|
||||
|
||||
if !response.success {
|
||||
let err: String = response.errors.iter().map(|s| s.to_owned()).collect();
|
||||
let err: String = response.errors.iter().map(|s| format!("{}\n", s.to_owned())).collect();
|
||||
return Err(format_err!("API Error: {}", err));
|
||||
}
|
||||
|
||||
|
@ -77,13 +77,19 @@ pub fn update_ddns(ip: &str, domain: &str, zone_id: &str, record_id: &str, email
|
|||
content: ip.to_owned(),
|
||||
};
|
||||
|
||||
client
|
||||
let response: CloudflareResponse = client
|
||||
.put(&url)
|
||||
.header("X-Auth-Email", email)
|
||||
.header("X-Auth-Key", key)
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&update_data)
|
||||
.send()?;
|
||||
.send()?
|
||||
.json()?;
|
||||
|
||||
if !response.success {
|
||||
let err: String = response.errors.iter().map(|s| format!("{}\n", s.to_owned())).collect();
|
||||
return Err(format_err!("Unsuccessful update of DNS record: {}", err));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue