Skip to content

Commit 743dd95

Browse files
authored
Merge pull request #99 from eadwinCode/config_with_prefix
Config with prefix
2 parents 3d97aba + bf72907 commit 743dd95

File tree

14 files changed

+136
-65
lines changed

14 files changed

+136
-65
lines changed

docs/configurations.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,48 @@ To allow any hostname either use `allowed_hosts=["*"]` or omit the middleware.
251251
Default: `True`
252252

253253
Indicates whether to append `www.` when redirecting host in `TrustedHostMiddleware`
254+
255+
256+
## **Configuration with prefix**
257+
Ellar configuration module also support loading of its configurations with appended prefix. for instance,
258+
we can have a file `my_settings.py` with some ellar's configurations set to it with some prefix `API_` as shown below.
259+
260+
```python
261+
# my_settings.py
262+
API_DEBUG = True
263+
API_SECRET_KEY = "your-secret-key-changed"
264+
API_INJECTOR_AUTO_BIND = True
265+
API_JINJA_TEMPLATES_OPTIONS = {"auto_reload": True}
266+
267+
OTHER_XYZ_CONFIGS_1 ='whatever'
268+
OTHER_XYZ_CONFIGS_2 ='whatever2'
269+
```
270+
To apply these configurations without having to load everything, you have to provide the prefix to be used to load configurations that
271+
belongs to ellar. For example,
272+
273+
```python
274+
from ellar.core.factory import AppFactory
275+
from .root_module import ApplicationModule
276+
277+
application = AppFactory.create_from_app_module(ApplicationModule, config_module=dict(
278+
config_module='project_name:my_settings',
279+
config_prefix='api_',
280+
))
281+
```
282+
In the above construct, we used a dict object to define the configuration module(`'project_name:my_settings'`) and prefix `api_`.
283+
This will be applied to the configuration instance when the application is ready.
284+
285+
## **Defining Configurations directly**
286+
During application bootstrapping with `AppFactory`, you can define app configurations directly under `config_module` as a dict object as some below.
287+
288+
```python
289+
from ellar.core.factory import AppFactory
290+
from .root_module import ApplicationModule
291+
292+
application = AppFactory.create_from_app_module(ApplicationModule, config_module=dict(
293+
SECRET_KEY = "your-secret-key-changed",
294+
INJECTOR_AUTO_BIND = True,
295+
MIDDLEWARE=[],
296+
EXCEPTION_HANDLERS=[]
297+
))
298+
```

docs/handling-response/response-model.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Response Model
1+
# **Response Models**
22

