Metadata-Version: 2.4
Name: clients_core
Version: 4.0.1
Summary: A collection of Python connection clients for E360 Microservices
Author-email: IQVIA <e360pypi@iqvia.com>
License: Copyright 2024 IQVIA Ltd
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
             http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
License-File: LICENSE.txt
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Requires-Python: >=3.12
Requires-Dist: oauthlib
Requires-Dist: pydantic-settings
Requires-Dist: pydantic<3,>=2
Requires-Dist: redis
Requires-Dist: requests
Requires-Dist: requests-oauthlib
Requires-Dist: semver
Requires-Dist: simplejson
Description-Content-Type: text/markdown

E360 Clients Core
===

[![pipeline status](http://rwes-gitlab01.internal.imsglobal.com/python-microservice-clients/clients-core/badges/master/pipeline.svg)](http://rwes-gitlab01.internal.imsglobal.com/python-microservice-clients/clients-core/commits/master)


## Installation

Use the E360 Artifactory package manager [e360-pypi-local](https://rwes-artifactory01.internal.imsglobal.com/artifactory/) to install `clients_core`.

**Requires Python 3.12 or newer.**

```bash
pip install clients_core --index-url=https://rwes-artifactory01.internal.imsglobal.com/artifactory/api/pypi/e360-pypi-local/simple
```


## Usage with Service Directory

1. Create a TokenHandler, with the right configuration for identity.
```python
from clients_core.authentication.token_handler import OIDCTokenHandler

token_handler = OIDCTokenHandler("<oidc_token_endpoint>", "<oidc_client_id>", "<oidc_client_secret>")

```
2. Create a SecureRestClient for ServiceDirectory, and pass the token handler
```python
from clients_core.secured_rest_client import SecuredRestClient
rest_client = SecuredRestClient("<url-to-service-directory", ["service-dir-scopes"], token_handler)
```

3. Create a ServiceDirectoryClient using the rest client
```python
from sd_clients.service_directory_client import ServiceDirectoryClient
sd_client = ServiceDirectoryClient(rest_client)
```


4. The SD client can now be passed to ApiMatchClient which will allow lookups of services
```python
from clients_core.api_match_client import ApiMatchClient, MatchSpec

match_client = ApiMatchClient(sd_client)
match_spec = MatchSpec("service-name", "endpoint-name",1 ,0, 1, ["service-scopes"])
service_rest_client = match_client.get_secured_client(match_spec, token_handler)
```

The service rest client can now be passed to a microservice client, or used directly to make REST calls to the target service


## Usage with cache

If it is desired to use a cache, we need to wrap our TokenHandler with TokenCache. There are currently two Cache implementations, RedisCache and DictCache.
`RedisCache` requires a connection URL when instantiated:

```python
from clients_core.authentication.cache import RedisCache
from clients_core.authentication.token_cache import TokenCache

cache = RedisCache("<redis-connection-url>")
token_cache = TokenCache(cache, token_handler)  # token handler as created in step .1
```

`token_cache` can now be used in place of `token_handler`.


## Development and Testing

Install [uv](https://docs.astral.sh/uv/) (Python 3.12+ recommended).

```bash
uv sync --group dev
```

Run checks and tests:

```bash
uv run ruff check clients_core tests
uv run ruff format --check clients_core tests
uv run pytest
```

Run a single test file or test:

```bash
uv run pytest tests/test_secured_rest_client.py
uv run pytest tests/test_secured_rest_client.py::test_simple_post
```

Optional type check:

```bash
uv run mypy clients_core
```

Build a wheel locally:

```bash
uv build --out-dir dist
```

On the corporate network, you can point resolution at Artifactory, for example:

```bash
export UV_INDEX_URL=https://rwes-artifactory01.internal.imsglobal.com/artifactory/api/pypi/e360-pypi-local/simple
uv sync --group dev
```

See `pyproject.toml` for optional explicit `e360-pypi` indexes when adding internal-only dependencies.


## Deployments

Auto CI enabled.

A deployment will happen every time a merge request is merged into master AND the version number is incremented in the `clients_core/__init__.py` file.


## Development

After cloning this repository:

```bash
uv sync --group dev
```

Install pre-commit hooks:

```bash
uv run pre-commit install --install-hooks --overwrite
```


## UML
![](docs/uml.png)
