mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-20 06:54:46 +00:00
support accept header mime types
This commit is contained in:
parent
25117a035f
commit
bb5f58e97d
2 changed files with 81 additions and 15 deletions
|
@ -1,6 +1,7 @@
|
|||
use crate::{ResourceType, StatusCode};
|
||||
#[cfg(feature = "openapi")]
|
||||
use crate::{OpenapiSchema, OpenapiType};
|
||||
use mime::{Mime, APPLICATION_JSON};
|
||||
use serde::Serialize;
|
||||
use serde_json::error::Error as SerdeJsonError;
|
||||
use std::error::Error;
|
||||
|
@ -8,7 +9,15 @@ use std::error::Error;
|
|||
/// A trait provided to convert a resource's result to json.
|
||||
pub trait ResourceResult
|
||||
{
|
||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>;
|
||||
/// Turn this into a response that can be returned to the browser. This api will likely
|
||||
/// change in the future.
|
||||
fn to_response(&self) -> Result<(StatusCode, String), SerdeJsonError>;
|
||||
|
||||
/// Return a list of supported mime types.
|
||||
fn accepted_types() -> Option<Vec<Mime>>
|
||||
{
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn schema() -> OpenapiSchema;
|
||||
|
@ -50,7 +59,7 @@ impl<T : ToString> From<T> for ResourceError
|
|||
|
||||
impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
|
||||
{
|
||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
fn to_response(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
{
|
||||
Ok(match self {
|
||||
Ok(r) => (StatusCode::OK, serde_json::to_string(r)?),
|
||||
|
@ -61,6 +70,11 @@ impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
|
|||
})
|
||||
}
|
||||
|
||||
fn accepted_types() -> Option<Vec<Mime>>
|
||||
{
|
||||
Some(vec![APPLICATION_JSON])
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn schema() -> OpenapiSchema
|
||||
{
|
||||
|
@ -105,11 +119,16 @@ impl<T> From<T> for Success<T>
|
|||
|
||||
impl<T : ResourceType> ResourceResult for Success<T>
|
||||
{
|
||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
fn to_response(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
{
|
||||
Ok((StatusCode::OK, serde_json::to_string(&self.0)?))
|
||||
}
|
||||
|
||||
fn accepted_types() -> Option<Vec<Mime>>
|
||||
{
|
||||
Some(vec![APPLICATION_JSON])
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn schema() -> OpenapiSchema
|
||||
{
|
||||
|
@ -150,7 +169,7 @@ impl From<()> for NoContent
|
|||
impl ResourceResult for NoContent
|
||||
{
|
||||
/// This will always be a _204 No Content_ together with an empty string.
|
||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
fn to_response(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
{
|
||||
Ok((Self::default_status(), "".to_string()))
|
||||
}
|
||||
|
@ -172,7 +191,7 @@ impl ResourceResult for NoContent
|
|||
|
||||
impl<E : Error> ResourceResult for Result<NoContent, E>
|
||||
{
|
||||
fn to_json(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
fn to_response(&self) -> Result<(StatusCode, String), SerdeJsonError>
|
||||
{
|
||||
Ok(match self {
|
||||
Ok(_) => (Self::default_status(), "".to_string()),
|
||||
|
@ -217,7 +236,7 @@ mod test
|
|||
fn resource_result_ok()
|
||||
{
|
||||
let ok : Result<Msg, MsgError> = Ok(Msg::default());
|
||||
let (status, json) = ok.to_json().expect("didn't expect error response");
|
||||
let (status, json) = ok.to_response().expect("didn't expect error response");
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert_eq!(json, r#"{"msg":""}"#);
|
||||
}
|
||||
|
@ -226,7 +245,7 @@ mod test
|
|||
fn resource_result_err()
|
||||
{
|
||||
let err : Result<Msg, MsgError> = Err(MsgError::default());
|
||||
let (status, json) = err.to_json().expect("didn't expect error response");
|
||||
let (status, json) = err.to_response().expect("didn't expect error response");
|
||||
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
|
||||
assert_eq!(json, format!(r#"{{"error":true,"message":"{}"}}"#, err.unwrap_err()));
|
||||
}
|
||||
|
@ -235,7 +254,7 @@ mod test
|
|||
fn success_always_successfull()
|
||||
{
|
||||
let success : Success<Msg> = Msg::default().into();
|
||||
let (status, json) = success.to_json().expect("didn't expect error response");
|
||||
let (status, json) = success.to_response().expect("didn't expect error response");
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
assert_eq!(json, r#"{"msg":""}"#);
|
||||
}
|
||||
|
@ -244,7 +263,7 @@ mod test
|
|||
fn no_content_has_empty_json()
|
||||
{
|
||||
let no_content = NoContent::default();
|
||||
let (status, json) = no_content.to_json().expect("didn't expect error response");
|
||||
let (status, json) = no_content.to_response().expect("didn't expect error response");
|
||||
assert_eq!(status, StatusCode::NO_CONTENT);
|
||||
assert_eq!(json, "");
|
||||
}
|
||||
|
@ -253,8 +272,8 @@ mod test
|
|||
fn no_content_result()
|
||||
{
|
||||
let no_content = NoContent::default();
|
||||
let res_def = no_content.to_json().expect("didn't expect error response");
|
||||
let res_err = Result::<NoContent, MsgError>::Ok(no_content).to_json().expect("didn't expect error response");
|
||||
let res_def = no_content.to_response().expect("didn't expect error response");
|
||||
let res_err = Result::<NoContent, MsgError>::Ok(no_content).to_response().expect("didn't expect error response");
|
||||
assert_eq!(res_def, res_err);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue