shopify_client/common/
utils.rs1use reqwest::Response;
2
3use crate::common::types::{APIError, ErrorResp};
4
5pub async fn parse_response<T: serde::de::DeserializeOwned>(resp: Response) -> Result<T, APIError> {
6 let response_text = resp.text().await;
7 match response_text {
8 Ok(response_text) => parse_response_from_text(&response_text),
9 Err(_) => Err(APIError::FailedToParse),
10 }
11}
12
13pub fn parse_response_from_text<T: serde::de::DeserializeOwned>(text: &str) -> Result<T, APIError> {
14 let response_json = serde_json::from_str::<T>(text);
15 match response_json {
16 Ok(response_json) => Ok(response_json),
17 Err(_) => {
18 let error_response_json = serde_json::from_str::<ErrorResp>(text);
19
20 match error_response_json {
21 Ok(error_response_json) => Err(APIError::ServerError {
22 errors: error_response_json.errors,
23 }),
24 Err(_) => Err(APIError::FailedToParse),
25 }
26 }
27 }
28}