poem_openapi/payload/
binary.rs

1use std::ops::{Deref, DerefMut};
2
3use bytes::Bytes;
4use poem::{Body, FromRequest, IntoResponse, Request, RequestBody, Response, Result};
5
6use crate::{
7    ApiResponse,
8    payload::{ParsePayload, Payload},
9    registry::{MetaMediaType, MetaResponse, MetaResponses, MetaSchema, MetaSchemaRef, Registry},
10};
11
12/// A binary payload.
13///
14/// # Examples
15///
16/// ```rust
17/// use poem::{
18///     Body, IntoEndpoint, Request, Result,
19///     error::BadRequest,
20///     http::{Method, StatusCode, Uri},
21///     test::TestClient,
22/// };
23/// use poem_openapi::{
24///     OpenApi, OpenApiService,
25///     payload::{Binary, Json},
26/// };
27/// use tokio::io::AsyncReadExt;
28///
29/// struct MyApi;
30///
31/// #[OpenApi]
32/// impl MyApi {
33///     #[oai(path = "/upload", method = "post")]
34///     async fn upload_binary(&self, data: Binary<Vec<u8>>) -> Json<usize> {
35///         Json(data.len())
36///     }
37///
38///     #[oai(path = "/upload_stream", method = "post")]
39///     async fn upload_binary_stream(&self, data: Binary<Body>) -> Result<Json<usize>> {
40///         let mut reader = data.0.into_async_read();
41///         let mut bytes = Vec::new();
42///         reader.read_to_end(&mut bytes).await.map_err(BadRequest)?;
43///         Ok(Json(bytes.len()))
44///     }
45/// }
46///
47/// let api = OpenApiService::new(MyApi, "Demo", "0.1.0");
48/// let cli = TestClient::new(api);
49///
50/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
51/// let resp = cli
52///     .post("/upload")
53///     .content_type("application/octet-stream")
54///     .body("abcdef")
55///     .send()
56///     .await;
57/// resp.assert_status_is_ok();
58/// resp.assert_text("6").await;
59///
60/// let resp = cli
61///     .post("/upload_stream")
62///     .content_type("application/octet-stream")
63///     .body("abcdef")
64///     .send()
65///     .await;
66/// resp.assert_status_is_ok();
67/// resp.assert_text("6").await;
68/// # });
69/// ```
70#[derive(Debug, Clone, Eq, PartialEq)]
71pub struct Binary<T>(pub T);
72
73impl<T> Deref for Binary<T> {
74    type Target = T;
75
76    fn deref(&self) -> &Self::Target {
77        &self.0
78    }
79}
80
81impl<T> DerefMut for Binary<T> {
82    fn deref_mut(&mut self) -> &mut Self::Target {
83        &mut self.0
84    }
85}
86
87impl<T: Send> Payload for Binary<T> {
88    const CONTENT_TYPE: &'static str = "application/octet-stream";
89
90    fn check_content_type(_content_type: &str) -> bool {
91        true
92    }
93
94    fn schema_ref() -> MetaSchemaRef {
95        MetaSchemaRef::Inline(Box::new(MetaSchema {
96            format: Some("binary"),
97            ..MetaSchema::new("string")
98        }))
99    }
100}
101
102impl ParsePayload for Binary<Vec<u8>> {
103    const IS_REQUIRED: bool = true;
104
105    async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
106        Ok(Self(<Vec<u8>>::from_request(request, body).await?))
107    }
108}
109
110impl ParsePayload for Binary<Bytes> {
111    const IS_REQUIRED: bool = true;
112
113    async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
114        Ok(Self(Bytes::from_request(request, body).await?))
115    }
116}
117
118impl ParsePayload for Binary<Body> {
119    const IS_REQUIRED: bool = true;
120
121    async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
122        Ok(Self(Body::from_request(request, body).await?))
123    }
124}
125
126impl<T: Into<Body> + Send> IntoResponse for Binary<T> {
127    fn into_response(self) -> Response {
128        Response::builder()
129            .content_type(Self::CONTENT_TYPE)
130            .body(self.0.into())
131    }
132}
133
134impl<T: Into<Body> + Send> ApiResponse for Binary<T> {
135    fn meta() -> MetaResponses {
136        MetaResponses {
137            responses: vec![MetaResponse {
138                description: "",
139                status: Some(200),
140                status_range: None,
141                content: vec![MetaMediaType {
142                    content_type: Self::CONTENT_TYPE,
143                    schema: Self::schema_ref(),
144                }],
145                headers: vec![],
146            }],
147        }
148    }
149
150    fn register(_registry: &mut Registry) {}
151}
152
153impl_apirequest_for_payload!(Binary<Vec<u8>>);
154impl_apirequest_for_payload!(Binary<Bytes>);
155impl_apirequest_for_payload!(Binary<Body>);