Skip to content

Commit 7a8d24c

Browse files
committed
refactored examples
1 parent a1ca63d commit 7a8d24c

34 files changed

+193
-189
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1-
from catapp.application.cats.services import CatService
2-
from ellar.common import Controller, Get, Put, Post, Delete, version, Render, Ctx
1+
from ellar.common import Controller, Ctx, delete, get, put, render, version
2+
3+
from .services import CatService
34

45

56
@Controller
67
class CatController:
78
def __init__(self, cat_service: CatService) -> None:
89
self.cat_service = cat_service
910

10-
@Get("/create")
11+
@get("/create")
1112
async def create_cat(self):
1213
return self.cat_service.create_cat()
1314

14-
@Put("/{cat_id:int}")
15+
@put("/{cat_id:int}")
1516
async def update_cat(self, cat_id: int):
1617
return self.cat_service.update_cat(cat_id)
1718

18-
@Delete("/{cat_id:int}")
19+
@delete("/{cat_id:int}")
1920
async def deleted_cat(self, cat_id: int):
2021
return self.cat_service.deleted_cat(cat_id)
2122

22-
@Get("/")
23-
@Render()
23+
@get("/")
24+
@render()
2425
async def list(self, ctx=Ctx()):
2526
return self.cat_service.list_cat()
2627

27-
@Get("/create", name='v2_create')
28+
@get("/create", name='v2_create')
2829
@version('v2')
2930
async def create_cat_v2(self):
3031
return self.cat_service.create_cat()

examples/catapp/application/cats/module.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
from ellar.common import Module
12
from ellar.core.modules import ModuleBase
23

3-
from catapp.application.cats.controllers import CatController
4-
from catapp.application.cats.routers import cat_router
5-
from catapp.application.cats.services import CatService, AnotherService
6-
from ellar.common import Module
4+
from .controllers import CatController
5+
from .routers import cat_router
6+
from .services import AnotherService, CatService
77

88

