Skip to content

Commit 6aeb432

Browse files
committed
feat/fix/tests: improve tests, add depends injection
1 parent 532e2c7 commit 6aeb432

File tree

16 files changed

+244
-416
lines changed

16 files changed

+244
-416
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,4 @@ ignored/
165165
*.db
166166
*.log
167167
translatedocs.py
168+
*/*.log

Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ PROJECT_NAME = "pyEchoNext"
4848
# could be handy for archiving the generated documentation or if some version
4949
# control system is used.
5050

51-
PROJECT_NUMBER = "0.7.12"
51+
PROJECT_NUMBER = "0.7.13"
5252

5353
# Using the PROJECT_BRIEF tag one can provide an optional one line description
5454
# for a project that appears at the top of each page and should give viewer a

README.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Welcome to **EchoNext**, where innovation meets simplicity! Are you tired of the
4545

4646
**Imagine** a lightweight framework that empowers you to create modern web applications with lightning speed and flexibility. With EchoNext, you're not just coding; you're building a masterpiece!
4747

48-
> Last stable version: 0.7.12 alpha
48+
> Last stable version: 0.7.13 alpha
4949
5050
> Next Big Update: ASYNC & unicorn support
5151
@@ -139,9 +139,95 @@ Once installed, you can start using the library in your Python projects. Check o
139139

140140
<p align="right">(<a href="#readme-top">back to top</a>)</p>
141141

142+
## ⚙️ Depends Injection
143+
pyEchoNext is universal, and you are free to use any Dependency-Injection framework. But we recommend using the specially developed [echonextdi](https://github.com/alexeev-prog/echonext_di). It is simple and fast to use.
144+
145+
> echonext_di goes in echonext dependencies
146+
147+
```python
148+
from echonextdi.containers.container import Container
149+
from echonextdi.depends import Depends
150+
from echonextdi.providers.callable_provider import CallableProvider
151+
152+
153+
def sqrt(a: int, b: int = 2):
154+
return a**b
155+
156+
157+
class SQRT_Dependency:
158+
def __init__(self, sqrt):
159+
self.sqrt = sqrt
160+
161+
162+
container = Container()
163+
container.register("sqrt", CallableProvider(sqrt))
164+
165+
166+
def calculate(number: int, depend: Depends = Depends(container, SQRT_Dependency)):
167+
print(f"{number} ^2 = {depend().sqrt(2)}")
168+
169+
170+
calculate(4) # Output: 16
171+
```
172+
142173
## 💻 Usage Examples
143174
You can view examples at [examples directory](./examples).
144175

176+
### Basic With Depends Injection
177+
178+
```python
179+
import os
180+
181+
from pyechonext.app import ApplicationType, EchoNext
182+
from pyechonext.config import Settings
183+
from pyechonext.middleware import middlewares
184+
from pyechonext.mvc.controllers import PageController
185+
from pyechonext.urls import URL
186+
187+
from echonextdi.containers.container import Container
188+
from echonextdi.depends import Depends
189+
from echonextdi.providers.callable_provider import CallableProvider
190+
191+
192+
class IndexController(PageController):
193+
def get(self, request, response, **kwargs):
194+
return "Hello"
195+
196+
def post(self, request, response, **kwargs):
197+
return "Hello"
198+
199+
200+
def say_hello(name: str, phrase: str = 'Hello'):
201+
return f'{phrase} {name}'
202+
203+
204+
class Hello_Dependency:
205+
def __init__(self, say_hello):
206+
self.say_hello = say_hello
207+
208+
209+
container = Container()
210+
container.register("say_hello", CallableProvider(say_hello))
211+
212+
url_patterns = [URL(path="/", controller=IndexController)]
213+
settings = Settings(
214+
BASE_DIR=os.path.dirname(os.path.abspath(__file__)), TEMPLATES_DIR="templates"
215+
)
216+
echonext = EchoNext(
217+
__name__,
218+
settings,
219+
middlewares,
220+
urls=url_patterns,
221+
application_type=ApplicationType.HTML,
222+
)
223+
224+
225+
@echonext.route_page("/hello/{name}")
226+
def hello(request, response, name: str = "World", depend: Depends = Depends(container, Hello_Dependency)):
227+
response.body = depend().say_hello(name)
228+
229+
```
230+
145231
### Performance caching
146232

147233
```python
@@ -594,7 +680,7 @@ To test the web framework, PyTest with the pytest-cov plugin is used. You can lo
594680

595681
| Statements | Miss | Coverage |
596682
|------------|------------|----------|
597-
| 1548 | 721 | 53% |
683+
| 1553 | 720 | 54% |
598684

599685
## Documentation 🌍
600686
Extended documentation and framework specifications are available at the following links:

SECURITY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ currently being supported with security updates.
77

88
| Version | Supported |
99
| ------- | ------------------ |
10+
| 0.7.13 | :white_check_mark: |
1011
| 0.7.12 | :white_check_mark: |
1112
| 0.7.11 | :white_check_mark: |
1213
| 0.6.11 | :white_check_mark: |

examples/modern.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
from pyechonext.mvc.controllers import PageController
77
from pyechonext.urls import URL
88

9+
from echonextdi.containers.container import Container
10+
from echonextdi.depends import Depends
11+
from echonextdi.providers.callable_provider import CallableProvider
12+
913

1014
class IndexController(PageController):
1115
def get(self, request, response, **kwargs):
@@ -15,6 +19,18 @@ def post(self, request, response, **kwargs):
1519
return "Hello"
1620

1721

22+
def say_hello(name: str, phrase: str = 'Hello'):
23+
return f'{phrase} {name}'
24+
25+
26+
class Hello_Dependency:
27+
def __init__(self, say_hello):
28+
self.say_hello = say_hello
29+
30+
31+
container = Container()
32+
container.register("say_hello", CallableProvider(say_hello))
33+
1834
url_patterns = [URL(path="/", controller=IndexController)]
1935
settings = Settings(
2036
BASE_DIR=os.path.dirname(os.path.abspath(__file__)), TEMPLATES_DIR="templates"
@@ -29,5 +45,5 @@ def post(self, request, response, **kwargs):
2945

3046

3147
@echonext.route_page("/hello/{name}")
32-
def hello(request, response, name: str = "World"):
33-
response.body = f"Hello {name}!"
48+
def hello(request, response, name: str = "World", depend: Depends = Depends(container, Hello_Dependency)):
49+
response.body = depend().say_hello(name)

pyechonext/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from rich import print
2323
from rich.traceback import install
2424

25-
__version__ = "0.7.12"
25+
__version__ = "0.7.13"
2626
install(show_locals=True)
2727

2828

pyechonext/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def _check_handler(self, request: Request, handler: Callable) -> Callable:
329329

330330
return handler
331331

332-
def _filling_response(self, route: Route, response: Response, handler: Callable):
332+
def _filling_response(self, route: Route, response: Response, request: Request, result: Any, handler: Callable):
333333
"""
334334
Filling response
335335
@@ -376,7 +376,7 @@ def _handle_request(self, request: Request) -> Response:
376376
elif result is None:
377377
return response
378378

379-
self._filling_response(route, response, handler)
379+
self._filling_response(route, response, request, result, handler)
380380
else:
381381
raise URLNotFound(f'URL "{request.path}" not found.')
382382

0 commit comments

Comments
 (0)