directory-client-core


Namedirectory-client-core JSON
Version 7.2.13 PyPI version JSON
download
home_pagehttps://github.com/uktrade/directory-client-core
SummaryPython common code for Directory API clients.
upload_time2024-02-23 09:04:14
maintainer
docs_urlNone
authorDepartment for International Trade
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # directory-client-core

[![code-climate-image]][code-climate]
[![circle-ci-image]][circle-ci]
[![codecov-image]][codecov]
[![pypi-image]][pypi]
[![semver-image]][semver]

**Directory Client Core.**

Common code for the Directory API clients.
---

## Requirements

## Installation

```shell
pip install directory-client-core
```

## Usage

```python
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


[code-climate-image]: https://codeclimate.com/github/uktrade/directory-client-core/badges/issue_count.svg
[code-climate]: https://codeclimate.com/github/uktrade/directory-client-core

[circle-ci-image]: https://circleci.com/gh/uktrade/directory-client-core/tree/master.svg?style=svg
[circle-ci]: https://circleci.com/gh/uktrade/directory-client-core/tree/master

[codecov-image]: https://codecov.io/gh/uktrade/directory-client-core/branch/master/graph/badge.svg
[codecov]: https://codecov.io/gh/uktrade/directory-client-core

[pypi-image]: https://badge.fury.io/py/directory-client-core.svg
[pypi]: https://badge.fury.io/py/directory-client-core

[semver-image]: https://img.shields.io/badge/Versioning%20strategy-SemVer-5FBB1C.svg
[semver]: https://semver.org




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/uktrade/directory-client-core",
    "name": "directory-client-core",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Department for International Trade",
    "author_email": "",
    "download_url": "",
    "platform": null,
    "description": "# directory-client-core\n\n[![code-climate-image]][code-climate]\n[![circle-ci-image]][circle-ci]\n[![codecov-image]][codecov]\n[![pypi-image]][pypi]\n[![semver-image]][semver]\n\n**Directory Client Core.**\n\nCommon code for the Directory API clients.\n---\n\n## Requirements\n\n## Installation\n\n```shell\npip install directory-client-core\n```\n\n## Usage\n\n```python\nfrom directory_client_core.base import AbstractAPIClient\n\n\nclass MyAPIClient(AbstractAPIClient):\n\n    version = 1  # passed as a header in all requests\n\n    def get_something(self):\n        return self.get(...)\n\n    def create_sometime(self):\n        return self.post(...)\n\n\nclient = MyAPIClient(\n    base_url='https://example.com/',\n    api_key='test',\n    sender_id='test-sender-id',\n    timeout=2,\n)\n\nresponse = client.get_something()\n```\n\n### Caching\n\nThe 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.\n\n```\n# settings.py\nDIRECTORY_CLIENT_CORE_CACHE_EXPIRE_SECONDS = 60 * 60 * 24 * 30  # 30 days\n\n# client.py\n\nfrom django.core.cache import caches\n\nfrom directory_client_core import helpers\nfrom directory_client_core.base import AbstractAPIClient\n\n\nclass APIClient(AbstractAPIClient):\n    version = 1\n\n    @helpers.fallback(cache=caches['fallback'])\n    def get(self, *args, **kwargs):\n        return super().get(*args, **kwargs)\n\n    def retrieve(self):\n        return self.get(url='/some/path/')\n```\n\nThe `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.\n\n## Development\n\n    $ git clone https://github.com/uktrade/directory-client-core\n    $ cd directory-client-core\n    $ make test_requirements\n\n## Publish to PyPI\n\nThe 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:\n\n| Setting                     |\n| --------------------------- |\n| DIRECTORY_PYPI_USERNAME     |\n| DIRECTORY_PYPI_PASSWORD     |\n\n\nThen run the following command:\n\n    make publish\n\n\n[code-climate-image]: https://codeclimate.com/github/uktrade/directory-client-core/badges/issue_count.svg\n[code-climate]: https://codeclimate.com/github/uktrade/directory-client-core\n\n[circle-ci-image]: https://circleci.com/gh/uktrade/directory-client-core/tree/master.svg?style=svg\n[circle-ci]: https://circleci.com/gh/uktrade/directory-client-core/tree/master\n\n[codecov-image]: https://codecov.io/gh/uktrade/directory-client-core/branch/master/graph/badge.svg\n[codecov]: https://codecov.io/gh/uktrade/directory-client-core\n\n[pypi-image]: https://badge.fury.io/py/directory-client-core.svg\n[pypi]: https://badge.fury.io/py/directory-client-core\n\n[semver-image]: https://img.shields.io/badge/Versioning%20strategy-SemVer-5FBB1C.svg\n[semver]: https://semver.org\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python common code for Directory API clients.",
    "version": "7.2.13",
    "project_urls": {
        "Homepage": "https://github.com/uktrade/directory-client-core"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "df55c2ebb1f3d3c42cc8c207ca7dcf3accb376c3e76bbe4904da49558bc25f30",
                "md5": "fc255b13d5d021f3f47af0038dda5f3e",
                "sha256": "d845baa972c10b7c92f2bc7d1a623b2b70277bc0c3a57141df4cf9218f09a303"
            },
            "downloads": -1,
            "filename": "directory_client_core-7.2.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fc255b13d5d021f3f47af0038dda5f3e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8154,
            "upload_time": "2024-02-23T09:04:14",
            "upload_time_iso_8601": "2024-02-23T09:04:14.036629Z",
            "url": "https://files.pythonhosted.org/packages/df/55/c2ebb1f3d3c42cc8c207ca7dcf3accb376c3e76bbe4904da49558bc25f30/directory_client_core-7.2.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-23 09:04:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "uktrade",
    "github_project": "directory-client-core",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": false,
    "circle": true,
    "lcname": "directory-client-core"
}
        
Elapsed time: 0.18648s