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
use http::{
    self,
    method::Method,
    uri::{self, Uri},
};

use std::convert::TryFrom;

pub struct Client {
    auth_token: String,
    target: Uri,
    pub(crate) client: eiktyrner::HttpsClient,
}

impl Client {
    pub fn new<T, S: AsRef<str>>(target: T, token: S) -> Self
    where
        Uri: TryFrom<T>,
        <Uri as TryFrom<T>>::Error: std::fmt::Debug,
    {
        let target = Uri::try_from(target).expect("Invalid Billecta target");

        Self {
            client: eiktyrner::HttpsClient::new(),
            auth_token: format!("SecureToken {}", token.as_ref()),
            target,
        }
    }

    pub(crate) fn new_request(
        &self,
        method: Method,
        path_and_query: uri::PathAndQuery,
    ) -> http::request::Builder {
        let target = format!("{}{}", self.target, path_and_query.as_str())
            .parse::<Uri>()
            .map_err(|err| {
                format!(
                    "Failed to build target with {}: {}",
                    path_and_query.as_str(),
                    err
                )
            })
            .expect("Building new request");

        http::Request::builder()
            .method(method)
            .header("Authorization", &self.auth_token)
            .uri(target)
    }
}