Skip to main content

A simple API client for connecting to remote services.

Project description

Simple Api Client

A simple API client for connecting to remote services.

Features

Along with common methods for creating and performing Json based API requests, this library supports the following extra features.

  • Convenience methods for adding Basic and Bearer auth headers.
  • A convenience method for adding signed cookies.
  • A timeout limit for all requests.
  • A global or per-request retry limit to retry an API request if it fails.
  • Uses a best effort approach to deal with malformed Json returned from an API endpoint.
  • Each response is guaranteed to contain a status code and well formed parsed Json data.
    • If something goes wrong, an error message will always be included in the data.
  • Fully supports returning binary data (as bytes) from an API request.
  • handles rate limiting when performing an API request. These requests can be configured to be retried before raising an error.

Installation

Install and update using pipenv

pip install -U simple-api-client

Usage

Simple use-case

from simple_api_client import ApiClient
from logging import Logger


logger = Logger("simple-logger", level="DEBUG")
client = ApiClient("http://www.example.com", logger)

response = client.get("/example/endpoint")

if response.status_code == 200:
    print(response.data)

Creating more specific clients

This client has been created to be as flexible as possible to be used as a base class for more specific clients. All you uneed to do is extend the ApiClient class and add any suitable methods.

from flask import g
from flask import current_app as app
from simple_api_client import ApiClient


class MyServiceClient(ApiClient):

    def use_cookie_auth(self, data):
        name = app.config.get("COOKIE_NAME")
        signing_key = app.config.get("COOKIE_SIGNING_KEY")
        signing_key_id = "trusted-service"
        payload = {"data": data}
        self.add_signed_cookie(name, payload, signing_key_id, signing_key)

    def get_remote_resource():
        return self.get("/example/endpoint")

A note on security

It's import to understand that when a client is initialised with headers and cookies, these will remain set for the lifetime of the client or until manually unset. If you don't want this state to remain in-between requests, it's important to take action to reset the client. In a Flask application this is easily achieved by assigning the client to the special 'g' object.

from flask import g
from flask import current_app as app
from simple_api_client import ApiClient


@app.before_request
def setup_api_client():
    g.client = ApiClient("http://www.example.com", app.logger)

Then in order to use it, import 'g'

from flask import g


response = g.client.get("/example/endpoint")

The benefit of this pattern is that the client is reset for every flask request so you don't need to worry about stale data in the client.

Development

The build pipeline require your tests to pass and code to be formatted

Make sure you have Python 3.x installed on your machine (use pyenv).

Install the dependencies with pipenv (making sure to include dev and pre-release packages):

pipenv install --dev --pre

Configure your environment:

pipenv shell && export PYTHONPATH="$PWD"

Run the tests:

pytest

Or with logging:

pytest -s

Or tests with coverage:

pytest --cov=./

Format the code with Black:

black $PWD

Releases

Cleanup the (.gitignored) dist folder (if you have one):

rm -rf dist

Notch up the version number in setup.py and build:

python3 setup.py sdist bdist_wheel

Push to PyPi (using the ScholarPack credentials when prompted)

python3 -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*

Links

Project details


Download files

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

Source Distribution

simple-api-client-1.0.3.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_api_client-1.0.3-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file simple-api-client-1.0.3.tar.gz.

File metadata

  • Download URL: simple-api-client-1.0.3.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

File hashes

Hashes for simple-api-client-1.0.3.tar.gz
Algorithm Hash digest
SHA256 970cd75facc169c4adc7899b88ef203b75ba427642fa2abc2f105625ee40e33d
MD5 605a19c008fb30d3b9fd321f070e31e5
BLAKE2b-256 6093eca2c65a23dc5106fbc91fd7815c353b492fe37ce24212776f5c0a7f7d32

See more details on using hashes here.

File details

Details for the file simple_api_client-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: simple_api_client-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9

File hashes

Hashes for simple_api_client-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 40d39a79a30c06a6089900bdf3e448cac56f2230f591fc104c012b5dfa812bfa
MD5 79f699f3af05f2b080080d6b59e99d59
BLAKE2b-256 dac66b141e9b50dbb93066ca79b765af7a73155613f4b776c14840c061a7cb14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page