diff --git a/libpaket/src/advices/www.rs b/libpaket/src/advices/www.rs index e5e2a46..7f774af 100644 --- a/libpaket/src/advices/www.rs +++ b/libpaket/src/advices/www.rs @@ -68,33 +68,11 @@ fn endpoint_advices() -> url::Url { impl crate::www::WebClient { // FIXME: more error parsing pub async fn advices(&self, dhli: &crate::login::DHLIdToken) -> crate::LibraryResult { - let cookie_headervalue = CookieHeaderValueBuilder::new() - .add_dhli(dhli) - .add_dhlcs(dhli) - .build_string(); + let res = request!(self.web_client, endpoint_advices, + header("Cookie", CookieHeaderValueBuilder::new().add_dhli(dhli).add_dhlcs(dhli).build_string()) + ); - let req = self.web_client - .get(endpoint_advices().clone()) - .header("Cookie", cookie_headervalue) - .build() - .unwrap(); - - let res = self.web_client.execute(req).await; - - if let Err(err) = res { - return Err(err.into()) - } - - let res = res.unwrap(); - let res_text = res.text().await.unwrap(); - let res = serde_json::from_str::(&res_text); - - let res = match res { - Ok(res) => res, - Err(err) => { - return Err(err.into()) - } - }; + let res = parse_json_response!(res, AdvicesResponse); if res.access_token_url.is_some() { if res.access_token_url.as_ref().unwrap().as_str() != crate::advices::endpoint_access_tokens() { diff --git a/libpaket/src/lib.rs b/libpaket/src/lib.rs index e88610e..d8c9c58 100644 --- a/libpaket/src/lib.rs +++ b/libpaket/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +mod utils; + mod www; pub use www::WebClient; @@ -8,8 +11,6 @@ pub mod stammdaten; pub use stammdaten::StammdatenClient; mod common; -#[macro_use] -mod utils; pub mod constants; #[cfg(feature = "locker_base")] diff --git a/libpaket/src/login/mod.rs b/libpaket/src/login/mod.rs index 757cde7..1e1faa0 100644 --- a/libpaket/src/login/mod.rs +++ b/libpaket/src/login/mod.rs @@ -4,14 +4,13 @@ mod openid_response; pub mod openid_token; mod utils; -pub use self::dhl_claims::{DHLIdToken, DHLCs}; +pub use self::dhl_claims::{DHLCs, DHLIdToken}; pub use self::openid_response::{RefreshToken, TokenResponse}; -pub use self::utils::{CodeVerfier, create_nonce}; +pub use self::utils::{create_nonce, CodeVerfier}; use super::common::APIResult; use crate::{LibraryError, LibraryResult}; - pub struct OpenIdClient { client: reqwest::Client, } @@ -31,27 +30,12 @@ impl OpenIdClient { &self, refresh_token: &RefreshToken, ) -> LibraryResult { - let req = self - .client - .post(constants::token::endpoint()) + let res = request_post!(self.client, + (constants::token::endpoint()), .form(constants::token::refresh_token_form(refresh_token.as_str()).as_slice()) - .build() - .unwrap(); + ); - let res = self.client.execute(req).await; - - match res { - Ok(res) => { - let res = res.text().await.unwrap(); - let res = serde_json::from_str::>(&res); - - match res { - Ok(res) => return res.into(), - Err(err) => Err(LibraryError::from(err)), - } - } - Err(err) => Err(LibraryError::from(err)), - } + parse_json_response_from_apiresult!(res, TokenResponse) } pub async fn token_authorization( @@ -78,23 +62,9 @@ impl OpenIdClient { let headermap = req.headers_mut(); headermap.append("Content-Length", len.into()); } - let res = self.client.execute(req).await; + let res = parse_response_internal!(res); - println!("auth_code: {:?}", res); - - match res { - Ok(res) => { - let res = res.text().await.unwrap(); - println!("auth_code reply: {:?}", res); - let res = serde_json::from_str::>(&res); - - match res { - Ok(res) => res.into(), - Err(err) => Err(LibraryError::from(err)), - } - } - Err(err) => Err(LibraryError::from(err)), - } + parse_json_response_from_apiresult!(res, TokenResponse) } } diff --git a/libpaket/src/utils.rs b/libpaket/src/utils.rs index b92c3d0..fff6f51 100644 --- a/libpaket/src/utils.rs +++ b/libpaket/src/utils.rs @@ -76,3 +76,127 @@ macro_rules! mini_assert_inval { }; } +macro_rules! parse_response_internal { + ($res: expr) => {{ + if let Err(err) = $res { + return Err(err.into()); + } + + let res = $res.unwrap(); + + res + }}; +} + +macro_rules! request_internal { + ( + $client: tt, + $method: ident, + $endpoint: tt + $(#$func:ident$args:tt)* + ) => { + { + let req = $client.$method($endpoint) + $( + .$func$args + )* + .build() + .unwrap(); + + let res = $client.execute(req).await; + + parse_response_internal!(res) + } + }; +} + +macro_rules! request { + ( + $self:ident.$client:ident, + $endpoint: tt, + $($func:ident$args:tt),* + ) => { + request_internal!(($self.$client), get, ($endpoint()) $(#$func$args)*) + }; + + ( + $self:ident.$client:ident, + $endpoint: ident, + $($func:ident$args:tt),* + ) => { + request_internal!(($self.$client), get, $endpoint $(#$func$args)*) + }; +} + +macro_rules! request_post { + ( + $self:ident.$client:ident, + $endpoint: ident, + $(.$func:ident$args:tt)* + ) => { + request_internal!( + ($self.$client), post, $endpoint $(# $func$args)*) + }; + ( + $self:ident.$client:ident, + $endpoint: tt, + $(.$func:ident$args:tt)* + ) => { + request_internal!( + ($self.$client), post, $endpoint $(# $func$args)*) + }; +} + +macro_rules! request_json { + ( + $self:ident.$client:ident, + $endpoint: ident, + $body: tt, + $($func:ident$args:tt),* + ) => { + request_json!($self.$client, ($endpoint()), $body, $( .$func$args)*) + }; + + ( + $self:ident.$client:ident, + $endpoint: tt, + $body: tt, + $(.$func:ident$args:tt)* + ) => {{ + let body = serde_json::to_string(&$body).unwrap(); + + request_post!( + $self.$client, $endpoint, + $( + .$func$args + )* + .header("content-type", "application/json") + .header("content-length", body.len()) + .body(body) + ) + }}; +} + +macro_rules! parse_json_response { + ($res: expr, $type: ty) => {{ + let res = $res.text().await.unwrap(); + let res = serde_json::from_str::<$type>(&res); + + match res { + Ok(res) => res, + Err(err) => return Err(crate::LibraryError::from(err)), + } + }}; +} + +macro_rules! parse_json_response_from_apiresult { + ($res: expr, $type: ty) => {{ + let res = $res.text().await.unwrap(); + let res = serde_json::from_str::>(&res); + + match res { + Ok(res) => res.into(), + Err(err) => Err(crate::LibraryError::from(err)), + } + }}; +}