Browse Source

Added proper error handling for http responses

Cesar Rodas 1 week ago
parent
commit
405f2f70aa

+ 8 - 0
crates/cdk-cli/src/sub_commands/cat_device_login.rs

@@ -88,6 +88,10 @@ async fn get_device_code_token(mint_info: &MintInfo) -> (String, String) {
         .finish();
     let device_code_response = bitreq::post(device_auth_url)
         .with_body(params)
+        .with_header(
+            "Content-Type".to_string(),
+            "application/x-www-form-urlencoded".to_string(),
+        )
         .send_async()
         .await
         .expect("Failed to send device code request");
@@ -134,6 +138,10 @@ async fn get_device_code_token(mint_info: &MintInfo) -> (String, String) {
             .finish();
         let token_response = bitreq::post(&token_url)
             .with_body(params)
+            .with_header(
+                "Content-Type".to_string(),
+                "application/x-www-form-urlencoded".to_string(),
+            )
             .send_async()
             .await
             .expect("Failed to send token request");

+ 4 - 0
crates/cdk-cli/src/sub_commands/cat_login.rs

@@ -95,6 +95,10 @@ async fn get_access_token(mint_info: &MintInfo, user: &str, password: &str) -> (
         .finish();
     let response = bitreq::post(token_url)
         .with_body(params)
+        .with_header(
+            "Content-Type".to_string(),
+            "application/x-www-form-urlencoded".to_string(),
+        )
         .send_async()
         .await
         .expect("Failed to send token request");

+ 4 - 0
crates/cdk-cli/src/sub_commands/mint_blind_auth.rs

@@ -171,6 +171,10 @@ async fn refresh_access_token(
         .finish();
     let response = bitreq::post(token_url)
         .with_body(params)
+        .with_header(
+            "Content-Type".to_string(),
+            "application/x-www-form-urlencoded".to_string(),
+        )
         .send_async()
         .await?;
 

+ 8 - 0
crates/cdk-integration-tests/tests/fake_auth.rs

@@ -774,6 +774,10 @@ async fn get_access_token(mint_info: &MintInfo) -> (String, String) {
         .finish();
     let response = bitreq::post(token_url)
         .with_body(params)
+        .with_header(
+            "Content-Type".to_string(),
+            "application/x-www-form-urlencoded".to_string(),
+        )
         .send_async()
         .await
         .expect("Failed to send token request");
@@ -825,6 +829,10 @@ async fn get_custom_access_token(
         .finish();
     let response = bitreq::post(token_url)
         .with_body(params)
+        .with_header(
+            "Content-Type".to_string(),
+            "application/x-www-form-urlencoded".to_string(),
+        )
         .send_async()
         .await
         .map_err(|_| Error::Custom("Failed to send token request".to_string()))?;

+ 4 - 0
crates/cdk/examples/auth_wallet.rs

@@ -125,6 +125,10 @@ async fn get_access_token(mint_info: &MintInfo) -> String {
         .finish();
     let response = bitreq::post(token_url)
         .with_body(params)
+        .with_header(
+            "Content-Type".to_string(),
+            "application/x-www-form-urlencoded".to_string(),
+        )
         .send_async()
         .await
         .expect("Failed to send token request");

+ 4 - 0
crates/cdk/src/oidc_client.rs

@@ -270,6 +270,10 @@ impl OidcClient {
 
         let response = bitreq::post(token_url)
             .with_body(body)
+            .with_header(
+                "Content-Type".to_string(),
+                "application/x-www-form-urlencoded".to_string(),
+            )
             .send_async_with_client(&self.client)
             .await?
             .json::<TokenResponse>()?;

+ 23 - 29
crates/cdk/src/wallet/mint_connector/transport.rs

@@ -2,7 +2,7 @@
 use std::collections::HashMap;
 use std::fmt::Debug;
 
-use bitreq::{Client, Proxy, Request, RequestExt};
+use bitreq::{Client, Proxy, Request, RequestExt, Response};
 use cdk_common::AuthToken;
 #[cfg(all(feature = "bip353", not(target_arch = "wasm32")))]
 use hickory_resolver::config::ResolverConfig;
@@ -70,6 +70,26 @@ pub struct Async {
 }
 
 impl Async {
+    fn handle_response<R>(response: Response) -> Result<R, Error>
+    where
+        R: DeserializeOwned,
+    {
+        if response.status_code != 200 {
+            return Err(match ErrorResponse::from_slice(response.as_bytes()) {
+                Ok(ok) => <ErrorResponse as Into<Error>>::into(ok),
+                Err(_) => Error::HttpError(Some(response.status_code as u16), "".to_string()),
+            });
+        }
+
+        serde_json::from_slice::<R>(response.as_bytes()).map_err(|err| {
+            tracing::warn!("Http Response error: {}", err);
+            match ErrorResponse::from_slice(response.as_bytes()) {
+                Ok(ok) => <ErrorResponse as Into<Error>>::into(ok),
+                Err(err) => err.into(),
+            }
+        })
+    }
+
     fn prepare_request(&self, req: Request, url: Url, auth: Option<AuthToken>) -> Request {
         let proxy = {
             let url = url.to_string();
@@ -196,20 +216,7 @@ impl Transport for Async {
             .await
             .map_err(|e| Error::HttpError(None, e.to_string()))?;
 
-        if response.status_code != 200 {
-            return Err(Error::HttpError(
-                Some(response.status_code as u16),
-                "".to_string(),
-            ));
-        }
-
-        serde_json::from_slice::<R>(response.as_bytes()).map_err(|err| {
-            tracing::warn!("Http Response error: {}", err);
-            match ErrorResponse::from_slice(response.as_bytes()) {
-                Ok(ok) => <ErrorResponse as Into<Error>>::into(ok),
-                Err(err) => err.into(),
-            }
-        })
+        Self::handle_response(response)
     }
 
     async fn http_post<P, R>(
@@ -233,20 +240,7 @@ impl Transport for Async {
             .await
             .map_err(|e| Error::HttpError(None, e.to_string()))?;
 
-        if response.status_code != 200 {
-            return Err(Error::HttpError(
-                Some(response.status_code as u16),
-                "".to_string(),
-            ));
-        }
-
-        serde_json::from_slice::<R>(response.as_bytes()).map_err(|err| {
-            tracing::warn!("Http Response error: {}", err);
-            match ErrorResponse::from_slice(response.as_bytes()) {
-                Ok(ok) => <ErrorResponse as Into<Error>>::into(ok),
-                Err(err) => err.into(),
-            }
-        })
+        Self::handle_response(response)
     }
 }