1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 06:54:46 +00:00

delegate request body parsing to trait

This commit is contained in:
Dominic 2019-10-20 01:59:28 +02:00
parent 57e4f36852
commit f737ac4332
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
3 changed files with 40 additions and 5 deletions

View file

@ -27,6 +27,7 @@ use gotham::{
use hyper::{
header::CONTENT_TYPE,
Body,
HeaderMap,
Method
};
use mime::{Mime, APPLICATION_JSON};
@ -144,7 +145,7 @@ where
fn handle_with_body<Body, F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
where
Body : DeserializeOwned,
Body : RequestBody,
F : FnOnce(&mut State, Body) -> R + Send + 'static,
R : ResourceResult
{
@ -156,8 +157,16 @@ where
Ok(body) => body,
Err(e) => return err((state, e.into_handler_error()))
};
let content_type : Mime = match HeaderMap::borrow_from(&state).get(CONTENT_TYPE) {
Some(content_type) => content_type.to_str().unwrap().parse().unwrap(),
None => {
let res = create_empty_response(&state, StatusCode::UNSUPPORTED_MEDIA_TYPE);
return ok((state, res))
}
};
let body = match serde_json::from_slice(&body) {
let body = match Body::from_body(body, content_type) {
Ok(body) => body,
Err(e) => return {
let error : ResourceError = e.into();