Skip to main content

Shared library for microservice

Project description

FDC Shared Kernel

Shared Kernel is a lightweight, modular Python library designed to facilitate rapid development of microservices. It provides essential utilities for data manipulation, logging, configuration management, and database connectivity, making it an ideal foundation for building scalable and maintainable microservices.

Table of Contents

Getting Started

Prerequisites

  • Python 3.6+
  • Pip

Installation

To install Shared Kernel, clone the repository and install it using pip:

   pip install fdc-shared-kernel

Usage

Importing Modules

Import the required modules from Shared Kernel into your project:

from shared_kernel.logger import Logger
from shared_kernel.config import Config
from dotenv import find_dotenv


def main():
    logger = Logger(name="my_app")
    logger.configure_logger()

    # Specify the path to the .env file if it's not in the current directory
    config_manager = Config(env_path=find_dotenv())

    # Access environment variables
    api_key = config_manager.get("KEY", "default_api_key")

    # Example usage
    logger.logger.info("This is an info message.")
    logger.logger.error("This is an error message.")
    logger.logger.info(api_key)


if __name__ == "__main__":
    main()

Initializing Database Connection

Use the DB class to initialize a database connection:

from shared_kernel.DB import DB

db_instance = DB("postgresql://user:password@localhost/dbname") 
engine, SessionLocal = db_instance.init_db_connection()

Initializing NATS Connection

Use the NATSClient class to initialize:

from shared_kernel.messaging import NATSClient
import asyncio

def run():
  nats_instance = NATSClient("nats://localhost:4222") 
    await nc_interface.connect()

    async def message_callback(data):
        print(f"Received a message: {data}")

    await nc_interface.subscribe("example_subject", message_callback)
    await nc_interface.publish("example_subject", "Hello NATS!")
    
if __name__ == '__main__':
    asyncio.run(run())

Using Keyvault Manager

Use the KeyVaultManager class to initialize:

from shared_kernel.security import KeyVaultManager

def run():
    aws_vault = KeyVaultManager.create_key_vault('aws', {
        'region_name': 'us-east-1',
        'AWS_SERVER_PUBLIC_KEY': '<key here>',
        'AWS_SERVER_SECRET_KEY': '<secret here>'
    })
    # AWS Secrets Manager operations
    aws_vault.store_secret("fdc_api_key", "123456")
    
    print(aws_vault.retrieve_secret("fdc_api_key"))  # Output: 123456
    
    print(aws_vault.list_secrets())  # Output: ['fdc_api_key']
    
    aws_vault.delete_secret("api_key")
    print(aws_vault.list_secrets())  # Output: []
if __name__ == '__main__':
    run()

Using JWT Token handler

Use the JWTTokenHandler class to initialize:

from shared_kernel.security import JWTTokenHandler

def run():
  secret_key = 'your-secret-key'
  token_handler = JWTTokenHandler(secret_key)
  payload = token_handler.decode_token('your-jwt-token')

if __name__ == '__main__':
    run()

Using JWTTokenHandler

Use the JWTTokenHandler class to initialize:

from shared_kernel.auth import JWTTokenHandler

def run():
  secret_key = 'your-secret-key'
  token_handler = JWTTokenHandler(secret_key)
  payload = token_handler.decode_token('your-jwt-token')
if __name__ == '__main__':
    run()

In routes

from shared_kernel.auth import token_required

@app.route('/')
@token_required
def user(current_user):
  return jsonify(current_user)

Using HTTP Client

Use the HttpClient class to initialize:

from shared_kernel.http import HttpClient

def run():
  client = HttpClient()
  response_data = client.get("https://api.example.com/data")
  print(response_data)
if __name__ == '__main__':
    run()

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

fdc_shared_kernel-0.0.184.tar.gz (99.4 kB view details)

Uploaded Source

Built Distribution

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

fdc_shared_kernel-0.0.184-py3-none-any.whl (174.9 kB view details)

Uploaded Python 3

File details

Details for the file fdc_shared_kernel-0.0.184.tar.gz.

File metadata

  • Download URL: fdc_shared_kernel-0.0.184.tar.gz
  • Upload date:
  • Size: 99.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.20

File hashes

Hashes for fdc_shared_kernel-0.0.184.tar.gz
Algorithm Hash digest
SHA256 4a53d8ef28b404cacd671dce453d1a7c3fa35d6e9e2c0de6dc2bc88347b5299b
MD5 7a6e0028fd807f208e0baea85638cc4c
BLAKE2b-256 18574d01f1dc9fd04a9a0665ee4b9772a7a328b1744140139741d7dc3a2b2eb5

See more details on using hashes here.

File details

Details for the file fdc_shared_kernel-0.0.184-py3-none-any.whl.

File metadata

File hashes

Hashes for fdc_shared_kernel-0.0.184-py3-none-any.whl
Algorithm Hash digest
SHA256 08e5e53bd4a1b707c7e7ce9325fa929104da0c41d5345ad28b153e3829291f0c
MD5 f3d9362f472f707d89afb75065d1a5e8
BLAKE2b-256 f9ae7c70b10e15320d9c70f7b1fa2d8fcb2d2987f2de6ea766b2968d47dc8ae5

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