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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::request::Request;
use crate::Error;
use crate::Result;
use derive_more::{Display, From, FromStr};
use reqwest::Method;
use reqwest::RequestBuilder;
use reqwest::Url;
use typed_builder::TypedBuilder;
static BASE_URL: &str = "https://api.lexoffice.io/v1";
#[derive(Clone, Debug, Display, FromStr, From)]
pub struct ApiKey(String);
impl ApiKey {
#[cfg(feature = "env")]
pub fn from_env() -> Result<Self> {
let env = std::env::var("LEXOFFICE_KEY")?;
Ok(Self::from(env))
}
#[cfg(feature = "fs")]
pub async fn from_file(file_name: &std::path::Path) -> Result<Self> {
let contents = tokio::fs::read_to_string(file_name).await?;
let contents = contents.trim();
Ok(contents.parse().unwrap())
}
#[cfg(all(feature = "fs", feature = "env"))]
pub async fn from_home() -> Result<Self> {
use std::env;
let home = env::var("HOME")?;
let mut file_name = std::path::PathBuf::from(home);
file_name.push(".lexoffice");
Self::from_file(&file_name).await
}
pub async fn try_default() -> Result<Self> {
cfg_if::cfg_if! {
if #[cfg(feature = "env")] {
if let Ok(key) = Self::from_env() {
return Ok(key);
}
}
}
cfg_if::cfg_if! {
if #[cfg(all(feature = "fs", feature = "env"))] {
if let Ok(key) = Self::from_home().await {
return Ok(key);
}
}
}
Err(Error::FailedToLoadApiKey)
}
}
impl From<&str> for ApiKey {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
#[cfg(not(target_arch = "wasm32"))]
fn default_client() -> reqwest::Client {
let user_agent = concat!(
"rust-",
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
);
reqwest::Client::builder()
.user_agent(user_agent)
.build()
.unwrap()
}
#[cfg(target_arch = "wasm32")]
fn default_client() -> reqwest::Client {
reqwest::Client::builder().build().unwrap()
}
#[derive(Debug, Clone, TypedBuilder)]
#[builder(doc)]
pub struct Client {
api_key: ApiKey,
#[builder(default=default_client())]
http_client: reqwest::Client,
#[builder(default=Url::parse(BASE_URL).unwrap())]
base_url: Url,
}
impl Client {
pub fn new<T: Into<ApiKey>>(api_key: T) -> Self {
Self::builder().api_key(api_key.into()).build()
}
pub fn request<T: Clone>(&self) -> Request<T> {
Request::new(self.clone())
}
pub(crate) fn http_builder(
&self,
method: Method,
url: Url,
) -> RequestBuilder {
let bearer = format!("Bearer {}", &self.api_key);
self.http_client
.request(method, url)
.header(reqwest::header::AUTHORIZATION, bearer)
}
pub fn base_url(&self) -> &Url {
&self.base_url
}
}