99
@Module(
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from typing import List
21
from dataclasses import dataclass
2+
from typing import List
33

4-
from catapp.application.cats.services import CatService
5-
from ellar.serializer import DataclassSerializer
4+
from ellar.common import Provide, render, Req
65
from ellar.core.routing import ModuleRouter
7-
from ellar.common import Provide, Req, Render
86
from ellar.core.templating import render_template
7+
from ellar.serializer import DataclassSerializer
8+
9+
from .services import CatService
910

1011
cat_router = ModuleRouter("/cats-router", tag='Testing')
1112

@@ -15,17 +16,17 @@ class CatObject(DataclassSerializer):
1516
name: str
1617

1718

18-
@cat_router.Get(response={200: List[CatObject]})
19+
@cat_router.get(response={200: List[CatObject]})
1920
async def get_cats(request=Req(), service: CatService = Provide()):
2021
return service.list_cat()
2122

2223

23-
@cat_router.HttpRoute('/html', methods=['get', 'post'])
24-
@Render("index.html")
24+
@cat_router.http_route('/html', methods=['get', 'post'])
25+
@render("index.html")
2526
async def get_cat_help(service: CatService = Provide()):
2627
return service.list_cat()
2728

2829

29-
@cat_router.Get('/html/outside')
30+
@cat_router.get('/html/outside')
3031
async def get_outside(request, service: CatService = Provide()):
3132
return render_template("index.html", request=request, context=service.list_cat())

examples/catapp/application/module.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from catapp.application.cats.module import CatModule
2-
from ellar.common import ApplicationModule
1+
from ellar.common import Module
32

3+
from ..application.cats.module import CatModule
44

5-
@ApplicationModule(
5+
6+
@Module(
67
modules=(CatModule, )
78
)
89
class AppModuleTest:

examples/catapp/server.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import os
22

3-
from catapp.application.module import AppModuleTest
43
from ellar.core.factory import AppFactory
54
from ellar.openapi.builder import OpenAPIDocumentBuilder
65
from ellar.openapi.module import OpenAPIDocumentModule
76

8-
os.environ.setdefault('ELLAR_CONFIG_MODULE', 'app_module_test.settings')
7+
from .application.module import AppModuleTest
8+
9+
os.environ.setdefault('ELLAR_CONFIG_MODULE', 'catapp.settings')
910

1011
app = AppFactory.create_from_app_module(AppModuleTest)
1112

1213
document_builder = OpenAPIDocumentBuilder()
13-
document_builder.set_title('Mirabel Title')\
14+
document_builder.set_title('Cat Application')\
1415
.set_version('1.0.2')\
1516
.set_contact(name='Eadwin', url='https://www.yahoo.com', email='[email protected]')\
1617
.set_license('MIT Licence', url='https://www.google.com')
1718

1819
document = document_builder.build_document(app)
19-
module = app.install_module(OpenAPIDocumentModule, app=app, document=document)
20+
module = app.install_module(OpenAPIDocumentModule, document=document)
2021
module.setup_redocs()
2122
module.setup_swagger_doc()

examples/catapp/tests/test_application.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os
2-
import pytest
3-
42
from pathlib import Path
5-
from catapp.application.cats.routers import cat_router
63

4+
import pytest
75
from ellar.core.testclient import TestClientFactory
86

7+
from catapp.application.cats.routers import cat_router
8+
99
os.environ.setdefault('ELLAR_CONFIG_MODULE', 'app_module_test.tests.settings')
1010

1111
BASEDIR = Path(__file__).resolve().parent.parent

tests/main.py

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,195 +10,195 @@
1010
router = ModuleRouter("")
1111

1212

13-
@router.Get("/root")
13+
@router.get("/root")
1414
def non_operation():
1515
return {"message": "Hello World"}
1616

1717

18-
@router.Get("/text")
18+
@router.get("/text")
1919
def get_text():
2020
return "Hello World"
2121

2222

23-
@router.Get("/path/{item_id}")
23+
@router.get("/path/{item_id}")
2424
def get_id(item_id):
2525
return item_id
2626

2727

28-
@router.Get("/path/str/{item_id}")
28+
@router.get("/path/str/{item_id}")
2929
def get_str_id(item_id: str):
3030
return item_id
3131

3232

33-
@router.Get("/path/int/{item_id}")
33+
@router.get("/path/int/{item_id}")
3434
def get_int_id(item_id: int):
3535
return item_id
3636

3737

38-
@router.Get("/path/float/{item_id}")
38+
@router.get("/path/float/{item_id}")
3939
def get_float_id(item_id: float):
4040
return item_id
4141

4242

43-
@router.Get("/path/bool/{item_id}")
43+
@router.get("/path/bool/{item_id}")
4444
def get_bool_id(item_id: bool):
4545
return item_id
4646

4747

48-
@router.Get("/path/param/{item_id}")
48+
@router.get("/path/param/{item_id}")
4949
def get_path_param_id(item_id: Optional[str] = Path(None)):
5050
return item_id
5151

5252

53-
@router.Get("/path/param-required/{item_id}")
53+
@router.get("/path/param-required/{item_id}")
5454
def get_path_param_required_id(item_id: str = Path(...)):
5555
return item_id
5656

5757

58-
@router.Get("/path/param-minlength/{item_id}")
58+
@router.get("/path/param-minlength/{item_id}")
5959
def get_path_param_min_length(item_id: str = Path(..., min_length=3)):
6060
return item_id
6161

6262

63-
@router.Get("/path/param-maxlength/{item_id}")
63+
@router.get("/path/param-maxlength/{item_id}")
6464
def get_path_param_max_length(item_id: str = Path(..., max_length=3)):
6565
return item_id
6666

6767

68-
@router.Get("/path/param-min_maxlength/{item_id}")
68+
@router.get("/path/param-min_maxlength/{item_id}")
6969
def get_path_param_min_max_length(item_id: str = Path(..., max_length=3, min_length=2)):
7070
return item_id
7171

7272

73-
@router.Get("/path/param-gt/{item_id}")
73+
@router.get("/path/param-gt/{item_id}")
7474
def get_path_param_gt(item_id: float = Path(..., gt=3)):
7575
return item_id
7676

7777

78-
@router.Get("/path/param-gt0/{item_id}")
78+
@router.get("/path/param-gt0/{item_id}")
7979
def get_path_param_gt0(item_id: float = Path(..., gt=0)):
8080
return item_id
8181

8282

83-
@router.Get("/path/param-ge/{item_id}")
83+
@router.get("/path/param-ge/{item_id}")
8484
def get_path_param_ge(item_id: float = Path(..., ge=3)):
8585
return item_id
8686

8787

88-
@router.Get("/path/param-lt/{item_id}")
88+
@router.get("/path/param-lt/{item_id}")
8989
def get_path_param_lt(item_id: float = Path(..., lt=3)):
9090
return item_id
9191

9292

93-
@router.Get("/path/param-lt0/{item_id}")
93+
@router.get("/path/param-lt0/{item_id}")
9494
def get_path_param_lt0(item_id: float = Path(..., lt=0)):
9595
return item_id
9696

9797

98-
@router.Get("/path/param-le/{item_id}")
98+
@router.get("/path/param-le/{item_id}")
9999
def get_path_param_le(item_id: float = Path(..., le=3)):
100100
return item_id
101101

102102

103-
@router.Get("/path/param-lt-gt/{item_id}")
103+
@router.get("/path/param-lt-gt/{item_id}")
104104
def get_path_param_lt_gt(item_id: float = Path(..., lt=3, gt=1)):
105105
return item_id
106106

107107

108-
@router.Get("/path/param-le-ge/{item_id}")
108+
@router.get("/path/param-le-ge/{item_id}")
109109
def get_path_param_le_ge(item_id: float = Path(..., le=3, ge=1)):
110110
return item_id
111111

112112

113-
@router.Get("/path/param-lt-int/{item_id}")
113+
@router.get("/path/param-lt-int/{item_id}")
114114
def get_path_param_lt_int(item_id: int = Path(..., lt=3)):
115115
return item_id
116116

117117

118-
@router.Get("/path/param-gt-int/{item_id}")
118+
@router.get("/path/param-gt-int/{item_id}")
119119
def get_path_param_gt_int(item_id: int = Path(..., gt=3)):
120120
return item_id
121121

122122

123-
@router.Get("/path/param-le-int/{item_id}")
123+
@router.get("/path/param-le-int/{item_id}")
124124
def get_path_param_le_int(item_id: int = Path(..., le=3)):
125125
return item_id
126126

127127

128-
@router.Get("/path/param-ge-int/{item_id}")
128+
@router.get("/path/param-ge-int/{item_id}")
129129
def get_path_param_ge_int(item_id: int = Path(..., ge=3)):
130130
return item_id
131131

132132

133-
@router.Get("/path/param-lt-gt-int/{item_id}")
133+
@router.get("/path/param-lt-gt-int/{item_id}")
134134
def get_path_param_lt_gt_int(item_id: int = Path(..., lt=3, gt=1)):
135135
return item_id
136136

137137

138-
@router.Get("/path/param-le-ge-int/{item_id}")
138+
@router.get("/path/param-le-ge-int/{item_id}")
139139
def get_path_param_le_ge_int(item_id: int = Path(..., le=3, ge=1)):
140140
return item_id
141141

142142

143-
@router.Get("/path/param-starlette-str/{item_id:str}")
143+
@router.get("/path/param-starlette-str/{item_id:str}")
144144
def get_path_param_starlette_str(item_id):
145145
return item_id
146146

147147

148-
@router.Get("/path/param-starlette-int/{item_id:int}")
148+
@router.get("/path/param-starlette-int/{item_id:int}")
149149
def get_path_param_starlette_int(item_id: int):
150150
assert isinstance(item_id, int)
151151
return item_id
152152

153153

154-
@router.Get("/path/param-starlette-int-str/{item_id:int}")
154+
@router.get("/path/param-starlette-int-str/{item_id:int}")
155155
def get_path_param_starlette_int_str(item_id: str):
156156
assert isinstance(item_id, str)
157157
return item_id
158158

159159

160-
@router.Get("/path/param-starlette-uuid/{item_id:uuid}")
160+
@router.get("/path/param-starlette-uuid/{item_id:uuid}")
161161
def get_path_param_starlette_uuid(item_id: UUID):
162162
assert isinstance(item_id, UUID)
163163
return item_id
164164

165165

166-
@router.Get("/query")
166+
@router.get("/query")
167167
def get_query(query):
168168
return f"foo bar {query}"
169169

170170

171-
@router.Get("/query/optional")
171+
@router.get("/query/optional")
172172
def get_query_optional(query=None):
173173
if query is None:
174174
return "foo bar"
175175
return f"foo bar {query}"
176176

177177

178-
@router.Get("/query/int")
178+
@router.get("/query/int")
179179
def get_query_type(query: int):
180180
return f"foo bar {query}"
181181

182182

183-
@router.Get("/query/int/optional")
183+
@router.get("/query/int/optional")
184184
def get_query_type_optional(query: int = None):
185185
if query is None:
186186
return "foo bar"
187187
return f"foo bar {query}"
188188

189189

190-
@router.Get("/query/int/default")
190+
@router.get("/query/int/default")
191191
def get_query_type_optional_10(query: int = 10):
192192
return f"foo bar {query}"
193193

194194

195-
@router.Get("/query/param-required")
195+
@router.get("/query/param-required")
196196
def get_query_param_required(query=Query(...)):
197197
assert isinstance(query, str)
198198
return f"foo bar {query}"
199199

200200

201-
@router.Get("/query/param-required/int")
201+
@router.get("/query/param-required/int")
202202
def get_query_param_required_type(query: int = Query(...)):
203203
assert isinstance(query, int)
204204
return f"foo bar {query}"
@@ -208,12 +208,12 @@ class AliasedSchema(Schema):
208208
query: str = Field(..., alias="aliased.-_~name")
209209

210210

211-
@router.Get("/query/aliased-name")
211+
@router.get("/query/aliased-name")
212212
def get_query_aliased_name(query: AliasedSchema = Query(..., alias="aliased.-_~name")):
213213
return f"foo bar {query.query}"
214214

215215

216-
@router.Get("/query/param")
216+
@router.get("/query/param")
217217
def get_query_param(request, query=Query(None)):
218218
if query is None:
219219
return "foo bar"

0 commit comments

Comments
 (0)