Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

А library for a REST API client that works with any API that follows somewhat the REST standard.

Project description

Universal REST API Client

Build Status codecov PyPI version

Features

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.

Installation

pip install universal-api-client

Usage

Initialising the client

from universal_api_client import Client
swapi_client = Client(base_url='https://swapi.co/api/')

Building a URL

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/'

Making a request

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

Authentication

The library allows the use of the requests authentication classes (request.auth).

There are 2 ways to add authentication:

  1. When initializing the client.
from requests.auth import HTTPBasicAuth

swapi_client = Client(base_url='https://swapi.co/api/', auth=HTTPBasicAuth('user', 'pass'))
  1. 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'))

Trailing slash

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'

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for universal-api-client, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size universal_api_client-0.2.0-py2.py3-none-any.whl (5.2 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size universal-api-client-0.2.0.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page