You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Also, since `RoleGuard` is marked as `injectable`, EllarInjector service will be able to resolve `RoleGuard` without `RoleGuard` registered as a provider.
Copy file name to clipboardExpand all lines: docs/index.md
+13-12Lines changed: 13 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -15,28 +15,29 @@
15
15
Ellar is a lightweight ASGI framework for building efficient and scalable server-side python applications.
16
16
It supports both OOP (Object-Oriented Programming) and FP (Functional Programming)
17
17
18
-
19
-
Ellar is built around [Starlette (ASGI toolkit)](https://www.starlette.io/) which processes all the HTTP requests and background tasks. Although, there is a high level
20
-
of abstraction, some concepts of Starlette are still supported.
18
+
Ellar is based on [Starlette (ASGI toolkit)](https://www.starlette.io/), a lightweight ASGI framework/toolkit well-suited for developing asynchronous web services in Python.
19
+
And while Ellar provides a high level of abstraction on top of Starlette, it still incorporates some of its features, as well as those of FastAPI.
20
+
If you are familiar with these frameworks, you will find it easy to understand and use Ellar.
21
21
22
22
## Inspiration
23
-
Ellar was heavily inspired by [NestJS](https://docs.nestjs.com/) in its simplicity in usage while managing complex project structures and applications.
24
-
It also adopted some concepts of [FastAPI](https://fastapi.tiangolo.com/) in handling request parameters and data serialization with pydantic.
25
-
With that said, the aim of Ellar focuses on a high level of abstraction of framework APIs, project structures, architectures, and speed of handling requests.
23
+
Ellar was deeply influenced by [NestJS](https://docs.nestjs.com/) for its ease of use and ability to handle complex project structures and applications.
24
+
Additionally, it took some concepts from [FastAPI](https://fastapi.tiangolo.com/) in terms of request parameter handling and data serialization with Pydantic.
25
+
26
+
With that said, the objective of Ellar is to offer a high level of abstraction in its framework APIs, along with a well-structured project setup, an object-oriented approach to web application design,
27
+
the ability to adapt to any desired software architecture, and ultimately, speedy request handling.
26
28
27
29
## Installation
28
30
To get started, you need to scaffold a project using [Ellar-CLI](https://eadwincode.github.io/ellar-cli/) toolkit. This is recommended for a first-time user.
29
31
The scaffolded project is more like a guide to project setup.
30
32
31
33
```shell
32
-
$(venv) pip install ellar[standard]
33
-
$(venv) ellar new project-name
34
+
$(venv) pip install ellar
34
35
```
35
36
36
-
### NB:
37
-
Some shells may treat square braces (`[` and `]`) as special characters. If that's the case here, then use a quote around the characters to prevent unexpected shell expansion.
37
+
After that, lets create a new project.
38
+
Run the command below and change the `project-name` with whatever name you decide.
38
39
```shell
39
-
pip install "ellar[standard]"
40
+
$(venv)ellar new project-name
40
41
```
41
42
42
43
then, start the app with:
@@ -60,7 +61,7 @@ Open your browser and navigate to [`http://localhost:8000/`](http://localhost:80
Copy file name to clipboardExpand all lines: docs/overview/custom_decorators.md
+24-16Lines changed: 24 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
2
2
Ellar provides a variety of function decorators in the `ellar.common` python module that can be used to modify the behavior of route functions.
3
+
3
4
These decorators can be used to change the response type of a route function, add filters to the response schema, define the OPENAPI context, and more.
4
5
In general, these decorators can help to simplify and streamline the process of creating routes.
We discussed decorators that are used to define route function parameter dependencies in Ellar.
59
60
These decorators, such as `Query`, `Form`, and `Body`, etc. are pydantic models used to specify the expected parameters for a route function.
61
+
60
62
However, there are also some route parameters that are **system** dependent, such as the `request` or `websocket` object, and the `response` object.
61
63
These parameters are resolved by the application and supplied to the route function when needed, and are not specified with pydantic models or user input.
62
64
63
65
### Provide(Type)
64
66
The **Provide(Type)** decorator is used to resolve a service provider and inject it into a route function parameter.
65
67
This can be useful when using the ModuleRouter feature in Ellar.
68
+
66
69
It allows for easy injection of services into route functions, making it easier to manage dependencies and improve code organization.
67
70
This can be useful for resolving database connections, external APIs, or other resources that are used by the route function.
In this example, the example_endpoint function is decorated with the **Context()** decorator, which injects the current `IExecutionContext` object into the `ctx` parameter of the function.
110
113
The `IExecutionContext` object provides access to various resources and information related to the current execution context, such as the current HTTP connection, query parameters, and more.
114
+
111
115
In this example, the `switch_to_http_connection()` method is used to access the current HTTP connection and the `get_client()` method is used to get the client object for the connection.
112
116
The `query_params` attribute of the client object is then accessed and included in the response returned by the endpoint.
The above code creates a WebSocket route '/test-ws' and when a client connects to this route,
168
172
the `example_endpoint` function is executed. The `Ws` decorator injects the current `WebSocket` object to the `ws` parameter of the function, which can then be used to interact with the WebSocket connection, such as accepting the connection and sending data to the client.
169
173
170
-
### Host
171
-
**Host()** decorator injects current client host address to route function parameter.
172
-
173
-
### Session
174
-
**Session()** decorator injects current Session object to route function parameter.
174
+
The same conditions and examples applies for:
175
175
176
-
### Http
177
-
**Http()** decorator injects current HTTP connection object to route function parameter.
176
+
-**Host()** decorator injects current client host address to route function parameter.
177
+
-**Session()** decorator injects current Session object to route function parameter. This requires [SessionMiddleware](https://www.starlette.io/middleware/#sessionmiddleware) module from Starlette added in application middleware and also `SessionMiddleware` module depends on [itsdangerous](https://pypi.org/project/itsdangerous/) package.
178
+
-**Http()** decorator injects current HTTP connection object to route function parameter.
178
179
179
180
## **Creating a Custom Parameter Decorators**
180
181
You can still create your own route parameter decorators that suits your need. You simply need to follow a contract, `NonParameterResolver`, and override the resolve function.
@@ -203,6 +204,7 @@ class UserParam(NonParameterResolver):
203
204
204
205
This example defines a custom decorator called `UserParam` that inherits from `NonParameterResolver`.
205
206
The `resolve` method is overridden to extract the user from the current `IExecutionContext`'s request.
207
+
206
208
If the user is found, it is returned as a dict with the key as the `parameter_name` of the decorator, along with an empty list of errors.
207
209
If no user is found, an empty dict and a list of errors containing an ErrorWrapper object is returned.
208
210
@@ -234,6 +236,7 @@ def index(self):
234
236
235
237
In the example, the index function is decorated with the `render` decorator,
236
238
which will return a 200 status code and HTML content from my_template.
239
+
237
240
The return object from the index function will be used as the templating context for `my_template` during the template rendering process.
238
241
This allows the function to pass data to the template and have it rendered with the provided context, the rendered template will be the response body.
239
242
@@ -379,15 +382,16 @@ See [Pydantic Model Export](https://docs.pydantic.dev/usage/exporting_models/#mo
379
382
### VERSION
380
383
**@version()** is a decorator that provides endpoint versioning for a route function.
381
384
This decorator allows you to specify the version of the endpoint that the function is associated with.
385
+
382
386
Based on the versioning scheme configuration in the application, versioned route functions are called. This can be useful for maintaining backward compatibility, or for rolling out new features to different versions of an application.
383
387
More information on how to use this decorator can be found in the [Versioning documentation]()
384
388
385
389
A quick example on how to use `version` decorator:
386
390
```python
387
-
from ellar.common import post, version
391
+
from ellar.common import post, Version
388
392
389
393
@post("/create", name='v2_v3_list')
390
-
@version('2', '3')
394
+
@Version('2', '3')
391
395
asyncdefget_item_v2_v3(self):
392
396
return {'message': 'for v2 and v3 request'}
393
397
```
@@ -397,16 +401,18 @@ This indicates that the `get_item_v2_v3` route function will handle version 2 an
397
401
This allows for multiple versions of the same endpoint to be handled by different route functions, each with their own logic and implementation.
398
402
399
403
### GUARDS
400
-
**@guards()** is a decorator that applies a protection class of type GuardCanActivate to a route function.
401
-
These protection classes have a can_execute function that is called to determine whether a route function should be executed.
402
-
This decorator allows you to apply certain conditions or checks before a route function is executed, such as authentication or authorization checks.
404
+
**@Guards()** is a decorator that applies a protection class of type `GuardCanActivate` to a route function.
405
+
These protection classes have a `can_execute` function that is called to determine whether a route function should be executed.
406
+
407
+
This decorator allows you to apply certain conditions or checks before a route function is executed, such as `authentication` or `authorization` checks.
403
408
This can help to ensure that only authorized users can access certain resources.
409
+
404
410
More information on how to use this decorator can be found in the [Guard Documentation]()
405
411
406
-
A quick example on how to use `guards` decorator:
412
+
A quick example on how to use `Guards` decorator:
407
413
```python
408
414
import typing as t
409
-
from ellar.common import get, guards
415
+
from ellar.common import get, Guards
410
416
from ellar.core.guard import APIKeyQuery
411
417
from ellar.core.connection import HTTPConnection
412
418
@@ -419,14 +425,16 @@ class MyAPIKeyQuery(APIKeyQuery):
419
425
420
426
421
427
@get("/")
422
-
@guards(MyAPIKeyQuery(), )
428
+
@Guards(MyAPIKeyQuery(), )
423
429
asyncdefget_guarded_items(self):
424
430
return {'message': 'worked fine with `key`=`supersecret`'}
425
431
```
426
-
The `guards` decorator, like the `version` decorator, takes a list of values as an argument.
432
+
The `Guards` decorator, like the `version` decorator, takes a list of values as an argument.
427
433
During a request, the provided guards are called in the order in which they are provided.
434
+
428
435
This allows you to apply multiple guards to a single route function and have them executed in a specific order.
429
436
This is useful for applying multiple levels of security or access control to a single endpoint.
437
+
430
438
Each guard class has a `can_execute` function that is called in the order specified by the decorator, if any of the guard's `can_execute` function returns False, the route function will not be executed.
0 commit comments