Skip to main content

Pundra: Your FastAPI Companion for Productivity

Project description

FastAPI Pundra

PyPI version Python 3.10+ License: MIT

Pundra: Your FastAPI Companion for Productivity

FastAPI Pundra is a comprehensive toolkit that extends FastAPI with essential utilities, helpers, and integrations to accelerate your API development. Whether you're building REST APIs or GraphQL endpoints, Pundra provides the building blocks you need to create robust, scalable applications.

๐Ÿš€ Features

๐Ÿ”ง Common Utilities

  • Password Management: Secure password hashing and verification using bcrypt
  • JWT Utilities: Token generation and validation helpers
  • Raw SQL Support: Execute raw SQL queries with ease
  • Helper Functions: Common utility functions for path management and more

๐Ÿ“ง Email System

  • Template-based Emails: HTML email templates with inline CSS support
  • Background Email Tasks: Asynchronous email sending with Celery integration
  • Mail Queue Management: Reliable email delivery with queue support
  • Multiple Recipients: Support for CC, BCC, and reply-to functionality

๐Ÿ”„ Task Scheduling

  • Celery Integration: Seamless Celery setup and configuration
  • Cron Scheduling: Define and manage scheduled tasks
  • Beat Scheduler: Redis-based beat scheduler for reliable task execution
  • Dynamic Task Discovery: Automatic task module discovery

๐ŸŒ REST API Enhancements

  • Exception Handling: Comprehensive exception classes for different HTTP status codes
  • Global Exception Handler: Centralized error handling for your FastAPI app
  • Pagination: Built-in pagination with URL generation
  • Validation Helpers: Enhanced request validation utilities

๐Ÿ“ GraphQL Support (Strawberry)

  • Common GraphQL Types: Reusable GraphQL type definitions
  • Pagination Types: Generic paginated list types for GraphQL
  • Resolver Utilities: Helper functions for Strawberry resolvers
  • Validation Integration: GraphQL-specific validation helpers

๐Ÿ“ฆ Installation

pip install fastapi-pundra

๐Ÿ› ๏ธ Quick Start

Basic Setup

from fastapi import FastAPI
from fastapi_pundra.rest.global_exception_handler import setup_exception_handlers
from fastapi_pundra.common.password import generate_password_hash, compare_hashed_password

app = FastAPI()

# Setup global exception handling
setup_exception_handlers(app)

@app.get("/")
async def root():
    return {"message": "Hello from FastAPI Pundra!"}

Password Management

from fastapi_pundra.common.password import generate_password_hash, compare_hashed_password

# Hash a password
password = "my_secure_password"
hashed = generate_password_hash(password)

# Verify a password
is_valid = compare_hashed_password(password, hashed)
print(is_valid)  # True

Email Sending

from fastapi import BackgroundTasks
from fastapi_pundra.common.mailer.mail import send_mail, send_mail_background

# Send email directly
await send_mail(
    subject="Welcome!",
    to=["user@example.com"],
    template_name="welcome_email.html",
    context={"username": "John Doe"}
)

# Send email in background
async def send_welcome_email(background_tasks: BackgroundTasks):
    await send_mail_background(
        background_tasks=background_tasks,
        subject="Welcome!",
        to=["user@example.com"],
        template_name="welcome_email.html",
        context={"username": "John Doe"}
    )

Pagination

from fastapi import Request
from fastapi_pundra.rest.paginate import paginate

@app.get("/users")
async def get_users(request: Request):
    # Assuming you have a SQLAlchemy query
    query = session.query(User)
    return paginate(
        request=request,
        query=query,
        serilizer=UserSerializer,
        the_page=1,
        the_per_page=10
    )

Celery Task Scheduling

from fastapi_pundra.common.scheduler.celery import create_celery_app

# Create Celery app
celery_app = create_celery_app("my_project")

@celery_app.task
def my_scheduled_task():
    print("This task runs on schedule!")
    return "Task completed"

GraphQL with Strawberry

import strawberry
from fastapi_pundra.gql_berry.common_gql_type import PaginatedList

@strawberry.type
class User:
    id: int
    name: str
    email: str

@strawberry.type
class Query:
    @strawberry.field
    def users(self) -> PaginatedList[User]:
        # Your logic here
        return PaginatedList(
            data=[User(id=1, name="John", email="john@example.com")],
            pagination={"page": 1, "total": 1}
        )

๐Ÿ”ง Configuration

Environment Variables

Create a .env file with the following variables:

# Email Configuration
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=your_app_password
MAIL_FROM_ADDRESS=noreply@yourdomain.com
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_SSL_TLS=True
MAIL_STARTTLS=False
MAIL_USE_CREDENTIALS=True

# Celery Configuration
CELERY_BROKER_URL=redis://localhost:6379/0
CELERY_RESULT_BACKEND=redis://localhost:6379/0

# Project Configuration
PROJECT_BASE_PATH=app

๐Ÿ“ Project Structure

fastapi-pundra/
โ”œโ”€โ”€ common/                 # Common utilities
โ”‚   โ”œโ”€โ”€ helpers.py         # Helper functions
โ”‚   โ”œโ”€โ”€ jwt_utils.py       # JWT utilities
โ”‚   โ”œโ”€โ”€ password.py        # Password management
โ”‚   โ”œโ”€โ”€ mailer/            # Email system
โ”‚   โ”‚   โ”œโ”€โ”€ mail.py        # Email sending
โ”‚   โ”‚   โ”œโ”€โ”€ mail_queue.py  # Email queue
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ raw_sql/           # Raw SQL utilities
โ”‚   โ””โ”€โ”€ scheduler/         # Task scheduling
โ”œโ”€โ”€ rest/                  # REST API utilities
โ”‚   โ”œโ”€โ”€ exceptions.py      # Exception classes
โ”‚   โ”œโ”€โ”€ paginate.py        # Pagination
โ”‚   โ””โ”€โ”€ ...
โ””โ”€โ”€ gql_berry/            # GraphQL utilities
    โ”œโ”€โ”€ common_gql_type.py # GraphQL types
    โ”œโ”€โ”€ pagination.py      # GraphQL pagination
    โ””โ”€โ”€ ...

๐Ÿงช Testing

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

๐Ÿ“š Documentation

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ‘จโ€๐Ÿ’ป Author

Mostafa Kamal

๐Ÿ™ Acknowledgments

๐Ÿ“ˆ Roadmap

  • Add more GraphQL utilities
  • Enhanced caching support
  • Database migration helpers
  • API rate limiting
  • WebSocket utilities
  • Enhanced logging system

Made with โค๏ธ for the FastAPI community

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

fastapi_pundra-0.0.19.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

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

fastapi_pundra-0.0.19-py3-none-any.whl (27.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_pundra-0.0.19.tar.gz.

File metadata

  • Download URL: fastapi_pundra-0.0.19.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for fastapi_pundra-0.0.19.tar.gz
Algorithm Hash digest
SHA256 ad5cd30bdd6a7d747347b83ce2a803fea76e284cae50528199ca545b1427af75
MD5 5f553a8a2250ee8a3017fe3ea897bc69
BLAKE2b-256 cb13c991ba396b131ece50507292ce17f119bc71b99984d478dcf072be6d3728

See more details on using hashes here.

File details

Details for the file fastapi_pundra-0.0.19-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_pundra-0.0.19-py3-none-any.whl
Algorithm Hash digest
SHA256 86037c8460b34c5989b7bbe1f213f2b7ac234093c9a987a1686226fcbff94368
MD5 9c570b6a19d8bfa511d4fc6d5bd6bee5
BLAKE2b-256 afe7030c2510329f2c5a905727b71f2834f139e7a46a65bdc45e5d1c508653f6

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