33
Each route handler has key-value pair of status codes and a response model.
44
This response model holds information on the type of response to be returned.
@@ -62,7 +62,7 @@ response = {200: JSONResponseModel(model_field_or_schema=UserSchema, description
6262
Each route handler has its own `ResponseModel` computation and validation. If there is no response definition, Ellar default the route handler model to `EmptyAPIResponseModel`.
6363

6464

65-
## Override Response Type
65+
## **Override Response Type**
6666

6767
When you use a `Response` class as response, a `ResponseModel` is used and the `response_type` is replaced with applied response class.
6868

@@ -96,7 +96,7 @@ from ellar.common import PlainTextResponse
9696
response = {200: ResponseModel(response_type=PlainTextResponse), 201: JSONResponseModel(model_field_or_schema=UserSchema)}
9797
```
9898

99-
## Response Model Properties
99+
## **Response Model Properties**
100100

101101
All response model follows `IResponseModel` contract.
102102

@@ -129,7 +129,7 @@ They include:
129129
- `model_field_or_schema`: `Optional` property. For return data validation. Default: `None` **Optional**
130130

131131

132-
## Different Response Models
132+
## **Different Response Models**
133133
Let's see different `ResponseModel` available in Ellar and how you can create one too.
134134

135135
### **ResponseModel**
@@ -183,7 +183,7 @@ Default `ResponseModel` applied when no response is defined.
183183
- model_field_or_schema: `dict`
184184
- media_type: `application/json`
185185

186-
## Custom Response Model
186+
## **Custom Response Model**
187187

188188
Lets create a new JSON response model.
189189

docs/handling-response/response.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Introduction
1+
## **Response and Serializers**
22
The `Serializer` class in the Ellar, is a custom class based on `pydantic` models, which provides additional functionality specific to Ellar's requirements.
33

44
To use `Serializer` in Ellar, you simply need to create a class that inherits from `Serializer` and define your data model using pydantic fields.
@@ -17,7 +17,7 @@ class UserSerializer(Serializer):
1717
With this setup, you can use the `UserSerializer` class to validate incoming data and or serialize outgoing response data,
1818
ensuring that it matches the expected format before saving it to the database or returning it to the client.
1919

20-
## Handling Responses
20+
## **Handling Responses**
2121

2222
Let's see how we can use **Serializer** as a responses schema which will help us validate out data output and also provide documentation on route function response.
2323

@@ -71,7 +71,7 @@ that can be easily serialized to JSON.
7171
The resulting dictionary is then passed to the [`JSONResponseModel`](./response-model/#jsonresponsemodel) for serialization to a
7272
JSON string and sending the response to the client.
7373

74-
## Using Dataclass as Response Schema
74+
## **Using Dataclass as Response Schema**
7575
We can utilize the `dataclasses` feature as a response schema by utilizing the `DataclassSerializer` a base class.
7676

7777
For instance, we can convert the `UserSchema` to a dataclass by defining `UserDataclass` as follows:
@@ -92,7 +92,7 @@ class UserDataclass(DataclassSerializer):
9292

9393
By replacing the `UserSchema` with `UserDataclass`, we can expect the same outcomes in the returned response, response validation, and documentation.
9494

95-
### Multiple Response Types
95+
### **Multiple Response Types**
9696

9797
The `response` parameter takes different shape. Let's see how to return a different response if the user is not authenticated.
9898

@@ -156,7 +156,7 @@ Here, the `response` parameter takes a KeyValuePair of the `status` and response
156156

157157

158158

159-
## Using Response Type/Object As Response
159+
## **Using Response Type/Object As Response**
160160

161161
You can use `Response` type to change the format of data returned from endpoint functions.
162162

docs/parsing-inputs/body.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Request Body
1+
# **Request Body**
22

33
Request bodies are typically used with “create” and “update” operations (POST, PUT, PATCH).
44
For example, when creating a resource using POST or PUT, the request body usually contains the representation of the resource to be created.
@@ -8,7 +8,7 @@ To declare a **request body**, you need to use **Ellar `Serializer`**.
88
!!! info
99
Under the hood **Ellar** uses <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a> models with all their power and benefits.
1010

11-
## Import Serializer
11+
## **Import Serializer**
1212

1313
First, you need to import `Serializer` from `ella.serializer`:
1414

@@ -24,7 +24,7 @@ from ellar.common import Serializer
2424

2525
```
2626

27-
## Create your data model
27+
## **Create your data model**
2828

2929
Then you declare your data model as a class that inherits from `Serializer`.
3030

@@ -71,7 +71,7 @@ For example, this model above declares a JSON "`object`" (or Python `dict`) like
7171
}
7272
```
7373

74-
## Declare it as a parameter
74+
## **Declare it as a parameter**
7575

7676
To add it to your *path operation*, declare it the same way you declared the path and query parameters:
7777

@@ -97,7 +97,7 @@ class ItemsController(ControllerBase):
9797

9898
... and declare its type as the model you created, `Item`.
9999

100-
## Results
100+
## **Results**
101101

102102
With just that Python type declaration, **Ellar** will:
103103

@@ -112,7 +112,7 @@ With just that Python type declaration, **Ellar** will:
112112
your models, and you can also use them anywhere else you like if it makes sense for your project.
113113
* Those schemas will be part of the generated OpenAPI schema, and used by the automatic documentation <abbr title="User Interfaces">UI's</abbr>.
114114

115-
## Automatic Docs
115+
## **Automatic Docs**
116116

117117
The JSON Schemas of your models will be part of your OpenAPI generated schema, and will be shown in the interactive API docs:
118118

@@ -123,7 +123,7 @@ The JSON Schemas of your models will be part of your OpenAPI generated schema, a
123123
![Openapi schema](../img/body-schema-doc2.png)
124124

125125

126-
## Request Body + Path parameters
126+
## **Request Body + Path parameters**
127127

128128
You can declare path parameters **and** body requests at the same time.
129129

@@ -152,7 +152,7 @@ class ItemsController(ControllerBase):
152152
return {"item_id": item_id, "item": item.dict()}
153153
```
154154

155-
## Request Body + Path + Query parameters
155+
## **Request Body + Path + Query parameters**
156156

157157
You can also declare **body**, **path** and **query** parameters, all at the same time.
158158

@@ -191,7 +191,7 @@ The function parameters will be recognized as follows:
191191
In here, we have combined both `Serializers` and `Controllers` in one file. This is for the convenience of writing this documentation.
192192
It's advised to have all your serializers in `schemas.py` and then import them over to `controllers.py` if needed.
193193

194-
## Singular values in body
194+
## **Singular values in body**
195195
The same way there is a `Query` and `Path` to define extra data for query and path parameters,
196196
**Ellar** provides an equivalent `Body`.
197197

@@ -253,7 +253,7 @@ In this case, **Ellar** will expect a body like:
253253
}
254254
```
255255

256-
## Multiple body params and query
256+
## **Multiple body params and query**
257257
Of course, you can also declare additional `query` parameters whenever you need, additional to anybody parameters.
258258

259259
As, by default, singular values are interpreted as query parameters, you don't have to explicitly add a `Query`, you can just do:

docs/parsing-inputs/cookie-params.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Cookie Parameters
1+
# **Cookie Parameters**
22

33
You can define Cookie parameters the same way you define `Path` parameters.
44

5-
## Import `Cookie`
5+
## **Import `Cookie`**
66

77
First import `Cookie` from `ellar.common` module
88

9-
## Declare `Cookie` parameters
9+
## **Declare `Cookie` parameters**
1010

1111
Then declare the cookie parameters using the same structure as with `Path` and `Query`.
1212

@@ -29,7 +29,7 @@ class ItemsController(ControllerBase):
2929
!!! info
3030
To declare cookies, you need to use `Cookie`, because otherwise the parameters would be interpreted as `query` parameters.
3131

32-
### Using Schema
32+
## **Using Schema**
3333

3434
You can also use Schema to encapsulate `Cookies` parameters:
3535

docs/parsing-inputs/file-params.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# File uploads
1+
# **File Uploads**
22

33
Handling files are no different from other parameters.
44
You can define files to be uploaded by using `File`.
@@ -10,11 +10,11 @@ You can define files to be uploaded by using `File`.
1010

1111
This is because uploaded files are sent as "form data".
1212

13-
## Import `File`
13+
## **Import `File`**
1414

1515
First import `File` from `ellar.common` module
1616

17-
## Define `File` parameters
17+
## **Define `File` parameters**
1818

1919
Create file parameters the same way you would for `Body` or `Form`:
2020

@@ -38,7 +38,7 @@ Have in mind that this means that the whole contents will be stored in memory. T
3838

3939
But there are several cases in which you might benefit from using `UploadFile`.
4040

41-
## `File` parameters with `UploadFile`
41+
## **`File` parameters with `UploadFile`**
4242

4343
Define a `File` parameter with a type of `UploadFile`:
4444

@@ -106,7 +106,7 @@ contents = myfile.file.read()
106106
**Ellar**'s `UploadFile` inherits directly from **Starlette**'s `UploadFile`, but adds some necessary parts to make it compatible with **Pydantic** and the other parts of Ellar.
107107

108108

109-
## Uploading array of files
109+
## **Uploading array of files**
110110

111111
To **upload several files** at the same time, just declare a `List` of `UploadFile`:
112112

@@ -124,7 +124,7 @@ class ItemsController(ControllerBase):
124124
return [f.filename for f in files]
125125
```
126126

