Internal mobile library for OSDental
Project description
osdental-mobile-library
Librería interna compartida para los microservicios móviles de OSDental. Provee conexión a base de datos, autenticación vía gRPC, cliente HTTP, decoradores de auditoría y manejo de excepciones estandarizado.
Versión: 0.3.1 · Python: ≥ 3.10
Instalación
pip install osdental-mb-library
Variables de entorno requeridas
| Variable | Descripción |
|---|---|
APPLICATIONINSIGHTS_CONNECTION_STRING |
Connection string de Azure Monitor |
SECURITY_GRPC_ENDPOINT |
Host del servicio de autenticación gRPC |
GRPC_SECURE |
true para canal TLS, false para inseguro |
ENVIRONMENT |
Nombre del entorno (dev, staging, prod) |
MICROSERVICE_NAME |
Nombre del microservicio que usa la librería |
MICROSERVICE_VERSION |
Versión del microservicio |
Módulos
Database.Connection
Singleton asíncrono para conexiones SQL Server via SQLAlchemy. Soporta connection string directa o Azure Managed Identity.
from osdentalMobileLibrary.Database.Connection import Connection
# Con connection string
db = Connection(db_url="mssql+aioodbc://user:pass@server/db?driver=...")
# Con Azure Managed Identity
db = Connection(db_url="mssql+aioodbc://server/db?...", use_managed_identity=True)
Métodos disponibles:
# Retorna el primer valor escalar
value = await db.execute_query_return_first_value(query, params)
# Retorna lista de dicts (o un dict si fetchone=True)
rows = await db.execute_query_return_data(query, params, fetchone=False)
# Ejecuta y valida STATUS_CODE, retorna STATUS_MESSAGE
msg = await db.execute_query_return_message(query, params, code="SUCCESS")
# Ejecuta sin retorno, o valida STATUS_CODE si se provee code
await db.execute_query(query, params, code=None)
# Ejecuta múltiples queries en una sola transacción
result = await db.execute_transaction_queries(
data=[{"query": "...", "params": {}, "code": "SUCCESS"}],
return_data=False
)
El pool está configurado con
pool_size=20,max_overflow=40,pool_recycle=3600.
Decorators
AuditLogMobile
Decorador para resolvers GraphQL (Strawberry). Valida el Bearer token vía gRPC, crea un span de OpenTelemetry en Azure Monitor y bloquea requests no autenticados.
from osdentalMobileLibrary.Decorators.AuditLogMobile import AuditLogMobile
@strawberry.type
class Query:
@strawberry.field
@AuditLogMobile()
async def get_patient(self, info: Info) -> Patient:
...
El decorador espera que info.context contenga un objeto request con headers Authorization (Bearer) y Agent-Mobile.
grpc_retry / rest_retry
Reintentos automáticos con backoff exponencial (3 intentos, espera 1–4 s).
from osdentalMobileLibrary.Decorators.Retry import grpc_retry, rest_retry
@grpc_retry # reintenta en grpc.RpcError
async def call_grpc(...): ...
@rest_retry # reintenta en httpx.RequestError
async def call_rest(...): ...
ExternalHttp.APIClient
Cliente HTTP asíncrono (extiende httpx.AsyncClient) con retry automático y logging a Service Bus.
from osdentalMobileLibrary.ExternalHttp.Client import APIClient
async with APIClient() as client:
# REST
data = await client.rest_request("GET", "https://api.example.com/resource")
# GraphQL
data = await client.graphql_request(
url="https://api.example.com/graphql",
consult="query { patients { id } }",
variables={},
headers={"Authorization": "Bearer ..."}
)
Cada llamada envía automáticamente el request y el response al Service Bus para auditoría.
Grpc.Client.AuthClient
Cliente gRPC asíncrono para validar tokens contra el servicio de autenticación.
from osdentalMobileLibrary.Grpc.Client.AuthClient import AuthClient
async with AuthClient() as auth:
response = await auth.validate_auth_token(
bearer_token="eyJ...",
agent_mobile="ios-v2.1"
)
if not response.Data:
# Token inválido o sin acceso
...
Exception
Jerarquía de excepciones tipadas. Todas extienden OSDException e incluyen message, error y status_code.
| Excepción | Uso |
|---|---|
OSDException |
Base — error genérico de la aplicación |
UnauthorizedException |
Token inválido o sin permisos |
RequestDataException |
Parámetros de request inválidos |
DatabaseException |
Error en ejecución de query |
HttpClientException |
Error en llamada HTTP externa |
AzureException |
Error en servicios Azure |
ValidationDataException |
Error de validación de datos |
MissingFieldException |
Campo requerido ausente |
InvalidFormatException |
Formato de dato incorrecto |
UnexpectedException |
Error inesperado genérico |
from osdentalMobileLibrary.Exception.ControlledException import DatabaseException
raise DatabaseException(message="Query falló", error=str(e))
Estructura del proyecto
src/osdentalMobileLibrary/
├── Database/ # Conexión async a SQL Server
├── Decorators/ # AuditLogMobile, grpc_retry, rest_retry
├── Exception/ # Excepciones tipadas
├── ExternalHttp/ # APIClient HTTP/GraphQL
├── Grpc/
│ ├── Base/ # GrpcClientBase
│ ├── Client/ # AuthClient
│ └── Generated/ # Protobuf generado
├── Models/ # Response model
├── ServicesBus/ # TaskQueue para Service Bus
└── Shared/ # Config, Logger, Enums, Utils, Azure
Build y distribución
python -m build
# Genera dist/osdental_mb_library-x.x.x-py3-none-any.whl
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 osdental_mb_library-0.3.2.tar.gz.
File metadata
- Download URL: osdental_mb_library-0.3.2.tar.gz
- Upload date:
- Size: 27.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f602f99e31be9b5575f52762876d997f16c95d44e84080bcd7cbd39d7717e395
|
|
| MD5 |
bd4fa2a348922dbafb975cdb402d6ebb
|
|
| BLAKE2b-256 |
ff8644d63d0fef08956dee7592000224ebbbc04f2920e409d301172659b6c9e9
|
File details
Details for the file osdental_mb_library-0.3.2-py3-none-any.whl.
File metadata
- Download URL: osdental_mb_library-0.3.2-py3-none-any.whl
- Upload date:
- Size: 35.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eee5537642dd4f2101b769906feaae1b27c2eaed77458a85280c782a1e252eb4
|
|
| MD5 |
d6e3357732d796f160bb0f9da0038df8
|
|
| BLAKE2b-256 |
42a66737dede88e264fe0e98383d98c2393150e40b82257a752e324486e2f2ae
|