Add API client examples
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -26,3 +26,77 @@ endpoints are structured by resource type. Examples:
|
|||||||
|
|
||||||
The API supports the HTTP verbs: **GET**, **POST**, **PUT**, **PATCH**,
|
The API supports the HTTP verbs: **GET**, **POST**, **PUT**, **PATCH**,
|
||||||
and **DELETE**.
|
and **DELETE**.
|
||||||
|
|
||||||
|
|
||||||
|
Example use
|
||||||
|
===========
|
||||||
|
|
||||||
|
Install Python Requests (http://docs.python-requests.org/en/master/)::
|
||||||
|
|
||||||
|
pip install requests
|
||||||
|
|
||||||
|
Get a list of document types::
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
requests.get('http://127.0.0.1:8000/api/document_types/', auth=('username', 'password')).json()
|
||||||
|
|
||||||
|
{u'count': 1,
|
||||||
|
u'next': None,
|
||||||
|
u'previous': None,
|
||||||
|
u'results': [{u'delete_time_period': 30,
|
||||||
|
u'delete_time_unit': u'days',
|
||||||
|
u'documents_count': 12,
|
||||||
|
u'documents_url': u'http://127.0.0.1:8000/api/document_types/1/documents/',
|
||||||
|
u'filenames': [],
|
||||||
|
u'id': 1,
|
||||||
|
u'label': u'Default',
|
||||||
|
u'trash_time_period': None,
|
||||||
|
u'trash_time_unit': None,
|
||||||
|
u'url': u'http://127.0.0.1:8000/api/document_types/1/'}]}
|
||||||
|
|
||||||
|
Upload a new document::
|
||||||
|
|
||||||
|
with open('test_document.pdf', mode='rb') as
|
||||||
|
requests.post('http://127.0.0.1:8000/api/documents/', auth=('username', 'password'), files={'file': file_object}, data={'document_type': 1}).json()
|
||||||
|
|
||||||
|
{u'description': u'',
|
||||||
|
u'document_type': 1,
|
||||||
|
u'id': 19,
|
||||||
|
u'label': u'test_document.pdf',
|
||||||
|
u'language': u'eng'}
|
||||||
|
|
||||||
|
Use API tokens to avoid sending the username and password on every request. Obtain a token by making a POST request to ``/api/auth/token/obtain/?format=json``::
|
||||||
|
|
||||||
|
requests.post('http://127.0.0.1:8000/api/auth/token/obtain/?format=json', data={'username': 'username', 'password': 'password'}).json()
|
||||||
|
|
||||||
|
{u'token': u'4ccbc35b5eb327aa82dc3b7c9747b578900f02bb'}
|
||||||
|
|
||||||
|
Add the API token to the request header::
|
||||||
|
|
||||||
|
headers = {'Authorization': 'Token 4ccbc35b5eb327aa82dc3b7c9747b578900f02bb'}
|
||||||
|
|
||||||
|
requests.get('http://127.0.0.1:8000/api/document_types/', headers=headers).json()
|
||||||
|
|
||||||
|
{u'description': u'',
|
||||||
|
u'document_type': 1,
|
||||||
|
u'id': 19,
|
||||||
|
u'label': u'test_document.pdf',
|
||||||
|
u'language': u'eng'}
|
||||||
|
|
||||||
|
|
||||||
|
Use sessions to avoid having to add the headers on each request::
|
||||||
|
|
||||||
|
session = requests.Session()
|
||||||
|
|
||||||
|
headers = {'Authorization': 'Token 4ccbc35b5eb327aa82dc3b7c9747b578900f02bb'}
|
||||||
|
|
||||||
|
session.headers.update(headers)
|
||||||
|
|
||||||
|
session.get('http://127.0.0.1:8000/api/document_types/')
|
||||||
|
|
||||||
|
{u'description': u'',
|
||||||
|
u'document_type': 1,
|
||||||
|
u'id': 19,
|
||||||
|
u'label': u'test_document.pdf',
|
||||||
|
u'language': u'eng'}
|
||||||
|
|||||||
Reference in New Issue
Block a user