-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsample.py
More file actions
133 lines (104 loc) · 2.85 KB
/
Copy pathsample.py
File metadata and controls
133 lines (104 loc) · 2.85 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
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
from typing import Optional
from ellar.common import (
Body,
Controller,
Module,
ModuleRouter,
Ws,
exception_handler,
middleware,
on_shutdown,
on_startup,
put,
template_filter,
template_global,
ws_route,
)
from ellar.core import App, Config
from ellar.core.connection import WebSocket
from ellar.core.modules import ModuleBase
from ellar.di import ProviderConfig
from ..schema import Item, User
class UserService:
def __init__(self):
self.user = User(username="username", full_name="full_name")
class AnotherUserService(UserService):
pass
@Controller(
"/items/{orgID:int}",
tag="Item",
description="Sample Controller",
external_doc_url="https://test.com",
external_doc_description="Find out more here",
)
class SampleController:
def __init__(self, user_service: UserService):
self._user_service = user_service
@put("/{item_id:uuid}")
async def update_item(
self,
*,
item_id: int,
item: Item,
user: User,
importance: int = Body(gt=0),
q: Optional[str] = None
):
results = {
"item_id": item_id,
"item": item,
"user": user,
"importance": importance,
}
if q:
results.update({"q": q})
results.update(self._user_service.user)
return results
@ws_route("/websocket")
async def websocket_test(self, *, web_socket: WebSocket = Ws()):
await web_socket.accept()
await web_socket.send_json({"message": "Websocket okay"})
await web_socket.close()
mr = ModuleRouter("/mr")
@mr.get("/get")
def get_mr():
return {"get_mr", "OK"}
@mr.post("/post")
def post_mr():
return {"post_mr", "OK"}
class ModuleBaseExample(ModuleBase):
_before_init_called = False
_app_ready_called = False
@exception_handler(404)
async def exception_404(cls, request, exc):
pass
@middleware("http")
async def middleware(cls, request, call_next):
response = await call_next(request)
return response
@template_global()
def some_template_global(cls, n):
pass
@template_filter()
def some_template_filter(cls, n):
pass
@classmethod
def before_init(cls, config: Config) -> None:
cls._before_init_called = True
def application_ready(self, app: "App") -> None:
self.__class__._app_ready_called = True
@on_startup
async def on_startup_handler(cls):
pass
@on_shutdown
def on_shutdown_handler(cls):
pass
ModuleBaseExample2 = type("ModuleBaseExample2", (ModuleBaseExample,), {})
SampleModule = Module(
controllers=(SampleController,),
routers=(mr,),
providers=(
UserService,
ProviderConfig(AnotherUserService, use_value=AnotherUserService()),
),
)(ModuleBaseExample2)