Common utilities for Earnbase services
Project description
Earnbase Common Module
Common utilities and shared functionality for Earnbase Platform services.
Installation
Development Environment
# Install from local source
cd earnbase-common
pdm install -e .
Production Environment
# Install from private registry
pdm add earnbase-common
Service Configuration
Add dependency to pyproject.toml:
[project]
dependencies = [
"earnbase-common @ {root:uri}/earnbase-common", # For development
# or
"earnbase-common>=0.1.0", # For production
]
Using MongoDB Client
Initialize Connection
from earnbase_common.database import mongodb
from earnbase_common.logging import get_logger
logger = get_logger(__name__)
async def startup():
"""Initialize MongoDB connection when service starts."""
await mongodb.connect(
url="mongodb://localhost:27017",
db_name="earnbase",
min_pool_size=10,
max_pool_size=100
)
async def shutdown():
"""Close MongoDB connection when service shuts down."""
await mongodb.close()
Collection Operations
# Find one document
user = await mongodb.find_one(
collection="users",
query={"email": "user@example.com"}
)
# Find multiple documents
users = await mongodb.find_many(
collection="users",
query={"is_active": True},
sort=[("created_at", -1)],
skip=0,
limit=10
)
# Insert document
user_id = await mongodb.insert_one(
collection="users",
document={
"email": "new@example.com",
"name": "New User"
}
)
# Update document
modified_count = await mongodb.update_one(
collection="users",
query={"_id": user_id},
update={
"$set": {"is_active": False}
}
)
# Delete document
deleted_count = await mongodb.delete_one(
collection="users",
query={"_id": user_id}
)
# Count documents
total = await mongodb.count_documents(
collection="users",
query={"is_active": True}
)
Index Management
# Create index
await mongodb.create_index(
collection="users",
keys=[("email", 1)],
unique=True
)
# Drop index
await mongodb.drop_index(
collection="users",
index_name="email_1"
)
# List indexes
indexes = await mongodb.list_indexes(collection="users")
Error Handling
from pymongo.errors import PyMongoError
try:
await mongodb.insert_one(collection="users", document=user_data)
except PyMongoError as e:
logger.error(
"mongodb_error",
operation="insert_user",
error=str(e),
error_type=type(e).__name__
)
raise
Logging
Logging Configuration
from earnbase_common.logging import configure_logging, get_logger
# Configure logging when service starts
configure_logging()
# Use logger
logger = get_logger(__name__)
logger.info("user_created", user_id=user_id, email=email)
logger.error("operation_failed", error=str(error))
Error Handling
Using Exception Handlers
from earnbase_common.errors import (
APIError,
ValidationError,
NotFoundError,
register_error_handlers
)
from fastapi import FastAPI
app = FastAPI()
# Register error handlers
register_error_handlers(app)
# Use exceptions
async def get_user(user_id: str):
user = await mongodb.find_one("users", {"_id": user_id})
if not user:
raise NotFoundError(
message="User not found",
details={"user_id": user_id}
)
return user
Contributing
- Fork repository
- Create feature branch
- Commit changes
- Push to branch
- Create Pull Request
License
MIT License
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
earnbase_common-0.1.13.tar.gz
(14.7 kB
view details)
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 earnbase_common-0.1.13.tar.gz.
File metadata
- Download URL: earnbase_common-0.1.13.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.1 CPython/3.10.16 Linux/6.8.0-49-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e795daae5af7174e0963cb2fd8906f38103f6ffebb18af648bde383227b8dc93
|
|
| MD5 |
01a0d4a23d2e17aa5262edbbfff34d02
|
|
| BLAKE2b-256 |
f2c29364f5d7509225fc040592c91a25469dfa89be1bfeffacf90b600eecfdc0
|
File details
Details for the file earnbase_common-0.1.13-py3-none-any.whl.
File metadata
- Download URL: earnbase_common-0.1.13-py3-none-any.whl
- Upload date:
- Size: 21.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.22.1 CPython/3.10.16 Linux/6.8.0-49-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6b6896ba9b8b80c0b205c112282e6e9d95d568f3ef7f3a911322983b6fd1a8e
|
|
| MD5 |
061b7b59aaa9080d6d5ccbf676326d67
|
|
| BLAKE2b-256 |
192545a902677ac868447bec4c6489090f968de99f88b7c3b4937b3dbe3cd604
|