pub async fn deserialize_response<T>(
response: Response,
) -> Result<T, QobuzApiError>where
T: DeserializeOwned,Expand description
Deserializes an HTTP response to the expected type.
This asynchronous function reads the text content from an HTTP response and attempts to deserialize it into the specified type using serde. This is a utility function used throughout the library to convert API responses into Rust data structures. It handles both the reading of the response body and the deserialization process, providing appropriate error handling for both steps.
§Type Parameters
T- The type to deserialize the response into, must implementDeserializeOwned
§Arguments
response- The HTTP response to deserialize
§Returns
Ok(T)- The deserialized data if successfulErr(QobuzApiError)- If reading the response or deserializing fails
§Errors
This function will return an error if:
- Reading the response body fails
- Deserializing the response body to the target type fails
§Examples
use qobuz_api_rust::utils::deserialize_response;
use serde_json::Value;
use reqwest::get;
#[tokio::main]
async fn main() -> Result<(), qobuz_api_rust::QobuzApiError> {
let response = get("https://httpbin.org/json").await.map_err(qobuz_api_rust::QobuzApiError::HttpError)?;
let data: Value = deserialize_response(response).await?;
println!("{:?}", data);
Ok(())
}