1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use std::ops::Deref;
use poem::{Error, FromRequest, Request, RequestBody, Result, Route};
use crate::registry::{
MetaApi, MetaOAuthScope, MetaParamIn, MetaRequest, MetaResponse, MetaResponses, MetaSchemaRef,
Registry,
};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ApiExtractorType {
RequestObject,
Parameter,
SecurityScheme,
PoemExtractor,
}
#[doc(hidden)]
pub struct UrlQuery(pub Vec<(String, String)>);
impl Deref for UrlQuery {
type Target = Vec<(String, String)>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl UrlQuery {
#[allow(missing_docs)]
pub fn get_all<'a, 'b: 'a>(&'b self, name: &'a str) -> impl Iterator<Item = &'b String> + 'a {
self.0
.iter()
.filter(move |(n, _)| n == name)
.map(|(_, value)| value)
}
#[allow(missing_docs)]
pub fn get(&self, name: &str) -> Option<&String> {
self.get_all(name).next()
}
}
pub struct ExtractParamOptions<T> {
pub name: &'static str,
pub default_value: Option<fn() -> T>,
}
impl<T> Default for ExtractParamOptions<T> {
fn default() -> Self {
Self {
name: "",
default_value: None,
}
}
}
#[poem::async_trait]
#[allow(unused_variables)]
pub trait ApiExtractor<'a>: Sized {
const TYPE: ApiExtractorType;
const PARAM_IS_REQUIRED: bool = false;
type ParamType;
type ParamRawType;
fn register(registry: &mut Registry) {}
fn security_scheme() -> Option<&'static str> {
None
}
fn param_in() -> Option<MetaParamIn> {
None
}
fn param_schema_ref() -> Option<MetaSchemaRef> {
None
}
fn request_meta() -> Option<MetaRequest> {
None
}
fn param_raw_type(&self) -> Option<&Self::ParamRawType> {
None
}
async fn from_request(
request: &'a Request,
body: &mut RequestBody,
param_opts: ExtractParamOptions<Self::ParamType>,
) -> Result<Self>;
}
#[poem::async_trait]
impl<'a, T: FromRequest<'a>> ApiExtractor<'a> for T {
const TYPE: ApiExtractorType = ApiExtractorType::PoemExtractor;
type ParamType = ();
type ParamRawType = ();
async fn from_request(
request: &'a Request,
body: &mut RequestBody,
_param_opts: ExtractParamOptions<Self::ParamType>,
) -> Result<Self> {
T::from_request(request, body).await
}
}
pub trait ApiResponse: Sized {
const BAD_REQUEST_HANDLER: bool = false;
fn meta() -> MetaResponses;
fn register(registry: &mut Registry);
#[allow(unused_variables)]
fn from_parse_request_error(err: Error) -> Self {
unreachable!()
}
}
impl ApiResponse for () {
fn meta() -> MetaResponses {
MetaResponses {
responses: vec![MetaResponse {
description: "",
status: Some(200),
content: vec![],
headers: vec![],
}],
}
}
fn register(_registry: &mut Registry) {}
}
impl<T: ApiResponse> ApiResponse for Result<T> {
const BAD_REQUEST_HANDLER: bool = T::BAD_REQUEST_HANDLER;
fn meta() -> MetaResponses {
T::meta()
}
fn register(registry: &mut Registry) {
T::register(registry);
}
fn from_parse_request_error(err: Error) -> Self {
Ok(T::from_parse_request_error(err))
}
}
pub trait Tags {
fn register(&self, registry: &mut Registry);
fn name(&self) -> &'static str;
}
pub trait OAuthScopes {
fn meta() -> Vec<MetaOAuthScope>;
fn name(&self) -> &'static str;
}
pub trait OpenApi: Sized {
fn meta() -> Vec<MetaApi>;
fn register(registry: &mut Registry);
fn add_routes(self, route: Route) -> Route;
fn combine<T: OpenApi>(self, other: T) -> CombinedAPI<Self, T> {
CombinedAPI(self, other)
}
}
pub struct CombinedAPI<A, B>(A, B);
impl<A: OpenApi, B: OpenApi> OpenApi for CombinedAPI<A, B> {
fn meta() -> Vec<MetaApi> {
let mut metadata = A::meta();
metadata.extend(B::meta());
metadata
}
fn register(registry: &mut Registry) {
A::register(registry);
B::register(registry);
}
fn add_routes(self, route: Route) -> Route {
self.1.add_routes(self.0.add_routes(route))
}
}