ulule_client/
lib.rs

1use serde::de::DeserializeOwned;
2
3use ulule::error::RequestError;
4use ulule::search;
5
6pub struct Client {
7    client: reqwest::Client,
8    host: String,
9    ulule_version: String,
10}
11
12impl Client {
13    pub fn new() -> Client {
14        Client {
15            client: reqwest::Client::new(),
16            host: "https://api.ulule.com".to_string(),
17            ulule_version: "2019-04-11".to_string(),
18        }
19    }
20
21    pub fn with_host(self, host: impl Into<String>) -> Client {
22        let mut clt = self;
23        clt.host = host.into();
24        clt
25    }
26
27    pub fn with_ulule_version(self, version: impl Into<String>) -> Client {
28        let mut clt = self;
29        clt.ulule_version = version.into();
30        clt
31    }
32
33    pub async fn get<T, S, U>(&self, path: S, params: Option<U>) -> Result<T, Error>
34    where
35        T: DeserializeOwned,
36        S: Into<String>,
37        U: Into<String>,
38    {
39
40        let p = params.map_or("".to_string(), |par| (par.into()));
41        let url = self.host.to_owned() + &path.into() + &p;
42        let res = self.client.get(&url)
43            .header("Ulule-Version", &self.ulule_version)
44            .send()
45            .await
46            .map_err(|e| Error::Http(e))?;
47
48        if res.status().is_success() {
49            let item: T =res.json().await.map_err(|e| Error::Http(e))?;
50            return Ok(item)
51        }
52
53        let e: Vec<ulule::error::RequestError> = res.json().await.map_err(|e|{Error::Http(e)})?;
54        Err(Error::Ulule(e))
55    }
56}
57
58pub async fn search_projects(
59    client: &Client,
60    params: Option<impl Into<String>>,
61) -> Result<search::Projects, Error> {
62    client.get("/v1/search/projects", params).await
63}
64
65#[derive(Debug)]
66pub enum Error {
67    /// An error reported by Ulule in the response body.
68    Ulule(Vec<RequestError>),
69    /// An error reported by the reqwest client.
70    Http(reqwest::Error)
71}