- Free software: MIT license
- Documentation: https://universal-api-client.readthedocs.io/en/latest/.
This library is a small REST API client with the following features:
- Url builder - allows you to build a url by natively calling the client's attributes
- HTTP requests - a thin wrapper around the requests library that allows full control of the HTTP requests.
pip install universal-api-client
from universal_api_client import Client
swapi_client = Client(base_url='https://swapi.co/api/')
The url builder is part of the request
(APIRequest
) attribute of the
client.
swapi_client.request.people # <universal_api_client.request.APIRequest at 0x1093c3eb8>
swapi_client.request.people.url # 'https://swapi.co/api/people/'
swapi_client.request.people(identifier=1).url # 'https://swapi.co/api/people/1/'
swapi_client.request.people(identifier='1').url # 'https://swapi.co/api/people/1/'
The requests are made by the already built APIRequest
object. The
method call returns the appropriate method call from the requests
library.
response = swapi_client.request.people(identifier='1').get() # <Response [200]>
print(response.status_code) # 200
The library allows the use of the requests authentication classes (request.auth).
There are 2 ways to add authentication:
- When initializing the client.
from requests.auth import HTTPBasicAuth
swapi_client = Client(base_url='https://swapi.co/api/', auth=HTTPBasicAuth('user', 'pass'))
- When performing the request (overrides the authentication set in the client).
from requests.auth import HTTPBasicAuth
swapi_client.request.people.get(auth=HTTPBasicAuth('user', 'pass'))
Some API urls require (or not) a trailing slash at the end of the URL. This can be controlled by the trailing_slash
flag when creating the client:
from universal_api_client import Client
swapi_client = Client(base_url='https://swapi.co/api/', trailing_slash=False)
swapi_client.request.people(identifier='1').url # 'https://swapi.co/api/people/1'
This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.