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
use serde::de::DeserializeOwned;

use ulule::error::RequestError;
use ulule::search;

pub struct Client {
    client: reqwest::Client,
    host: String,
    ulule_version: String,
}

impl Client {
    pub fn new() -> Client {
        Client {
            client: reqwest::Client::new(),
            host: "https://api.ulule.com".to_string(),
            ulule_version: "2019-04-11".to_string(),
        }
    }

    pub fn with_host(self, host: impl Into<String>) -> Client {
        let mut clt = self;
        clt.host = host.into();
        clt
    }

    pub fn with_ulule_version(self, version: impl Into<String>) -> Client {
        let mut clt = self;
        clt.ulule_version = version.into();
        clt
    }

    pub async fn get<T, S, U>(&self, path: S, params: Option<U>) -> Result<T, Error>
    where
        T: DeserializeOwned,
        S: Into<String>,
        U: Into<String>,
    {

        let p = params.map_or("".to_string(), |par| (par.into()));
        let url = self.host.to_owned() + &path.into() + &p;
        let res = self.client.get(&url)
            .header("Ulule-Version", &self.ulule_version)
            .send()
            .await
            .map_err(|e| Error::Http(e))?;

        if res.status().is_success() {
            let item: T =res.json().await.map_err(|e| Error::Http(e))?;
            return Ok(item)
        }

        let e: Vec<ulule::error::RequestError> = res.json().await.map_err(|e|{Error::Http(e)})?;
        Err(Error::Ulule(e))
    }
}

pub async fn search_projects(
    client: &Client,
    params: Option<impl Into<String>>,
) -> Result<search::Projects, Error> {
    client.get("/v1/search/projects", params).await
}

#[derive(Debug)]
pub enum Error {
    /// An error reported by Ulule in the response body.
    Ulule(Vec<RequestError>),
    /// An error reported by the reqwest client.
    Http(reqwest::Error)
}