-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_post.py
More file actions
46 lines (35 loc) · 1.49 KB
/
echo_post.py
File metadata and controls
46 lines (35 loc) · 1.49 KB
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
from http import HTTPStatus
from typing import Any, Dict, List, Optional
from pydantic import BaseModel, Field
from api.routes import Api
from api.schema import ApiTag, DefaultErrorResponse
class EchoPostRequest(BaseModel):
body: Dict[str, Any] = Field(description="This body will be echoed back.")
class EchoPostQuery(BaseModel):
time: Optional[int] = Field(description="Amount of body will be echoed back.")
class EchoPostResponse(BaseModel):
body: Dict[str, Any] = Field(description="This body will be echoed back.")
class EchoPostResponseList(BaseModel):
body: List[EchoPostResponse] = Field(description="This body will be echoed back.")
class EchoPostRoute(Api):
def register(self, app):
@app.post(
"/echo",
description="Echo the request body back.",
tags=[ApiTag.Echo],
responses={
HTTPStatus.OK: EchoPostResponseList,
HTTPStatus.INTERNAL_SERVER_ERROR: DefaultErrorResponse,
},
)
def echo(body: EchoPostRequest, query: EchoPostQuery):
try:
time = 1 if query.time is None else query.time
return [
EchoPostResponse(body=body.body).model_dump() for i in range(time)
], HTTPStatus.OK
except Exception as e:
return (
DefaultErrorResponse(stacktrace=e.__str__()).model_dump(),
HTTPStatus.INTERNAL_SERVER_ERROR,
)