mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 13:02:28 +00:00
post/create requests work
This commit is contained in:
parent
8db145587f
commit
d13155c90a
3 changed files with 93 additions and 7 deletions
|
@ -1,3 +1,4 @@
|
|||
#[macro_use] extern crate log;
|
||||
#[macro_use] extern crate serde;
|
||||
|
||||
use fake::{faker::internet::en::Username, Fake};
|
||||
|
@ -7,7 +8,7 @@ use gotham::{
|
|||
router::builder::*,
|
||||
state::State
|
||||
};
|
||||
use gotham_restful::{DrawResources, DrawResourceRoutes, GetResource, IndexResource, Resource, Success};
|
||||
use gotham_restful::*;
|
||||
use log::LevelFilter;
|
||||
use log4rs::{
|
||||
append::console::ConsoleAppender,
|
||||
|
@ -17,7 +18,7 @@ use log4rs::{
|
|||
|
||||
struct Users;
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct User
|
||||
{
|
||||
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
|
||||
{
|
||||
fn setup<D : DrawResourceRoutes>(mut route : D)
|
||||
{
|
||||
route.index::<_, Self>();
|
||||
route.get::<_, _, Self>();
|
||||
route.create::<_, _, Self>();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,5 +27,5 @@ where
|
|||
/// Handle a POST request on the Resource root.
|
||||
pub trait CreateResource<Body : DeserializeOwned, R : ResourceResult>
|
||||
{
|
||||
fn post(state : &mut State, body : Body) -> R;
|
||||
fn create(state : &mut State, body : Body) -> R;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
use crate::{GetResource, IndexResource, Resource, ResourceResult};
|
||||
use futures::future::{err, ok};
|
||||
use crate::{
|
||||
result::ResourceError,
|
||||
CreateResource,
|
||||
GetResource,
|
||||
IndexResource,
|
||||
Resource,
|
||||
ResourceResult,
|
||||
StatusCode
|
||||
};
|
||||
use futures::{
|
||||
future::{Future, err, ok},
|
||||
stream::Stream
|
||||
};
|
||||
use gotham::{
|
||||
handler::{HandlerFuture, IntoHandlerError},
|
||||
helpers::http::response::create_response,
|
||||
|
@ -34,11 +45,17 @@ pub trait DrawResourceRoutes
|
|||
R : ResourceResult,
|
||||
IR : IndexResource<R>;
|
||||
|
||||
fn get<ID, R, IR>(&mut self)
|
||||
fn get<ID, R, GR>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
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>
|
||||
|
@ -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>
|
||||
{
|
||||
to_handler_future(state, |state| IR::index(state))
|
||||
|
@ -72,6 +132,11 @@ where
|
|||
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 {
|
||||
($implType:ident) => {
|
||||
impl<'a, C, P> DrawResources for $implType<'a, C, P>
|
||||
|
@ -109,6 +174,16 @@ macro_rules! implDrawResourceRoutes {
|
|||
.with_path_extractor::<PathExtractor<ID>>()
|
||||
.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue