1. Overview¶
API Client Factory is a python3 wrapper for REST APIs. That is, APIs that follow the pattern of using the methods as below:
GET: List or Retrieve
POST: Create
UPDATE: Full Update
PATCH: Partial Update
2. Settings¶
To enable django_api_client in your project you need to add it to INSTALLED_APPS in your project settings.py file:
INSTALLED_APPS = (
...
'django_api_client',
...
)
You need also to add your APIs settings using DJANGO_API_CLIENT
constant. E.g:
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'
}
}
For more information on the available configurations, see at:
3. Client Methods¶
For each endpoint the client Factory will create the follow structure:
Example to /user/users/
Create:
usage: api_client.user.users.create(data=data)
return: Response of POST of data (dict) to /user/users/
List:
usage: api_client.user.users.list()
return: Response of GET to /user/users/
Get/Retrieve/Detail:
usage: api_client.user.users.get(id=123)
return: Response of GET to /user/users/123/
Update:
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/
Delete:
usage: api_client.user.users.delete(id=123)
return: Response of GET to /user/users/123/
4. Client Usage¶
Import the api_client_factory module and create an instance using the name of the API you set in the
DJANGO_API_CLIENT
constant on settings.py file:
>> 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}
]
}