mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-20 06:54:46 +00:00
add raw response
This commit is contained in:
parent
656595711c
commit
9e9b8869c9
2 changed files with 80 additions and 0 deletions
|
@ -2,6 +2,8 @@ use crate::{ResourceType, StatusCode};
|
|||
#[cfg(feature = "openapi")]
|
||||
use crate::{OpenapiSchema, OpenapiType};
|
||||
use mime::{Mime, APPLICATION_JSON};
|
||||
#[cfg(feature = "openapi")]
|
||||
use openapiv3::{SchemaKind, StringFormat, StringType, Type, VariantOrUnknownOrEmpty};
|
||||
use serde::Serialize;
|
||||
use serde_json::error::Error as SerdeJsonError;
|
||||
use std::error::Error;
|
||||
|
@ -15,6 +17,15 @@ pub struct Response
|
|||
|
||||
impl Response
|
||||
{
|
||||
pub fn new(status : StatusCode, body : String, mime : Option<Mime>) -> Self
|
||||
{
|
||||
Self {
|
||||
status,
|
||||
body,
|
||||
mime
|
||||
}
|
||||
}
|
||||
|
||||
pub fn json(status : StatusCode, body : String) -> Self
|
||||
{
|
||||
Self {
|
||||
|
@ -243,10 +254,66 @@ impl<E : Error> ResourceResult for Result<NoContent, E>
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Raw<T>
|
||||
{
|
||||
pub raw : T,
|
||||
pub mime : Mime
|
||||
}
|
||||
|
||||
impl<T> Raw<T>
|
||||
{
|
||||
pub fn new(raw : T, mime : Mime) -> Self
|
||||
{
|
||||
Self { raw, mime }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T : ToString> ResourceResult for Raw<T>
|
||||
{
|
||||
fn to_response(&self) -> Result<Response, SerdeJsonError>
|
||||
{
|
||||
Ok(Response::new(StatusCode::OK, self.raw.to_string(), Some(self.mime.clone())))
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn schema() -> OpenapiSchema
|
||||
{
|
||||
OpenapiSchema::new(SchemaKind::Type(Type::String(StringType {
|
||||
format: VariantOrUnknownOrEmpty::Item(StringFormat::Binary),
|
||||
pattern: None,
|
||||
enumeration: Vec::new()
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E : Error> ResourceResult for Result<Raw<T>, E>
|
||||
where
|
||||
Raw<T> : ResourceResult
|
||||
{
|
||||
fn to_response(&self) -> Result<Response, SerdeJsonError>
|
||||
{
|
||||
match self {
|
||||
Ok(raw) => raw.to_response(),
|
||||
Err(e) => {
|
||||
let err : ResourceError = e.into();
|
||||
Ok(Response::json(StatusCode::INTERNAL_SERVER_ERROR, serde_json::to_string(&err)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
fn schema() -> OpenapiSchema
|
||||
{
|
||||
<Raw<T> as ResourceResult>::schema()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test
|
||||
{
|
||||
use super::*;
|
||||
use mime::TEXT_PLAIN;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize)]
|
||||
|
@ -309,4 +376,15 @@ mod test
|
|||
assert_eq!(res.body, "");
|
||||
assert_eq!(res.mime, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_response()
|
||||
{
|
||||
let msg = "Test";
|
||||
let raw = Raw::new(msg, TEXT_PLAIN);
|
||||
let res = raw.to_response().expect("didn't expect error response");
|
||||
assert_eq!(res.status, StatusCode::OK);
|
||||
assert_eq!(res.body, msg);
|
||||
assert_eq!(res.mime, Some(TEXT_PLAIN));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue