Skip to main content

Python library for PVM

Project description

pvmlib

Una librería personalizada para manejo de logging, validación de tokens, manejo de excepciones y consumo de servicios REST.

Instalación

Para instalar la librería, usa pip:

pip install pvmlib

Implementación

Logger

from pvmlib import logger_singleton, default_logger, application, measurement
from datetime import datetime

# Crear instancias de Application y Measurement
app_info = application(
    name="bs-customer",
    version="1.3.7",
    env="PROD",
    kind="rest-service"
)

measure = measurement(
    method="getClientInfo",
    elapsedTime=50
)

# Crear una entrada de log
log_entry = default_logger(
    level="INFO",
    schemaVersion="1.0.0",
    logType="TRANSACTION",
    sourceIP="10.1.130.25",
    status="SUCCESS",
    message="Obtener info del cliente",
    logOrigin="INTERNAL",
    timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3],
    tracingId="48ad2b64a76dc6ab66da565b6298e0e6fad5b7796a8a35aa6b",
    hostname="server-1",
    eventType="TRANSACTION_PAYMENT_PROCESSING_FAILED",
    application=app_info,
    measurement=measure,
    destinationIP="10.10.129.200",
    additionalInfo={"clientID": 123}
)

# Obtener el logger y registrar la entrada de log
logger = logger_singleton().get_logger()
logger.info(log_entry.json())

Interceptor

from fastapi import FastAPI
from pvmlib import intercept_middleware, application

app = FastAPI()

app_info = application(
    name="bs-customer",
    version="1.3.7",
    env="PROD",
    kind="rest-service"
)

auth_service_url = "https://auth-service-url.com"

app.add_middleware(intercept_middleware, app_info=app_info, auth_service_url=auth_service_url)

RestClient

from pvmlib import call_service_network

base_url = "https://api.example.com"
client = call_service_network(base_url)

# Realizar una solicitud GET
response = client.get("/endpoint", params={"key": "value"}, headers={"Authorization": "Bearer token"})
print(response)

# Realizar una solicitud POST
response = client.post("/endpoint", json={"key": "value"}, headers={"Authorization": "Bearer token"})
print(response)

Manejo de Excepciones

from pvmlib import response_exception

try:
    # Código que puede lanzar una excepción
    raise response_exception(
        error_code="CUSTOM_ERROR",
        message="Ocurrió un error personalizado",
        http_status_code=400,
        headers={"X-Custom-Header": "value"}
    )
except response_exception as e:
    print(e.to_model_dump())

Respuesta Exitosa

from pvmlib import response_ok

response = response_ok(
    status_code=200,
    message="Operación exitosa",
    transaction_id="12345",
    time_elapsed=100,
    data={"key": "value"}
)

print(response.to_model_dump())

Contribución

Si deseas contribuir a este proyecto, por favor abre un issue o envía un pull request en el repositorio de GitHub.

Licencia

Este proyecto está licenciado bajo los términos de la licencia MIT.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pvmlib-1.6.4.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pvmlib-1.6.4-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file pvmlib-1.6.4.tar.gz.

File metadata

  • Download URL: pvmlib-1.6.4.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for pvmlib-1.6.4.tar.gz
Algorithm Hash digest
SHA256 b43bfd3cd1940baa2016b65f629725897653df4a0c23a30848af438dbd83e235
MD5 b4bd443acfc4978115407eb2e1a91b9c
BLAKE2b-256 a752aa6351b7390377afa4c588d0cf1a6b625c77171bb0e6a2c2df2cc800800f

See more details on using hashes here.

File details

Details for the file pvmlib-1.6.4-py3-none-any.whl.

File metadata

  • Download URL: pvmlib-1.6.4-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for pvmlib-1.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 42643cdcbabfa33bf8b3a5401d9d39dc0e61ee5540fddba41dff3e3d0d44ef55
MD5 81739a0b4ce9581cc40a6845f09c2b22
BLAKE2b-256 fbf8ea9e54d5bc9f1dcd244bc490fb066f23418853635bcddc1f5cd821ea70c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page