shopify_client/common/
utils.rs

1use 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) => {
9            let response_json = serde_json::from_str::<T>(&response_text);
10            match response_json {
11                Ok(response_json) => Ok(response_json),
12                Err(_) => {
13                    let error_response_json = serde_json::from_str::<ErrorResp>(&response_text);
14
15                    match error_response_json {
16                        Ok(error_response_json) => Err(APIError::ServerError {
17                            errors: error_response_json.errors,
18                        }),
19                        Err(_) => Err(APIError::FailedToParse),
20                    }
21                }
22            }
23        }
24        Err(_) => Err(APIError::FailedToParse),
25    }
26}