mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-19 22:44:38 +00:00
separate RequestBody and ResponseBody traits from ResourceType
This commit is contained in:
parent
3a03dc60fa
commit
57e4f36852
6 changed files with 60 additions and 40 deletions
|
@ -126,4 +126,4 @@ pub use routing::{DrawResources, DrawResourceRoutes};
|
||||||
pub use routing::WithOpenapi;
|
pub use routing::WithOpenapi;
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
pub use types::ResourceType;
|
pub use types::*;
|
||||||
|
|
|
@ -4,6 +4,7 @@ use crate::{
|
||||||
routing::*,
|
routing::*,
|
||||||
OpenapiSchema,
|
OpenapiSchema,
|
||||||
OpenapiType,
|
OpenapiType,
|
||||||
|
RequestBody,
|
||||||
ResourceType
|
ResourceType
|
||||||
};
|
};
|
||||||
use futures::future::ok;
|
use futures::future::ok;
|
||||||
|
@ -21,7 +22,7 @@ use log::error;
|
||||||
use mime::{APPLICATION_JSON, TEXT_PLAIN};
|
use mime::{APPLICATION_JSON, TEXT_PLAIN};
|
||||||
use openapiv3::{
|
use openapiv3::{
|
||||||
Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
||||||
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody, Response, Responses, Schema,
|
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
||||||
SchemaKind, Server, StatusCode, Type
|
SchemaKind, Server, StatusCode, Type
|
||||||
};
|
};
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
@ -280,7 +281,7 @@ fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr<Schema
|
||||||
links: IndexMap::new()
|
links: IndexMap::new()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let request_body = body_schema.map(|schema| Item(RequestBody {
|
let request_body = body_schema.map(|schema| Item(OARequestBody {
|
||||||
description: None,
|
description: None,
|
||||||
content: schema_to_content(schema),
|
content: schema_to_content(schema),
|
||||||
required: true
|
required: true
|
||||||
|
@ -367,7 +368,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
fn search<Handler, Query, Res>(&mut self)
|
fn search<Handler, Query, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
|
Query : ResourceType + DeserializeOwned + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceSearch<Query, Res>
|
Handler : ResourceSearch<Query, Res>
|
||||||
{
|
{
|
||||||
|
@ -383,7 +384,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
fn create<Handler, Body, Res>(&mut self)
|
fn create<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceCreate<Body, Res>
|
Handler : ResourceCreate<Body, Res>
|
||||||
{
|
{
|
||||||
|
@ -400,7 +401,7 @@ macro_rules! implOpenapiRouter {
|
||||||
|
|
||||||
fn update_all<Handler, Body, Res>(&mut self)
|
fn update_all<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdateAll<Body, Res>
|
Handler : ResourceUpdateAll<Body, Res>
|
||||||
{
|
{
|
||||||
|
@ -418,7 +419,7 @@ macro_rules! implOpenapiRouter {
|
||||||
fn update<Handler, ID, Body, Res>(&mut self)
|
fn update<Handler, ID, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdate<ID, Body, Res>
|
Handler : ResourceUpdate<ID, Body, Res>
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{DrawResourceRoutes, ResourceResult, ResourceType};
|
use crate::{DrawResourceRoutes, RequestBody, ResourceResult, ResourceType};
|
||||||
use gotham::{
|
use gotham::{
|
||||||
router::response::extender::StaticResponseExtender,
|
router::response::extender::StaticResponseExtender,
|
||||||
state::{State, StateData}
|
state::{State, StateData}
|
||||||
|
@ -35,25 +35,25 @@ where
|
||||||
/// Handle a GET request on the Resource with additional search parameters.
|
/// Handle a GET request on the Resource with additional search parameters.
|
||||||
pub trait ResourceSearch<Query : ResourceType, R : ResourceResult>
|
pub trait ResourceSearch<Query : ResourceType, R : ResourceResult>
|
||||||
where
|
where
|
||||||
Query : ResourceType + StateData + StaticResponseExtender
|
Query : ResourceType + DeserializeOwned + StateData + StaticResponseExtender
|
||||||
{
|
{
|
||||||
fn search(state : &mut State, query : Query) -> R;
|
fn search(state : &mut State, query : Query) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a POST request on the Resource root.
|
/// Handle a POST request on the Resource root.
|
||||||
pub trait ResourceCreate<Body : ResourceType, R : ResourceResult>
|
pub trait ResourceCreate<Body : RequestBody, R : ResourceResult>
|
||||||
{
|
{
|
||||||
fn create(state : &mut State, body : Body) -> R;
|
fn create(state : &mut State, body : Body) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a PUT request on the Resource root.
|
/// Handle a PUT request on the Resource root.
|
||||||
pub trait ResourceUpdateAll<Body : ResourceType, R : ResourceResult>
|
pub trait ResourceUpdateAll<Body : RequestBody, R : ResourceResult>
|
||||||
{
|
{
|
||||||
fn update_all(state : &mut State, body : Body) -> R;
|
fn update_all(state : &mut State, body : Body) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a PUT request on the Resource with an id.
|
/// Handle a PUT request on the Resource with an id.
|
||||||
pub trait ResourceUpdate<ID, Body : ResourceType, R : ResourceResult>
|
pub trait ResourceUpdate<ID, Body : RequestBody, R : ResourceResult>
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{ResourceType, StatusCode};
|
use crate::{ResponseBody, StatusCode};
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
use crate::{OpenapiSchema, OpenapiType};
|
use crate::{OpenapiSchema, OpenapiType};
|
||||||
use hyper::Body;
|
use hyper::Body;
|
||||||
|
@ -110,7 +110,7 @@ impl<T : ToString> From<T> for ResourceError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
|
impl<R : ResponseBody, E : Error> ResourceResult for Result<R, E>
|
||||||
{
|
{
|
||||||
fn into_response(self) -> Result<Response, SerdeJsonError>
|
fn into_response(self) -> Result<Response, SerdeJsonError>
|
||||||
{
|
{
|
||||||
|
@ -170,7 +170,7 @@ impl<T> From<T> for Success<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T : ResourceType> ResourceResult for Success<T>
|
impl<T : ResponseBody> ResourceResult for Success<T>
|
||||||
{
|
{
|
||||||
fn into_response(self) -> Result<Response, SerdeJsonError>
|
fn into_response(self) -> Result<Response, SerdeJsonError>
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
resource::*,
|
resource::*,
|
||||||
result::{ResourceError, ResourceResult, Response},
|
result::{ResourceError, ResourceResult, Response},
|
||||||
|
RequestBody,
|
||||||
ResourceType,
|
ResourceType,
|
||||||
StatusCode
|
StatusCode
|
||||||
};
|
};
|
||||||
|
@ -77,26 +78,26 @@ pub trait DrawResourceRoutes
|
||||||
|
|
||||||
fn search<Handler, Query, Res>(&mut self)
|
fn search<Handler, Query, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
|
Query : ResourceType + DeserializeOwned + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceSearch<Query, Res>;
|
Handler : ResourceSearch<Query, Res>;
|
||||||
|
|
||||||
fn create<Handler, Body, Res>(&mut self)
|
fn create<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceCreate<Body, Res>;
|
Handler : ResourceCreate<Body, Res>;
|
||||||
|
|
||||||
fn update_all<Handler, Body, Res>(&mut self)
|
fn update_all<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdateAll<Body, Res>;
|
Handler : ResourceUpdateAll<Body, Res>;
|
||||||
|
|
||||||
fn update<Handler, ID, Body, Res>(&mut self)
|
fn update<Handler, ID, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdate<ID, Body, Res>;
|
Handler : ResourceUpdate<ID, Body, Res>;
|
||||||
|
|
||||||
|
@ -121,7 +122,7 @@ fn response_from(res : Response, state : &State) -> hyper::Response<Body>
|
||||||
}
|
}
|
||||||
if Method::borrow_from(state) != Method::HEAD
|
if Method::borrow_from(state) != Method::HEAD
|
||||||
{
|
{
|
||||||
*r.body_mut() = res.body.into();
|
*r.body_mut() = res.body;
|
||||||
}
|
}
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
@ -217,7 +218,7 @@ where
|
||||||
|
|
||||||
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceCreate<Body, Res>
|
Handler : ResourceCreate<Body, Res>
|
||||||
{
|
{
|
||||||
|
@ -226,7 +227,7 @@ where
|
||||||
|
|
||||||
fn update_all_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
fn update_all_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdateAll<Body, Res>
|
Handler : ResourceUpdateAll<Body, Res>
|
||||||
{
|
{
|
||||||
|
@ -236,7 +237,7 @@ where
|
||||||
fn update_handler<Handler, ID, Body, Res>(state : State) -> Box<HandlerFuture>
|
fn update_handler<Handler, ID, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdate<ID, Body, Res>
|
Handler : ResourceUpdate<ID, Body, Res>
|
||||||
{
|
{
|
||||||
|
@ -372,7 +373,7 @@ macro_rules! implDrawResourceRoutes {
|
||||||
|
|
||||||
fn create<Handler, Body, Res>(&mut self)
|
fn create<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceCreate<Body, Res>
|
Handler : ResourceCreate<Body, Res>
|
||||||
{
|
{
|
||||||
|
@ -384,7 +385,7 @@ macro_rules! implDrawResourceRoutes {
|
||||||
|
|
||||||
fn update_all<Handler, Body, Res>(&mut self)
|
fn update_all<Handler, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdateAll<Body, Res>
|
Handler : ResourceUpdateAll<Body, Res>
|
||||||
{
|
{
|
||||||
|
@ -397,7 +398,7 @@ macro_rules! implDrawResourceRoutes {
|
||||||
fn update<Handler, ID, Body, Res>(&mut self)
|
fn update<Handler, ID, Body, Res>(&mut self)
|
||||||
where
|
where
|
||||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
Body : ResourceType,
|
Body : RequestBody,
|
||||||
Res : ResourceResult,
|
Res : ResourceResult,
|
||||||
Handler : ResourceUpdate<ID, Body, Res>
|
Handler : ResourceUpdate<ID, Body, Res>
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,28 +3,46 @@ use crate::OpenapiType;
|
||||||
|
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
/// A type that can be used inside a request or response body. Implemented for every type
|
|
||||||
/// that is serializable with serde, however, it is recommended to use the rest_struct!
|
|
||||||
/// macro to create one.
|
|
||||||
#[cfg(not(feature = "openapi"))]
|
#[cfg(not(feature = "openapi"))]
|
||||||
pub trait ResourceType : DeserializeOwned + Serialize
|
pub trait ResourceType
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "openapi"))]
|
#[cfg(not(feature = "openapi"))]
|
||||||
impl<T : DeserializeOwned + Serialize> ResourceType for T
|
impl<T> ResourceType for T
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A type that can be used inside a request or response body. Implemented for every type
|
|
||||||
/// that is serializable with serde, however, it is recommended to use the rest_struct!
|
|
||||||
/// macro to create one.
|
|
||||||
#[cfg(feature = "openapi")]
|
|
||||||
pub trait ResourceType : OpenapiType + DeserializeOwned + Serialize
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "openapi")]
|
#[cfg(feature = "openapi")]
|
||||||
impl<T : OpenapiType + DeserializeOwned + Serialize> ResourceType for T
|
pub trait ResourceType : OpenapiType
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "openapi")]
|
||||||
|
impl<T : OpenapiType> ResourceType for T
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// A type that can be used inside a response body. Implemented for every type that is
|
||||||
|
/// serializable with serde. If the `openapi` feature is used, it must also be of type
|
||||||
|
/// `OpenapiType`.
|
||||||
|
pub trait ResponseBody : ResourceType + Serialize
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
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 + DeserializeOwned
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T : ResourceType + DeserializeOwned> RequestBody for T
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue