eskom_se_push_api/
traits.rs1use std::borrow::Cow;
2
3#[cfg(any(feature = "async", doc))]
4use async_trait::async_trait;
5
6use serde::de::DeserializeOwned;
7
8#[cfg(any(feature = "ureq", doc))]
9use crate::ureq_client::handle_ureq_response;
10
11use crate::errors::HttpError;
12
13pub trait Endpoint {
14 type Output: DeserializeOwned;
15
16 fn method(&self) -> &str {
18 "GET"
19 }
20
21 fn endpoint(&self) -> Cow<'static, str>;
23
24 fn url(&self) -> Result<url::Url, HttpError> {
26 Ok(url::Url::parse(&self.endpoint()).unwrap())
27 }
28
29 #[cfg(any(feature = "ureq", doc))]
30 fn ureq_client(&self, client: &ureq::Agent) -> Result<Self::Output, HttpError> {
34 let url_endpoint = self.url()?;
35 handle_ureq_response(client.request(self.method(), url_endpoint.as_str()).call())
36 }
37
38 #[cfg(any(feature = "ureq", doc))]
39 fn ureq(&self, token: &str) -> Result<Self::Output, HttpError> {
42 use crate::constants::TOKEN_KEY;
43
44 let url_endpoint = self.url()?;
45 handle_ureq_response(
46 ureq::request(self.method(), url_endpoint.as_str())
47 .set(TOKEN_KEY, token)
48 .call(),
49 )
50 }
51
52 #[cfg(any(all(feature = "reqwest", feature = "sync"), doc))]
53 fn reqwest_client(&self, client: &reqwest::blocking::Client) -> Result<Self::Output, HttpError> {
57 let url_endpoint = self.url()?;
58 crate::reqwest_blocking_client::handle_reqwest_response_blocking::<Self::Output>(
59 client.get(url_endpoint.as_str()).send(),
60 )
61 }
62
63 #[cfg(any(all(feature = "reqwest", feature = "sync"), doc))]
64 fn reqwest(&self, token: &str) -> Result<Self::Output, HttpError> {
67 use http::header;
68
69 use crate::constants::TOKEN_KEY;
70
71 let url_endpoint = self.url()?;
72 let mut headers = header::HeaderMap::new();
73 headers.insert(TOKEN_KEY, header::HeaderValue::from_str(token).unwrap());
74 let client = reqwest::blocking::ClientBuilder::new()
75 .default_headers(headers)
76 .build()
77 .unwrap();
78 crate::reqwest_blocking_client::handle_reqwest_response_blocking::<Self::Output>(
79 client.get(url_endpoint.as_str()).send(),
80 )
81 }
82}
83
84#[cfg(any(all(feature = "reqwest", feature = "async"), doc))]
85#[async_trait]
86pub trait EndpointAsync: Endpoint {
87 #[cfg(any(all(feature = "reqwest", feature = "async"), doc))]
88 async fn reqwest_client_async(
92 &self,
93 client: &reqwest::Client,
94 ) -> Result<Self::Output, HttpError> {
95 let url_endpoint = self.url()?;
96 crate::reqwest_async_client::handle_reqwest_response::<Self::Output>(
97 client.get(url_endpoint.as_str()).send().await,
98 )
99 .await
100 }
101
102 #[cfg(any(all(feature = "reqwest", feature = "async"), doc))]
103 async fn reqwest_async(&self, token: &str) -> Result<Self::Output, HttpError> {
106 let url_endpoint = self.url()?;
107
108 let mut headers = http::header::HeaderMap::new();
109 headers.insert(
110 crate::constants::TOKEN_KEY,
111 http::header::HeaderValue::from_str(token).unwrap(),
112 );
113 let client = reqwest::ClientBuilder::new()
114 .default_headers(headers)
115 .build()
116 .unwrap();
117 crate::reqwest_async_client::handle_reqwest_response::<Self::Output>(
118 client.get(url_endpoint.as_str()).send().await,
119 )
120 .await
121 }
122}