Shared services for TinyAI microservices platform
Project description
TinySDK
Shared services for the TinyAI microservices platform. Provides unified MongoDB, MinIO, and RabbitMQ clients with consistent interfaces across all services.
Features
- TinyDBService: MongoDB client with CRUD operations and automatic index management
- TinyStorageHandler: MinIO/S3-compatible storage client with file and folder operations
- TinyMessageService: Async (event-driven) RabbitMQ client using SelectConnection
- TinyMessageServiceSync: Sync (blocking) RabbitMQ client using BlockingConnection
- Consistent error handling with retry logic and exponential backoff
- Environment variable configuration with optional parameter overrides
- Full type hints for better IDE support
Installation
```bash pip install tinysdk ```
Quick Start
TinyDBService (MongoDB)
```python from tinysdk import TinyDBService
Initialize with environment variables (DB_LINK, DB_NAME)
db = TinyDBService()
Or provide credentials explicitly
db = TinyDBService( db_link="mongodb://localhost:27017", db_name="mydb" )
Create a document
result = db.create({ "collection": "users", "document": {"name": "John", "email": "john@example.com"} })
Read documents with filtering, sorting, and paging
result = db.read({ "collection": "users", "filter": {"name": "John"}, "sort": {"field": "email", "direction": 1}, "paging": {"page": 0, "limit": 10} })
Update documents
db.update({ "collection": "users", "filter": {"name": "John"}, "data": {"$set": {"email": "newemail@example.com"}} })
Delete documents
db.delete({ "collection": "users", "filter": {"name": "John"} }) ```
TinyStorageHandler (MinIO)
```python from tinysdk import TinyStorageHandler import io
Initialize with environment variables (MINIO_ENDPOINT, MINIO_ACCESSKEY, MINIO_SECRETKEY)
storage = TinyStorageHandler()
Or provide credentials explicitly
storage = TinyStorageHandler( endpoint="localhost:9000", access_key="minioadmin", secret_key="minioadmin" )
Upload a file from buffer
file_buffer = io.BytesIO(b"file contents") storage.upload_file("my-bucket", "path/to/file.txt", file_buffer, file_buffer.getbuffer().nbytes)
Download a file (returns tuple of buffer and object name)
file_buffer, object_name = storage.download_file("my-bucket", "path/to/file.txt")
List objects
objects = storage.list_objects("my-bucket", prefix="path/")
Check if object exists
exists = storage.object_exists("my-bucket", "path/to/file.txt")
Delete a file
storage.delete_file("my-bucket", "path/to/file.txt") ```
TinyMessageService (Async RabbitMQ)
```python from tinysdk import TinyMessageService
def process_message(msg: dict): print(f"Received: {msg}")
Initialize with environment variables (RABBITMQ_HOST, RABBITMQ_PORT, RABBITMQ_USER, RABBITMQ_PASS)
queue_settings = [ ("my-queue", process_message), ] msg_service = TinyMessageService(queue_settings)
Or provide credentials explicitly
msg_service = TinyMessageService( queue_settings=queue_settings, host="localhost", port=5672, user="guest", password="guest" )
Publish a message
msg_service.publish("my-queue", {"task": "process_data", "id": 123})
Start consuming (blocks)
msg_service.run() ```
TinyMessageServiceSync (Sync RabbitMQ)
```python from tinysdk import TinyMessageServiceSync
Initialize
msg_service = TinyMessageServiceSync()
Publish a message
msg_service.publish("my-queue", {"task": "process_data"})
Consume messages (blocks)
def process_message(msg: dict): print(f"Received: {msg}")
msg_service.consume("my-queue", process_message) ```
Environment Variables
TinySDK reads credentials from environment variables by default:
MongoDB (TinyDBService)
- `DB_LINK` - MongoDB connection string (e.g., `mongodb://mongo-server:27017`)
- `DB_NAME` - Database name (e.g., `tinyDatabase`)
MinIO (TinyStorageHandler)
- `MINIO_ENDPOINT` - MinIO server endpoint (e.g., `minio-server:9000`)
- `MINIO_ACCESSKEY` - Access key
- `MINIO_SECRETKEY` - Secret key
RabbitMQ (TinyMessageService + TinyMessageServiceSync)
- `RABBITMQ_HOST` - RabbitMQ server hostname (e.g., `rabbitmq-server`)
- `RABBITMQ_PORT` - RabbitMQ port (default: `5672`)
- `RABBITMQ_USER` - Username (e.g., `myuser`)
- `RABBITMQ_PASS` - Password
Advanced Usage
Custom Index Configuration
```python from pymongo import ASCENDING, DESCENDING
custom_indexes = { "users": [ [("email", ASCENDING), {"unique": True}], [("created_at", DESCENDING)] ], "orders": [ [("user_id", ASCENDING)], [("status", ASCENDING), ("created_at", DESCENDING)] ] }
db = TinyDBService(index_config=custom_indexes) ```
Custom HTTP Client for MinIO
```python from urllib3 import PoolManager from urllib3.util.retry import Retry from urllib3.util import Timeout from datetime import timedelta
http = PoolManager( timeout=Timeout(connect=timedelta(minutes=5).seconds, read=timedelta(minutes=5).seconds), maxsize=100, retries=Retry(total=5, backoff_factor=0.2, status_forcelist=[500, 502, 503, 504]), )
storage = TinyStorageHandler(http_client=http) ```
Utility Functions
```python from tinysdk.utils import get_date, is_valid, exception_logger, retry
Get current UTC timestamp
timestamp = get_date() # Returns ISO 8601 format
Validate dictionary keys
data = {"name": "John", "email": "john@example.com"} if is_valid(data, ["name", "email"]): print("All required fields present")
Use decorators
@exception_logger def my_function(): # Automatically logs exceptions pass
@retry(exceptions=(ConnectionError,), tries=3, delay=2) def flaky_operation(): # Retries on ConnectionError with exponential backoff pass ```
Migration from Duplicated Code
If you're migrating from duplicated service files:
-
Install TinySDK: ```bash pip install tinysdk ```
-
Update imports: ```python
Before:
from src.tiny_db_service import TinyDBService from src.storage_handler import TinyStorageHandler from src.msg_service import TinyMessageService
After:
from tinysdk import TinyDBService, TinyStorageHandler, TinyMessageService
Or for sync messaging:
from tinysdk import TinyMessageServiceSync ```
-
Remove old files: ```bash rm src/tiny_db_service.py src/storage_handler.py src/msg_service.py ```
-
Update requirements.txt: ``` tinysdk>=0.1.0 ```
Development
Setup
```bash git clone https://github.com/SynapzeGmbH/TinySDK.git cd TinySDK python3 -m venv venv source venv/bin/activate pip install -e ".[dev]" ```
Running Tests
```bash pytest tests/ -v ```
Code Formatting
```bash ruff format src/ tests/ ruff check src/ tests/ --fix ```
License
MIT License - see LICENSE for details.
Contributing
Contributions are welcome! Please ensure:
- Code follows the existing style (ruff formatting)
- All tests pass
- New features include tests and documentation
Support
For issues and questions, please open an issue on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tinysdk-0.1.0.tar.gz.
File metadata
- Download URL: tinysdk-0.1.0.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73c108e3894e6659f4d5f29a6e81046de81623dc1b8defaf93af5eb18b1abd71
|
|
| MD5 |
a0c2d7a44307d9e7e93d803e2429da70
|
|
| BLAKE2b-256 |
dfce3af7cf3ce298cf076f1928d7b1c15a7884281a041e358ae194b169389538
|
Provenance
The following attestation bundles were made for tinysdk-0.1.0.tar.gz:
Publisher:
publish.yml on synapzegmbh/TinySDK
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tinysdk-0.1.0.tar.gz -
Subject digest:
73c108e3894e6659f4d5f29a6e81046de81623dc1b8defaf93af5eb18b1abd71 - Sigstore transparency entry: 804617424
- Sigstore integration time:
-
Permalink:
synapzegmbh/TinySDK@a1ad6b0f5ea162f3db84fe7d72eb9a6a114293ee -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/synapzegmbh
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a1ad6b0f5ea162f3db84fe7d72eb9a6a114293ee -
Trigger Event:
push
-
Statement type:
File details
Details for the file tinysdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tinysdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58ba1a69b1098b78e3ab1a30d4eb0893c04e3905d05e5cc19103d4e24dcf113c
|
|
| MD5 |
c330469cb670f0ebd949be07d12a4574
|
|
| BLAKE2b-256 |
f2d466f8b760378b3524c738c91fc4f1d252d3d1cdf6c297488e6f38dc5079c2
|
Provenance
The following attestation bundles were made for tinysdk-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on synapzegmbh/TinySDK
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tinysdk-0.1.0-py3-none-any.whl -
Subject digest:
58ba1a69b1098b78e3ab1a30d4eb0893c04e3905d05e5cc19103d4e24dcf113c - Sigstore transparency entry: 804617427
- Sigstore integration time:
-
Permalink:
synapzegmbh/TinySDK@a1ad6b0f5ea162f3db84fe7d72eb9a6a114293ee -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/synapzegmbh
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a1ad6b0f5ea162f3db84fe7d72eb9a6a114293ee -
Trigger Event:
push
-
Statement type: