Library with REST APIs Client code for Django
Project description
Para visualizar o README em português.
Django API Client
Django API Client is a client for APIs in general, which allows iterating with the API as if they were using a Local model in their project, through a client and Custom CBV (Class based Views)
Some reasons to use the Django API Client
If you work with microservices with APIs in multiple locations and want to continue using Django as a WebApp with the same capabilities to render data as if you were using native models
You want to separate your Django project to let one of them only with the API with DRF and the other as a WebApp with Templates (HTML), CSS, JS instead of using some JS frontend (ReactJS, AngularJS, etc.)
You want to use a third party API to list, create and change using the django template system
For more information, see our documentation at Read the Docs.
Ref: * Queryset: (Result of a search using Django’s ORM)
Requirements
Python 3.x
Django 2.0 or later
How to install
You can get Django Stuff by using pip:
$ pip install django-api-client
If you want to install it from source, grab the git repository from Gitlab and run setup.py:
$ git clone git@github.com:rhenter/django_api_client.git
$ cd django_api_client
$ python setup.py install
Config
To enable django_api_client in your project you need to add it to INSTALLED_APPS in your projects settings.py file:
INSTALLED_APPS = (
...
'django_api_client',
...
)
Example
Adicione tambem ao settings.py a configuração para acessar sua API:
DJANGO_API_CLIENT = {
'API': [
{
'NAME': 'production',
'URL_BASE': 'https://example.com/v1',
'ENDPOINTS': [
'/order/orders',
'/user/users',
...
],
'AUTHENTICATION_ACCESS_TOKEN': 'TOKEN'
},
{
'NAME': 'localhost',
'URL_BASE': 'http://localhost:8001/v1',
'ENDPOINTS': [
'/order/orders',
'/user/users',
...
],
'AUTHENTICATION_ACCESS_TOKEN': 'TOKEN'
}
]
}
Obs: Os detalhes de configuração serão explicados na documentação
Crie um arquivo clients.py em alguma pasta nucleo do seu projeto, caso não tenha crie dentro da sua pasta do projeto para ser simples de ser importado de qualquer lugar do projeto com o seguinte conteúdo:
from django_api_client.client import api_client_factory
api_client = api_client_factory('production')
- Obs:
O nome desta variável será o nome nome do cliente que você usará em todo o seu projeto
Recomendo para produção usar uma env var setada no settings.py para você poder alterar de maneira simples o nome da API sem a necessidade de criar varios.
No nosso caso, temos a opção de “production” e “localhost”, o factory gerará o cliente de acordo com o nome utilizado e os parametros descritos nele
Agora vamos listar os dados usando o sistema de templates normal do Django
Vamos imaginar que o cliente esta na pasta de projeto (pasta que contem o arquivo settings.py)
from django_api_client.mixins import ClientAPIListMixin
from pasta_do_projeto.clients import api_client
class OrderListView(ClientAPIListMixin):
template_name = "template_name.html" # Caminho do seu template HTML
page_title = 'Orders' # Gera uma variavel de contexto para usar no seu template
page_base_url = reverse_lazy('order:list') # Informação usada na paginação
paginate_by = 50 # Número de items para gerar a paginação
client_method = api_client.order.get_orders
- Obs:
O cliente gerará para cada endpoint a toda uma estrutura amigavel para o usuário. Exemplo com o endpoint /order/orders/:
nome do endpoint: order
métodos:
get_orders # GET: Listar
get_order # GET: Detalhe de um recurso usando um identificador
create_order # POST: Cria um registro de um recurso
update_order # PUT/PATCH: Atualiza total ou parciamente um recurso usando um identificador
O que isso quer dizer?
Que o cliente sempre gerará a estutura de acordo os nomes dos seus endpoints
Documentation
Check out the latest django-api-client documentation at Read the Docs
Contributing
Please send pull requests, very much appreciated.
Fork the repository on GitHub.
Make a branch off of master and commit your changes to it.
Install requirements. pip install -r requirements-dev.txt
Install pre-commit. pre-commit install
Run the tests with cd test-django-project; py.test -vv -s
Create a Pull Request with your contribution
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_api_client-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 873ed4a22dd335726abec8b9aaf7eb60bacdc65febce2e56d2ef4fa1ee232eaa |
|
MD5 | ddc5032e68b3b0688d6151072a3e4e81 |
|
BLAKE2b-256 | a67c175b765fa0620deb267c3eaa2442d671aff4a51a906170021b0aec7e43a5 |