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
use super::api;

pub struct PinServerClient {
    pub client: reqwest::Client,
}

impl Default for PinServerClient {
    fn default() -> Self {
        Self::new()
    }
}

impl PinServerClient {
    pub fn new() -> Self {
        Self {
            client: reqwest::Client::new(),
        }
    }

    pub async fn request<D>(&self, req: api::PinServerRequestParams) -> Result<D, Error>
    where
        D: serde::de::DeserializeOwned,
    {
        let url = match &req.urls {
            api::PinServerUrls::Array(urls) => urls.first().ok_or(Error::NoUrlProvided)?,
            api::PinServerUrls::Object { url, .. } => url,
        };

        let res = self.client.post(url).json(&req.data).send().await?;

        if res.status().is_success() {
            res.json().await.map_err(Error::from)
        } else {
            Err(Error::Server(format!("{:?}", res)))
        }
    }
}

#[derive(Debug)]
pub enum Error {
    NoUrlProvided,
    Client(reqwest::Error),
    Server(String),
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Self::Client(e)
    }
}