Skip to main content

Build Status

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

Release files for simple-api-client 1.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-api-client 1.0.8
File Size Uploaded
simple-api-client-1.0.8.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-api-client 1.0.8
File Interpreter ABI Platform
simple_api_client-1.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / simple-api-client-1.0.8.tar.gz

Download URL simple-api-client-1.0.8.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f2181d685688e1def0e1bd835ccb9c41950a495e6c3f42c1b843670a011dd342
BLAKE2b-256 checksum
How to use checksums
108f7f42c9a06e287457b7a0aa42d484a1039079acd12d322a3b71eba97bbf22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.4

Release files / simple_api_client-1.0.8-py3-none-any.whl

Download URL simple_api_client-1.0.8-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e09e072f12369eea067768191b2f8e4b915217e3ba9c073ec00d43eb8e2a2b5d
BLAKE2b-256 checksum
How to use checksums
eaf5b8adba7f4a7e0c7e600f79e24c0c65142aeb21963ce3e723525786b4d2d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

1.0.8 This release

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page