1. Visão Geral

O API Client Factory é um wrapper python3 para APIs REST. Ou seja, APIs que seguem o padrão de uso dos métodos abaixo:

GET: List or Retrieve
POST: Create
UPDATE: Full Update
PATCH: Partial Update

2. Configurações

Para habilitar o django_api_client você precisa adiciona-lo ao INSTALLED_APPS no seu arquivo settings.py do seu projeto:

INSTALLED_APPS = (
    ...
    'django_api_client',
    ...
)

Você tambem precisa adicionar suas configurações das suas API usando a constante DJANGO_API_CLIENT. Ex

DJANGO_API_CLIENT = {
    'API': {
        'NAME': '<Slug Name to Access Your client>',
        'BASE_URL': 'https://example.com/v1',
        'ENDPOINTS': [
            '<PATH OF THE ENDPOINT 1>',
            '<PATH OF THE ENDPOINT 2>',
            ...
        ],
        'AUTHENTICATION_ACCESS_TOKEN': 'TOKEN'
    }
}
  • Para mais informações sobre as configurações diponiveis, veja em:

3. Métodos Cliente

Para cada endpoint o API Client irá criar a seguinte estrutura:

Exemplo para /user/users/

  • Criar:

usage: api_client.user.users.create(data=data)
return: Response of POST of data (dict) to /user/users/
  • Listar:

usage: api_client.user.users.list()
return: Response of GET to /user/users/
  • Get/Recuperar/Detalhe:

usage: api_client.user.users.get(id=123)
return: Response of GET to /user/users/123/
  • Atualizar:

usage: api_client.user.users.update(id=123, data=data, partial=False)
return: the response of UPDATE or PATCH of data (dict) to /user/users/123/
  • Deletar:

usage: api_client.user.users.delete(id=123)
return: Response of GET to /user/users/123/

4. Uso do Cliente

  • Importe o modulo api_client_factory e crie uma instância usando o nome da API que você definiu na constante DJANGO_API_CLIENT no arquivo settings.py

>> from django_api_client.client import api_client_factory
>> api_client = api_client_factory('<Slug Name to Access Your client>')
>>
>> result = api_client.user.users.list()
>>
>> # Use the result as object
>> print(result.as_obj())
UserUsers(
    previous=None,
    count=1,
    next=None,
    results=[
        NamelessModel(occupation=None, full_name='Admin System',
            image=None, cpf='', is_superuser=True, cellphone='', email='', sex=None, username='admin', birthdate='09/09/1999',
            logged_as='', id=1, is_temp=False, is_active=True)
    ]
)
>>
>> # Use the result as dict
>> print(result.as_dict())
{'count': 1,
 'next': None,
 'previous': None,
 'results': [{'id': 1,
   'username': 'admin',
   'full_name': 'Admin System',
   'sex': None,
   'birthdate': '09/09/1999',
   'cpf': '',
   'cellphone': '',
   'email': '',
   'image': None,
   'occupation': None,
   'logged_as': '',
   'is_superuser': True,
   'is_active': True,
   'is_temp': False}
  ]
 }