127-
## Uploading files with extra fields
127+
## **Uploading files with extra fields**
128128

129129
Note: HTTP protocol does not allow you to send files in application/json format by default (unless you encode it somehow to JSON on client side)
130130

docs/parsing-inputs/form-params.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Form data
1+
# **Form Data**
22

33
**Ellar** also allows you to parse and validate `request.POST` data
44
(aka `application x-www-form-urlencoded` or `multipart/form-data`).
@@ -10,7 +10,7 @@ When you need to receive form fields instead of JSON, you can use `Form`.
1010
E.g. `pip install python-multipart`.
1111

1212

13-
## Form Data as params
13+
## **Form Data as params**
1414

1515
```python
1616
# project_name/apps/items/controllers.py
@@ -39,7 +39,7 @@ from ellar.common import Form
3939
username: str = Form()
4040
```
4141

42-
## Using a Schema
42+
## **Using a Schema**
4343

4444
In a similar manner to [Body](../body/#declare-it-as-a-parameter), you can use
4545
a Schema to organize your parameters.
@@ -66,7 +66,7 @@ class ItemsController(ControllerBase):
6666

6767
![Openapi schema](../img/form-schema-doc.png)
6868

69-
## Request form + path + query parameters
69+
## **Request form + path + query parameters**
7070

7171
In a similar manner to [Body](../body/#request-body-path-query-parameters), you can use
7272
Form data in combination with other parameter sources.
@@ -101,7 +101,7 @@ class ItemsController(ControllerBase):
101101
return {"item_id": item_id, "item": item.dict(), "q": q}
102102
```
103103

104-
## Mapping Empty Form Field to Default
104+
## **Mapping Empty Form Field to Default**
105105

106106
Form fields that are optional, are often sent with an empty value. This value is
107107
interpreted as an empty string, and thus may fail validation for fields such as `int` or `bool`.

docs/parsing-inputs/header-params.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Header Parameters
1+
# **Header Parameters**
22

33
You can define Header parameters the same way you define `Query`, `Path` and `Cookie` parameters.
44

55
To query this operation, you use a URL like:
66

7-
## Import `Header`
7+
## **Import `Header`**
88

99
First import `Header` from `ellar.common` module
1010

11-
## Declare `Header` parameters
11+
## **Declare `Header` parameters**
1212

1313
Then declare the header parameters using the same structure as with `Path`, `Query` and `Cookie`.
1414

@@ -31,7 +31,7 @@ class ItemsController(ControllerBase):
3131
!!! info
3232
To declare headers, you need to use `Header`, because otherwise the parameters would be interpreted as query parameters.
3333

34-
## Automatic conversion
34+
## **Automatic conversion**
3535

3636
`Header` has a little extra functionality.
3737

@@ -67,7 +67,7 @@ class ItemsController(ControllerBase):
6767
Before setting `convert_underscores` to `False`, bear in mind that some HTTP proxies and servers disallow the usage of headers with underscores.
6868

6969

70-
## Duplicate headers
70+
## **Duplicate headers**
7171

7272
It is possible to receive duplicate headers. That means, the same header with multiple values.
7373

@@ -109,7 +109,7 @@ The response would be like:
109109
}
110110
```
111111

112-
### Using Schema
112+
## **Using Schema**
113113

114114
You can also use Schema to encapsulate `Header` parameters:
115115

docs/parsing-inputs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Intro
1+
# **Input Parsing Tutorial**
22

33
In this section, we are going to learn how inputs are parsed in the Ellar route handle functions.
44

@@ -44,7 +44,7 @@ visit [http://localhost:8000/items/](http://localhost:8000/items/),
4444
All code example will be done on the `ItemController` in `project_name/apps/items/controllers.py`.
4545
Please keep it open.
4646

47-
## Tutorial
47+
## **Tutorial**
4848
You are going to learn how to use the following route handler parameter:
4949

5050
- [Path](./path-params)

0 commit comments

Comments
 (0)