netloc_http_request/
lib.rs

1//! Report an IP address update via an HTTP request.
2//!
3//! Currently plain text only, but other formats are planned.
4
5#![warn(missing_docs)]
6
7use async_trait::async_trait;
8use netloc_core::reporter::{Data, Reporter};
9pub use reqwest::Client;
10
11/// A [`Reporter`] that sends the data to the specified URL.
12pub struct HttpRequest {
13    /// HTTP client.
14    pub client: Client,
15
16    /// The URL to send the request to.
17    pub url: String,
18}
19
20impl HttpRequest {
21    /// Create a new [`HttpRequest`] reporter from a url.
22    pub fn from_url(url: impl Into<String>) -> Self {
23        let client = Client::new();
24        let url = url.into();
25        Self { client, url }
26    }
27}
28
29#[async_trait]
30impl Reporter for HttpRequest {
31    type Error = Box<dyn std::error::Error>;
32
33    async fn report(&self, data: &Data) -> Result<(), Self::Error> {
34        self.client
35            .post(&self.url)
36            .header(reqwest::header::CONTENT_TYPE, "text/plain")
37            .body(data.ip.clone())
38            .send()
39            .await?;
40        Ok(())
41    }
42}