1use super::config::PlatzClientConfig;
2use super::error::PlatzClientError;
3use super::request::PlatzRequest;
4use async_std::sync::RwLock;
5use reqwest::Url;
6use reqwest::header::{HeaderName, HeaderValue};
7
8pub struct PlatzClient {
9 config: RwLock<PlatzClientConfig>,
10}
11
12impl<'s> PlatzClient {
13 pub async fn new() -> Result<Self, PlatzClientError> {
14 Ok(Self {
15 config: RwLock::new(PlatzClientConfig::new().await?),
16 })
17 }
18
19 pub(super) async fn build_url(&self, path: &str) -> Result<Url, PlatzClientError> {
20 self.config
21 .read()
22 .await
23 .server_url
24 .join(path)
25 .map_err(PlatzClientError::UrlJoinError)
26 }
27
28 pub(super) async fn authorization(
29 &self,
30 ) -> Result<(HeaderName, HeaderValue), PlatzClientError> {
31 let mut config = self.config.write().await;
32 if config.expired() {
33 *config = PlatzClientConfig::new().await?;
34 }
35
36 config.get_authorization().await
37 }
38
39 pub fn request<S>(&'s self, method: reqwest::Method, path: S) -> PlatzRequest<'s>
40 where
41 S: AsRef<str>,
42 {
43 PlatzRequest::new(self, method, path)
44 }
45}