Skip to main content

Python common code for Directory API clients.

Project description

directory-client-core

code-climate-image circle-ci-image codecov-image pypi-image semver-image

Directory Client Core.

Common code for the Directory API clients.

Requirements

Installation

pip install directory-client-core

Usage

from directory_client_core.base import AbstractAPIClient


class MyAPIClient(AbstractAPIClient):

    version = 1  # passed as a header in all requests

    def get_something(self):
        return self.get(...)

    def create_sometime(self):
        return self.post(...)


client = MyAPIClient(
    base_url='https://example.com/',
    api_key='test',
    sender_id='test-sender-id',
    timeout=2,
)

response = client.get_something()

Caching

The decorator directory_client_core.helpers.fallback can be used to cache the responses from the remote server, allowing the cached content to be later used if the remote server does not return the up to date live content (maybe it times out, maybe the server is down). This decorator also saves etag response headers to later expose them in requests and respect 304 (Not modified) response and serve already cached contents.

# settings.py
DIRECTORY_CLIENT_CORE_CACHE_EXPIRE_SECONDS = 60 * 60 * 24 * 30  # 30 days

# client.py

from django.core.cache import caches

from directory_client_core import helpers
from directory_client_core.base import AbstractAPIClient


class APIClient(AbstractAPIClient):
    version = 1

    @helpers.fallback(cache=caches['fallback'])
    def get(self, *args, **kwargs):
        return super().get(*args, **kwargs)

    def retrieve(self):
        return self.get(url='/some/path/')

The fallback creates log entries when cache events occur. To reduce noise DIRECTORY_CLIENT_CORE_CACHE_LOG_THROTTLING_SECONDS can be set in settings. This will result in a log event being created only once every period of time. By default this means seeing "cache hit for url x" (for a given url) is shown once every 24 hours.

Development

$ git clone https://github.com/uktrade/directory-client-core
$ cd directory-client-core
$ make test_requirements

Publish to PyPI

The package should be published to PyPI on merge to master. If you need to do it locally then get the credentials from rattic and add the environment variables to your host machine:

Setting
DIRECTORY_PYPI_USERNAME
DIRECTORY_PYPI_PASSWORD

Then run the following command:

make publish

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

directory_client_core-7.3.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file directory_client_core-7.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for directory_client_core-7.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 858a2458bafad1ffce2532d49655d27192a390c20ce36d800825c3b0f8e4fee3
MD5 71483cfb9e87d3678c71d8012ac97d58
BLAKE2b-256 14747a45b1acb614862710200120851a50f08425d4552883b96fbbf2a3c76cda

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