1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 14:57:01 +00:00

add derive for raw request body

This commit is contained in:
Dominic 2019-10-20 15:42:26 +02:00
parent f737ac4332
commit 5282dbbe6c
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
6 changed files with 243 additions and 18 deletions

View file

@ -22,7 +22,6 @@ A basic server with only one resource, handling a simple `GET` request, could lo
# use gotham::{router::builder::*, state::State};
# use gotham_restful::{DrawResources, Resource, Success};
# use serde::{Deserialize, Serialize};
#
/// Our RESTful Resource.
#[derive(Resource)]
#[rest_resource(read_all)]
@ -55,6 +54,32 @@ fn main() {
}
```
Uploads and Downloads can also be handled, but you need to specify the mime type manually:
```rust,no_run
# #[macro_use] extern crate gotham_restful_derive;
# use gotham::{router::builder::*, state::State};
# use gotham_restful::{DrawResources, Raw, Resource, Success};
# use serde::{Deserialize, Serialize};
#[derive(Resource)]
#[rest_resource(create)]
struct ImageResource;
#[derive(FromBody, RequestBody)]
#[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
struct RawImage(Vec<u8>);
#[rest_create(ImageResource)]
fn create(_state : &mut State, body : RawImage) -> Raw<Vec<u8>> {
Raw::new(body.0, mime::APPLICATION_OCTET_STREAM)
}
# fn main() {
# gotham::start("127.0.0.1:8080", build_simple_router(|route| {
# route.resource::<ImageResource, _>("image");
# }));
# }
```
Look at the [example] for more methods and usage with the `openapi` feature.
# License

View file

@ -38,34 +38,42 @@ impl<T : ResourceType + Serialize> ResponseBody for T
}
/// A type that can be used inside a request body. Implemented for every type that is
/// deserializable with serde. If the `openapi` feature is used, it must also be of type
/// `OpenapiType`.
pub trait RequestBody : ResourceType + Sized
/// This trait must be implemented by every type that can be used as a request body. It allows
/// to create the type from a hyper body chunk and it's content type.
pub trait FromBody : Sized
{
type Err : Into<ResourceError>;
/// Return all types that are supported as content types
fn supported_types() -> Option<Vec<Mime>>
{
None
}
/// Create the request body from a raw body and the content type.
fn from_body(body : Chunk, content_type : Mime) -> Result<Self, Self::Err>;
}
impl<T : ResourceType + DeserializeOwned> RequestBody for T
impl<T : DeserializeOwned> FromBody for T
{
type Err = serde_json::Error;
fn supported_types() -> Option<Vec<Mime>>
{
Some(vec![APPLICATION_JSON])
}
fn from_body(body : Chunk, _content_type : Mime) -> Result<Self, Self::Err>
{
serde_json::from_slice(&body)
}
}
/// A type that can be used inside a request body. Implemented for every type that is
/// deserializable with serde. If the `openapi` feature is used, it must also be of type
/// `OpenapiType`.
pub trait RequestBody : ResourceType + FromBody
{
/// Return all types that are supported as content types.
fn supported_types() -> Option<Vec<Mime>>
{
None
}
}
impl<T : ResourceType + DeserializeOwned> RequestBody for T
{
fn supported_types() -> Option<Vec<Mime>>
{
Some(vec![APPLICATION_JSON])
}
}