-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsample.py
More file actions
78 lines (60 loc) · 1.7 KB
/
Copy pathsample.py
File metadata and controls
78 lines (60 loc) · 1.7 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
from typing import Optional
from ellar.common import Body, Controller, Inject, Module, ModuleRouter, put, ws_route
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}",
)
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: Body[int, Body.P(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: Inject[WebSocket]):
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"}
@Module(
controllers=(SampleController,),
routers=(mr,),
providers=(
UserService,
ProviderConfig(AnotherUserService, use_value=AnotherUserService()),
),
name="ModuleBaseExample",
)
class ModuleBaseExample(ModuleBase):
pass