O que há neste pacote?
Este pacote tem ferramentas para extender as funcionalidades de API's rest escritas em Django e Django Rest Framework.
Middlewares
Envelope
Este middleware dá a capacidade de incluir um envelope na respostas da API:
Ex:
Requisição: https://[.....]/?envelope=true
Resposta:
{
"code": 200,
"message": "OK",
"data": {
"id": "11e87365528a11659bdd005056a83b77",
"numero_loja": 2,
"endereco": "RUA OTAVIO ROCHA, 152",
"cidade": "PORTO ALEGRE",
"estado": "RS",
"cep": 90020150,
"telefone": "(51) 3901-6053"
}
}
Requisição: https://[.....]/?envelope=false
Resposta:
{
"id": "11e87365528a11659bdd005056a83b77",
"numero_loja": 2,
"endereco": "RUA OTAVIO ROCHA, 152",
"cidade": "PORTO ALEGRE",
"estado": "RS",
"cep": 90020150,
"telefone": "(51) 3901-6053"
}
Obs.: Por default, o recurso de envelope fica desabilitado (igual a envelope=false).
Log
Este middleware habilita uma personalização dos logs para envio para o Graylog, ferramenta de registro de logs.
Segue um exemplo de configuração:
LOGGING = {
'version': 1,
'filters': {
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
'settings_filter': {
'()': 'apicredito.core.logfilter.SettingsFilter',
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
},
'graypy': {
'level': 'INFO',
'class': 'graypy.GELFHandler', # configurações do Graylog
'host': '192.168.0.45',
'port': 12201,
'level_names': True,
'extra_fields': True
}
},
'loggers': {
'apilog': {
'level': 'INFO',
'handlers': ['graypy'],
'filters': ['settings_filter']
}
}
}
Para habilitar os middlewares, coloque na seção MIDDLEWARE do settings.py da sua API Django:
MIDDLEWARE = [
[...]
'lins_restapi.http.EnvelopeMiddleware',
'lins_restapi.http.LoggingMiddleware',
]
Paginação
Este pacote estende a biblioteca de paginação do Django, padronizando e integrando com os demais middlewares do pacote. Ele adiciona headers nas respostas da API, contendo o total de registros, o número de registros retornados na página e qual a pagina atual. Se envelope=true, também retorna como um campo do envelope.
Conceitos:
- total ou X-Total: Total de registros da requisição;
- page ou X-Page: Página atual/requisitada;
- per_page ou X-Per-Page: Número de registros por página;
Ex.:
Requisição:
https://[...]?per_page=2&page=5&envelope=true
Resposta:
{
"code": 200,
"message": "OK",
"total": 43,
"page": 5,
"per_page": 2,
"data": [
{
"id": "11e87365528a5bff9bdd005056a83b77",
"numero_loja": 10,
"endereco": "RUA JULIO DE CASTILHOS, 2030",
"cidade": "CAXIAS DO SUL",
"estado": "RS",
"cep": 95010002,
"telefone": "(54) 3223-1685"
},
{
"id": "11e87365528a62609bdd005056a83b77",
"numero_loja": 11,
"endereco": "AV. NAÇÕES UNIDAS, 2001",
"cidade": "NOVO HAMBURGO",
"estado": "RS",
"cep": 93320020,
"telefone": "(51) 3594.5948"
}
]
}
Headers:
X-Page →5
X-Per-Page →2
X-Total →43
Obs.: Se houver cors, como o django-cors-headers, é necessário adicionar os headers de paginação como exceção:
CORS_EXPOSE_HEADERS = [
'X-Page',
'X-Per-Page',
'X-Total'
]
Outros recursos
- Sobrescrita de classes do Swagger Generator afim de tratar o protocolo https;
Metadata
Release files for lins-restapi 2.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| lins_restapi-2.1.1.tar.gz | 6.0 kB | Without provenance | Details for lins_restapi-2.1.1.tar.gz |
Release files / lins_restapi-2.1.1.tar.gz
| Download URL | lins_restapi-2.1.1.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8254c9f2044083c4bf6d0a1807cc2706d725831c80a6391ea1cdcb7f99b6a541
|
|
BLAKE2b-256 checksum How to use checksums |
5f17e5c0f8824b52852361c639045591c2fd1cfe962caf8c0644f451b1c43c5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
|
Security for lins-restapi 2.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.