cloudflareresponse
This commit is contained in:
		@@ -34,6 +34,14 @@ struct DnsRecord {
 | 
				
			|||||||
    meta: HashMap<Box<str>, serde_json::Value>,
 | 
					    meta: HashMap<Box<str>, serde_json::Value>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Serialize, Deserialize, Debug)]
 | 
				
			||||||
 | 
					struct CloudflareResponse {
 | 
				
			||||||
 | 
					    success: bool,
 | 
				
			||||||
 | 
					    errors: Vec<HashMap<String, serde_json::Value>>,
 | 
				
			||||||
 | 
					    messages: Vec<HashMap<String, serde_json::Value>>,
 | 
				
			||||||
 | 
					    result: Option<Vec<DnsRecord>>,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl CloudflareClient {
 | 
					impl CloudflareClient {
 | 
				
			||||||
    pub async fn new(api_key: Box<str>, zone_id: Box<str>, domains: Vec<Box<str>>) -> Result<Self> {
 | 
					    pub async fn new(api_key: Box<str>, zone_id: Box<str>, domains: Vec<Box<str>>) -> Result<Self> {
 | 
				
			||||||
        let force_ipv4 = IpAddr::from([0, 0, 0, 0]);
 | 
					        let force_ipv4 = IpAddr::from([0, 0, 0, 0]);
 | 
				
			||||||
@@ -60,7 +68,7 @@ impl CloudflareClient {
 | 
				
			|||||||
            return Ok(());
 | 
					            return Ok(());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        info!(
 | 
					        info!(
 | 
				
			||||||
            "Ip has changed from '{}' -> '{}'",
 | 
					            "IP has changed from '{}' -> '{}'",
 | 
				
			||||||
            &self.current_ip, &new_ip
 | 
					            &self.current_ip, &new_ip
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        self.update_dns_records(new_ip).await?;
 | 
					        self.update_dns_records(new_ip).await?;
 | 
				
			||||||
@@ -75,7 +83,7 @@ impl CloudflareClient {
 | 
				
			|||||||
                Ok(r) => r,
 | 
					                Ok(r) => r,
 | 
				
			||||||
                Err(e) => {
 | 
					                Err(e) => {
 | 
				
			||||||
                    error!(
 | 
					                    error!(
 | 
				
			||||||
                        "Could not getch dns records for domain '{}': {:?}",
 | 
					                        "Could not fetch DNS records for domain '{}': {:?}",
 | 
				
			||||||
                        &domain, &e
 | 
					                        &domain, &e
 | 
				
			||||||
                    );
 | 
					                    );
 | 
				
			||||||
                    continue;
 | 
					                    continue;
 | 
				
			||||||
@@ -109,7 +117,8 @@ impl CloudflareClient {
 | 
				
			|||||||
            "https://api.cloudflare.com/client/v4/zones/{}/dns_records?type=A&name={}",
 | 
					            "https://api.cloudflare.com/client/v4/zones/{}/dns_records?type=A&name={}",
 | 
				
			||||||
            self.zone_id, domain
 | 
					            self.zone_id, domain
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
        let mut response = self
 | 
					
 | 
				
			||||||
 | 
					        let response = self
 | 
				
			||||||
            .client
 | 
					            .client
 | 
				
			||||||
            .get(&url)
 | 
					            .get(&url)
 | 
				
			||||||
            .header("Authorization", format!("Bearer {}", self.api_key))
 | 
					            .header("Authorization", format!("Bearer {}", self.api_key))
 | 
				
			||||||
@@ -117,12 +126,23 @@ impl CloudflareClient {
 | 
				
			|||||||
            .send()
 | 
					            .send()
 | 
				
			||||||
            .await?
 | 
					            .await?
 | 
				
			||||||
            .error_for_status()?
 | 
					            .error_for_status()?
 | 
				
			||||||
            .json::<HashMap<String, Vec<DnsRecord>>>()
 | 
					            .text()
 | 
				
			||||||
            .await?;
 | 
					            .await?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        response
 | 
					        let cloudflare_response: CloudflareResponse =
 | 
				
			||||||
            .remove("result")
 | 
					            serde_json::from_str(&response).context("Failed to deserialize Cloudflare response")?;
 | 
				
			||||||
            .context("Key result not in return")
 | 
					
 | 
				
			||||||
 | 
					        if !cloudflare_response.success {
 | 
				
			||||||
 | 
					            error!(
 | 
				
			||||||
 | 
					                "Cloudflare API returned errors: {:?}",
 | 
				
			||||||
 | 
					                cloudflare_response.errors
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					            return Err(anyhow::anyhow!("Cloudflare API returned errors"));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        cloudflare_response
 | 
				
			||||||
 | 
					            .result
 | 
				
			||||||
 | 
					            .context("Key 'result' not found in response")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async fn update_dns_record(&self, record: &mut DnsRecord, ip_content: Box<str>) -> Result<()> {
 | 
					    async fn update_dns_record(&self, record: &mut DnsRecord, ip_content: Box<str>) -> Result<()> {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user