add awc::ClientResponse::json_with_content_type_unchecked

This commit is contained in:
Ozgur Akkurt 2021-10-05 03:34:42 +03:00
parent a3806cde19
commit 420b17d985
1 changed files with 23 additions and 10 deletions

View File

@ -203,15 +203,25 @@ where
MessageBody::new(self) MessageBody::new(self)
} }
/// Loads and parse `application/json` encoded body. /// Loads and parses `application/json` encoded body.
/// Return `JsonBody<T>` future. It resolves to a `T` value. /// Returns a s`JsonBody<T>` future. It resolves to a `T` value.
/// ///
/// Returns error: /// Returns error:
/// ///
/// * content type is not `application/json` /// * content type is not `application/json`
/// * content length is greater than 256k /// * content length is greater than 256k
pub fn json<T: DeserializeOwned>(&mut self) -> JsonBody<S, T> { pub fn json<T: DeserializeOwned>(&mut self) -> JsonBody<S, T> {
JsonBody::new(self) JsonBody::new(self, true)
}
/// Loads and parses `application/json` encoded body.
/// Returns a `JsonBody<T>` future. It resolves to a `T` value.
///
/// Returns error:
///
/// * content length is greater than 256k
pub fn json_with_content_type_unchecked<T: DeserializeOwned>(&mut self) -> JsonBody<S, T> {
JsonBody::new(self, false)
} }
} }
@ -337,14 +347,17 @@ where
U: DeserializeOwned, U: DeserializeOwned,
{ {
/// Create `JsonBody` for request. /// Create `JsonBody` for request.
pub fn new(res: &mut ClientResponse<S>) -> Self { pub fn new(res: &mut ClientResponse<S>, check_content_type: bool) -> Self {
// check content-type // Create a function to check content-type
let json = if let Ok(Some(mime)) = res.mime_type() { let json = || {
mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON) if let Ok(Some(mime)) = res.mime_type() {
} else { mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
false } else {
false
}
}; };
if !json {
if check_content_type && !json() {
return JsonBody { return JsonBody {
length: None, length: None,
fut: None, fut: None,