| 241 | } |
| 242 | |
| 243 | async fn parse_response<T: DeserializeOwned>(&self, resp: reqwest::Response) -> Result<T> { |
| 244 | let status = resp.status(); |
| 245 | |
| 246 | if !status.is_success() { |
| 247 | let text = resp.text().await.map_err(|e| Error::UnexpectedError { |
| 248 | message: "failed to read response".to_string(), |
| 249 | source: Some(Box::new(e)), |
| 250 | })?; |
| 251 | |
| 252 | // Parse error response as ErrorResponse and map code to corresponding error |
| 253 | let error_response: super::ErrorResponse = |
| 254 | RestError::parse_error_response(&text, status.as_u16()); |
| 255 | let rest_error: RestError = RestError::from_error_response(error_response); |
| 256 | return Err(Error::from(rest_error)); |
| 257 | } |
| 258 | |
| 259 | // Parse successful response |
| 260 | let text = resp.text().await.map_err(|e| Error::UnexpectedError { |
| 261 | message: "failed to read response".to_string(), |
| 262 | source: Some(Box::new(e)), |
| 263 | })?; |
| 264 | |
| 265 | // Handle empty response body - return null as default for types like serde_json::Value |
| 266 | if text.trim().is_empty() { |
| 267 | return serde_json::from_str("null").map_err(|e| Error::UnexpectedError { |
| 268 | message: "failed to parse empty response".to_string(), |
| 269 | source: Some(Box::new(e)), |
| 270 | }); |
| 271 | } |
| 272 | |
| 273 | serde_json::from_str(&text).map_err(|e| Error::UnexpectedError { |
| 274 | message: "failed to parse json".to_string(), |
| 275 | source: Some(Box::new(e)), |
| 276 | }) |
| 277 | } |
| 278 | } |