1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 04:52:28 +00:00

post/create requests work

This commit is contained in:
Dominic 2019-09-27 17:43:01 +02:00
parent 8db145587f
commit d13155c90a
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
3 changed files with 93 additions and 7 deletions

View file

@ -1,3 +1,4 @@
#[macro_use] extern crate log;
#[macro_use] extern crate serde; #[macro_use] extern crate serde;
use fake::{faker::internet::en::Username, Fake}; use fake::{faker::internet::en::Username, Fake};
@ -7,7 +8,7 @@ use gotham::{
router::builder::*, router::builder::*,
state::State state::State
}; };
use gotham_restful::{DrawResources, DrawResourceRoutes, GetResource, IndexResource, Resource, Success}; use gotham_restful::*;
use log::LevelFilter; use log::LevelFilter;
use log4rs::{ use log4rs::{
append::console::ConsoleAppender, append::console::ConsoleAppender,
@ -17,7 +18,7 @@ use log4rs::{
struct Users; struct Users;
#[derive(Serialize)] #[derive(Deserialize, Serialize)]
struct User struct User
{ {
username : String username : String
@ -44,12 +45,22 @@ impl GetResource<u64, Success<User>> for Users
} }
} }
impl CreateResource<User, Success<()>> for Users
{
fn create(_state : &mut State, body : User) -> Success<()>
{
info!("Created User: {}", body.username);
().into()
}
}
impl Resource for Users impl Resource for Users
{ {
fn setup<D : DrawResourceRoutes>(mut route : D) fn setup<D : DrawResourceRoutes>(mut route : D)
{ {
route.index::<_, Self>(); route.index::<_, Self>();
route.get::<_, _, Self>(); route.get::<_, _, Self>();
route.create::<_, _, Self>();
} }
} }

View file

@ -27,5 +27,5 @@ where
/// Handle a POST request on the Resource root. /// Handle a POST request on the Resource root.
pub trait CreateResource<Body : DeserializeOwned, R : ResourceResult> pub trait CreateResource<Body : DeserializeOwned, R : ResourceResult>
{ {
fn post(state : &mut State, body : Body) -> R; fn create(state : &mut State, body : Body) -> R;
} }

View file

@ -1,5 +1,16 @@
use crate::{GetResource, IndexResource, Resource, ResourceResult}; use crate::{
use futures::future::{err, ok}; result::ResourceError,
CreateResource,
GetResource,
IndexResource,
Resource,
ResourceResult,
StatusCode
};
use futures::{
future::{Future, err, ok},
stream::Stream
};
use gotham::{ use gotham::{
handler::{HandlerFuture, IntoHandlerError}, handler::{HandlerFuture, IntoHandlerError},
helpers::http::response::create_response, helpers::http::response::create_response,
@ -34,11 +45,17 @@ pub trait DrawResourceRoutes
R : ResourceResult, R : ResourceResult,
IR : IndexResource<R>; IR : IndexResource<R>;
fn get<ID, R, IR>(&mut self) fn get<ID, R, GR>(&mut self)
where where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
R : ResourceResult, R : ResourceResult,
IR : GetResource<ID, R>; GR : GetResource<ID, R>;
fn create<Body, R, CR>(&mut self)
where
Body : DeserializeOwned,
R : ResourceResult,
CR : CreateResource<Body, R>;
} }
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture> fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
@ -56,6 +73,49 @@ where
} }
} }
fn handle_with_body<Body, F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
where
Body : DeserializeOwned,
F : FnOnce(&mut State, Body) -> R + Send + 'static,
R : ResourceResult
{
let f = hyper::Body::take_from(&mut state)
.concat2()
.then(|body| {
let body = match body {
Ok(body) => body,
Err(e) => return err((state, e.into_handler_error()))
};
let body = match serde_json::from_slice(&body) {
Ok(body) => body,
Err(e) => return {
let error : ResourceError = e.into();
match serde_json::to_string(&error) {
Ok(json) => {
let res = create_response(&state, StatusCode::BAD_REQUEST, APPLICATION_JSON, json);
ok((state, res))
},
Err(e) => err((state, e.into_handler_error()))
}
}
};
let res = get_result(&mut state, body).to_json();
match res {
Ok((status, body)) => {
let res = create_response(&state, status, APPLICATION_JSON, body);
ok((state, res))
},
Err(e) => err((state, e.into_handler_error()))
}
});
Box::new(f)
}
fn index_handler<R : ResourceResult, IR : IndexResource<R>>(state : State) -> Box<HandlerFuture> fn index_handler<R : ResourceResult, IR : IndexResource<R>>(state : State) -> Box<HandlerFuture>
{ {
to_handler_future(state, |state| IR::index(state)) to_handler_future(state, |state| IR::index(state))
@ -72,6 +132,11 @@ where
to_handler_future(state, |state| GR::get(state, id)) to_handler_future(state, |state| GR::get(state, id))
} }
fn create_handler<Body : DeserializeOwned, R : ResourceResult, CR : CreateResource<Body, R>>(state : State) -> Box<HandlerFuture>
{
handle_with_body::<Body, _, _>(state, |state, body| CR::create(state, body))
}
macro_rules! implDrawResourceRoutes { macro_rules! implDrawResourceRoutes {
($implType:ident) => { ($implType:ident) => {
impl<'a, C, P> DrawResources for $implType<'a, C, P> impl<'a, C, P> DrawResources for $implType<'a, C, P>
@ -109,6 +174,16 @@ macro_rules! implDrawResourceRoutes {
.with_path_extractor::<PathExtractor<ID>>() .with_path_extractor::<PathExtractor<ID>>()
.to(|state| get_handler::<ID, R, IR>(state)); .to(|state| get_handler::<ID, R, IR>(state));
} }
fn create<Body, R, CR>(&mut self)
where
Body : DeserializeOwned,
R : ResourceResult,
CR : CreateResource<Body, R>
{
self.0.post(&self.1)
.to(|state| create_handler::<Body, R, CR>(state));
}
} }
} }
} }