Skip to main content

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

Release files for directory-client-core 7.3.3

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

Built distribution (wheel)

Table of built distributions (wheels) for directory-client-core 7.3.3
File Interpreter ABI Platform
directory_client_core-7.3.3-py3-none-any.whl Python 3 none any Details

Release files / directory_client_core-7.3.3-py3-none-any.whl

Download URL directory_client_core-7.3.3-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce93069501d524d90739402a3275a3919b1885c226adc998f754a31ad1f9f49e
BLAKE2b-256 checksum
How to use checksums
2b728f0f1dd98443292f0d0bf5d155f5ab12f920a8f2b6d033d731cdacf625cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

7.3.3 This release

1 release file

7.3.2

1 release file

7.3.1

1 release file

7.3.0

1 release file

7.2.14

1 release file

7.2.13

1 release file

7.2.12

1 release file

7.2.11

1 release file

7.2.10

1 release file

7.2.9

1 release file

7.2.8

1 release file

7.2.7

1 release file

7.2.6

1 release file

7.2.5

1 release file

7.2.4

1 release file

7.2.3

1 release file

7.2.2

1 release file

7.2.1

1 release file

7.1.2

1 release file

7.1.1

1 release file

7.1.0

1 release file

7.0.0

1 release file

6.3.0

1 release file

6.2.0

1 release file

6.1.0

1 release file

6.0.1

1 release file

6.0.0

1 release file

5.1.1

1 release file

5.1.0

1 release file

5.0.0

1 release file

4.4.0

1 release file

4.3.1

1 release file

4.3.0

1 release file

4.2.0

1 release file

4.0.2

1 release file

4.0.1

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.1

1 release file

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