-
-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
poem_openapi::response::StaticFileResponse
and implement `Ap…
…iResponse trait` for `poem::web::StaticFileResponse`
- Loading branch information
Showing
7 changed files
with
151 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "poem-openapi-derive" | ||
version = "2.0.22" | ||
version = "2.0.23" | ||
authors = ["sunli <[email protected]>"] | ||
edition = "2021" | ||
description = "Macros for poem-openapi" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "poem-openapi" | ||
version = "2.0.22" | ||
version = "2.0.23" | ||
authors = ["sunli <[email protected]>"] | ||
edition = "2021" | ||
description = "OpenAPI support for Poem." | ||
|
@@ -23,14 +23,14 @@ static-files = ["poem/static-files"] | |
websocket = ["poem/websocket"] | ||
|
||
[dependencies] | ||
poem-openapi-derive = { path = "../poem-openapi-derive", version = "2.0.22" } | ||
poem-openapi-derive = { path = "../poem-openapi-derive", version = "2.0.23" } | ||
poem = { path = "../poem", version = "1.3.51", features = [ | ||
"multipart", | ||
"tempfile", | ||
"cookie", | ||
"sse", | ||
"xml", | ||
"yaml" | ||
"yaml", | ||
] } | ||
|
||
tokio = { version = "1.17.0", features = ["fs"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,85 @@ | ||
use poem::{error::StaticFileError, Body}; | ||
use poem::{web::StaticFileResponse, Body}; | ||
|
||
use crate::{ | ||
payload::{Binary, PlainText}, | ||
payload::{Binary, Payload}, | ||
registry::{MetaHeader, MetaMediaType, MetaResponse, MetaResponses, Registry}, | ||
types::Type, | ||
ApiResponse, | ||
}; | ||
|
||
/// A static file response. | ||
#[cfg_attr(docsrs, doc(cfg(feature = "static-files")))] | ||
#[derive(ApiResponse)] | ||
#[oai(internal)] | ||
pub enum StaticFileResponse { | ||
/// Ok | ||
#[oai(status = 200)] | ||
Ok( | ||
Binary<Body>, | ||
/// The ETag (or entity tag) HTTP response header is an identifier for a | ||
/// specific version of a resource. It lets caches be more efficient and | ||
/// save bandwidth, as a web server does not need to resend a full | ||
/// response if the content was not changed. Additionally, etags help to | ||
/// prevent simultaneous updates of a resource from overwriting each | ||
/// other ("mid-air collisions"). | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag> | ||
#[oai(header = "etag")] | ||
Option<String>, | ||
/// The Last-Modified response HTTP header contains a date and time when | ||
/// the origin server believes the resource was last modified. It is | ||
/// used as a validator to determine if the resource is the same as the | ||
/// previously stored one. Less accurate than an ETag header, it is a | ||
/// fallback mechanism. Conditional requests containing | ||
/// If-Modified-Since or If-Unmodified-Since headers make use of this | ||
/// field. | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified> | ||
#[oai(header = "last-modified")] | ||
Option<String>, | ||
/// The Content-Type representation header is used to indicate the | ||
/// original media type of the resource (prior to any content encoding | ||
/// applied for sending). | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type> | ||
#[oai(header = "content-type")] | ||
Option<String>, | ||
), | ||
/// Not modified | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/304> | ||
#[oai(status = 304)] | ||
NotModified, | ||
/// Bad request | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400> | ||
#[oai(status = 400)] | ||
BadRequest, | ||
/// Resource was not found | ||
#[oai(status = 404)] | ||
NotFound, | ||
/// Precondition failed | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412> | ||
#[oai(status = 412)] | ||
PreconditionFailed, | ||
/// Range not satisfiable | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/416> | ||
#[oai(status = 416)] | ||
RangeNotSatisfiable( | ||
/// The Content-Range response HTTP header indicates where in a full | ||
/// body message a partial message belongs. | ||
/// | ||
/// Reference: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range> | ||
#[oai(header = "content-range")] | ||
String, | ||
), | ||
/// Internal server error | ||
#[oai(status = 500)] | ||
InternalServerError(PlainText<String>), | ||
} | ||
const ETAG_DESCRIPTION: &str = r#"The ETag (or entity tag) HTTP response header is an identifier for a specific version of a resource. It lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content was not changed. Additionally, etags help to prevent simultaneous updates of a resource from overwriting each other ("mid-air collisions")."#; | ||
const LAST_MODIFIED_DESCRIPTION: &str = r#"The Last-Modified response HTTP header contains a date and time when the origin server believes the resource was last modified. It is used as a validator to determine if the resource is the same as the previously stored one. Less accurate than an ETag header, it is a fallback mechanism. Conditional requests containing If-Modified-Since or If-Unmodified-Since headers make use of this field."#; | ||
const CONTENT_TYPE_DESCRIPTION: &str = r#"The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied for sending)."#; | ||
|
||
impl StaticFileResponse { | ||
/// Create a static file response. | ||
pub fn new(res: Result<poem::web::StaticFileResponse, StaticFileError>) -> Self { | ||
res.into() | ||
} | ||
} | ||
|
||
impl From<Result<poem::web::StaticFileResponse, StaticFileError>> for StaticFileResponse { | ||
fn from(res: Result<poem::web::StaticFileResponse, StaticFileError>) -> Self { | ||
match res { | ||
Ok(poem::web::StaticFileResponse::Ok { | ||
body, | ||
etag, | ||
last_modified, | ||
content_type, | ||
.. | ||
}) => StaticFileResponse::Ok(Binary(body), etag, last_modified, content_type), | ||
Ok(poem::web::StaticFileResponse::NotModified) => StaticFileResponse::NotModified, | ||
Err( | ||
StaticFileError::MethodNotAllowed(_) | ||
| StaticFileError::NotFound | ||
| StaticFileError::InvalidPath | ||
| StaticFileError::Forbidden(_), | ||
) => StaticFileResponse::NotFound, | ||
Err(StaticFileError::PreconditionFailed) => StaticFileResponse::PreconditionFailed, | ||
Err(StaticFileError::RangeNotSatisfiable { size }) => { | ||
StaticFileResponse::RangeNotSatisfiable(format!("*/{}", size)) | ||
} | ||
Err(StaticFileError::Io(_)) => StaticFileResponse::BadRequest, | ||
impl ApiResponse for StaticFileResponse { | ||
fn meta() -> MetaResponses { | ||
MetaResponses { | ||
responses: vec![ | ||
MetaResponse { | ||
description: "", | ||
status: Some(200), | ||
content: vec![MetaMediaType { | ||
content_type: Binary::<Body>::CONTENT_TYPE, | ||
schema: Binary::<Body>::schema_ref(), | ||
}], | ||
headers: vec![MetaHeader { | ||
name: "etag".to_string(), | ||
description: Some(ETAG_DESCRIPTION.to_string()), | ||
required: false, | ||
deprecated: false, | ||
schema: String::schema_ref(), | ||
}, MetaHeader { | ||
name: "last-modified".to_string(), | ||
description: Some(LAST_MODIFIED_DESCRIPTION.to_string()), | ||
required: false, | ||
deprecated: false, | ||
schema: String::schema_ref(), | ||
}, MetaHeader { | ||
name: "content-type".to_string(), | ||
description: Some(CONTENT_TYPE_DESCRIPTION.to_string()), | ||
required: false, | ||
deprecated: false, | ||
schema: String::schema_ref(), | ||
}], | ||
}, | ||
MetaResponse { | ||
description: "Not modified", | ||
status: Some(304), | ||
content: vec![], | ||
headers: vec![], | ||
}, | ||
MetaResponse { | ||
description: "Bad request", | ||
status: Some(400), | ||
content: vec![], | ||
headers: vec![], | ||
}, | ||
MetaResponse { | ||
description: "Resource was not found", | ||
status: Some(404), | ||
content: vec![], | ||
headers: vec![], | ||
}, | ||
MetaResponse { | ||
description: "Precondition failed", | ||
status: Some(412), | ||
content: vec![], | ||
headers: vec![], | ||
}, | ||
MetaResponse { | ||
description: "The Content-Range response HTTP header indicates where in a full body message a partial message belongs.", | ||
status: Some(416), | ||
content: vec![], | ||
headers: vec![], | ||
}, MetaResponse { | ||
description: "Internal server error", | ||
status: Some(500), | ||
content: vec![], | ||
headers: vec![], | ||
}, | ||
], | ||
} | ||
} | ||
|
||
fn register(_registry: &mut Registry) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters