PythonServiceUtil
PythonServiceUtil is a utility library designed to streamline Python-based microservice development. It offers robust support for:
- REST Inter-service Calls: Simplifies HTTP communication between services.
- AWS SQS Integration: Enhances message queue operations.
- Redis Support: Provides efficient caching and data storage.
- MongoDB Integration: Simplifies database interactions, including transactions (mongoengine object data mapper).
- Context Management: Facilitates seamless context management in multithreaded and multiprocessing environments.
Features
Validation Workflow
The validation process is structured as follows:
- Access Token Validation: Validates the access token as the primary step.
- Fallback to X-API-KEY Validation: If the access token validation fails, X-API-KEY validation is used as a fallback.
Note: The JWKS (JSON Web Key Set) must follow the format
List[Jwk_TH]. Specify the symmetric or asymmetric algorithm to use viaAUTH_TOKEN_ALGORITHM.
class Jwk_TH(TypedDict, total=False):
alg: Required[str]
e: str
kid: Required[str]
kty: str
n: str
use: str
def __load_jwks() -> List[Jwk_TH]:
return json.loads(str(config("JWKS")))
Configuration
Configure the library using an .env file with the following keys:
ENVIRONMENT=develop
LOGGING_FILENAME=service.log
AUTH_TOKEN_ALGORITHM=RS256
BEDROCK_AWS_REGION_NAME=region_name
AWS_REGION_NAME=region_name
AWS_ACCESS_KEY=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_access_key
X_API_KEY_EMBEDDING_SERVICE_1=access_key
X_API_KEY_EMBEDDING_SERVICE_2=access_key
JWKS=JWKS
Usage
Headers Validation Model
The headers validation model supports both inter-service calls and client-to-backend communication. It first checks cookie validation and then falls back to header-based authorization.
import uuid
from pydantic import BaseModel, Field
from typing import Any, Dict
class Headers_PM(BaseModel):
correlationid: str = Field(default_factory=lambda: str(uuid.uuid4()))
username: str = "not_applicable"
authorization: str = Field(default="")
def model_dump(self, exclude_fields={}, **kwargs) -> Dict[str, Any]:
return super().model_dump(**kwargs, exclude=exclude_fields)
Setting Up REST Middlewares
Here is an example of setting up REST middlewares in a FastAPI app:
app.add_middleware(
HeaderValidationMiddleware,
x_api_key_1=cast(str, config("X_API_KEY_EMBEDDING_SERVICE_1")),
x_api_key_2=cast(str, config("X_API_KEY_EMBEDDING_SERVICE_2")),
authexpiryignore_paths=frozenset([
ServicePaths.CONTEXT_PATH.value + "/encoders",
ServicePaths.CONTEXT_PATH.value + "/llm",
]),
)
app.add_middleware(ExceptionMiddleware)
Key Fields in Headers_PM
correlationid: A unique identifier for tracing requests, generated usinguuid4().username: The username associated with the request; defaults to"not_applicable".authorization: The authorization token, defaulting to an empty string.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Follow these steps:
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name. - Commit your changes:
git commit -m 'Add feature'. - Push to the branch:
git push origin feature-name. - Open a pull request.
Publishing to PyPI
To upload the library to PyPI, follow these steps:
pip install -r requirements.dev.txt
python setup.py bdist_wheel sdist
twine check dist/*
twine upload dist/*
Metadata
Release files for zoldics-service-utils 0.1.17
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| zoldics_service_utils-0.1.17.tar.gz | 25.9 kB | Without provenance | Details for zoldics_service_utils-0.1.17.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| zoldics_service_utils-0.1.17-py3-none-any.whl | Python 3 | none | any | Details for zoldics_service_utils-0.1.17-py3-none-any.whl |
Total release size: 64.7 kB
Release files / zoldics_service_utils-0.1.17.tar.gz
| Download URL | zoldics_service_utils-0.1.17.tar.gz |
|---|---|
| Size | 25.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
074ccbfb28a842d5cf2986ce14e6c1c90ab948eb4752b008b0c2415ab8f3cb13
|
|
BLAKE2b-256 checksum How to use checksums |
488b77b8abfc5485f9e5f3e87353dda7bed9a1e980a7ff4911d4765d1d3594e4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.11
|
Release files / zoldics_service_utils-0.1.17-py3-none-any.whl
| Download URL | zoldics_service_utils-0.1.17-py3-none-any.whl |
|---|---|
| Size | 38.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
61d0b3586daefc917eb36c3fbe5dfc2af5ad636b2fde3c683cb9cc9ef85b9bf8
|
|
BLAKE2b-256 checksum How to use checksums |
b0d0db19969209a55e980b88b21b02566bfa5a77674efa53f10b153389f33c74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.11.11
|
Security for zoldics-service-utils 0.1.17
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.