Skip to content

MarkHoo/pureapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PureAPI

A lightweight and elegant Python web framework for building modern APIs.

Python License

Features

  • 🚀 Modern Routing - Type-annotated path parameters with automatic conversion
  • 📖 OpenAPI Support - Built-in Swagger UI and ReDoc documentation
  • 🎯 Simple Design - Intuitive API that's easy to learn and use
  • 🔧 WSGI Compatible - Works with any WSGI server (Gunicorn, uWSGI, etc.)
  • 📦 Zero Dependencies - Uses only Python standard library

Installation

pip install pureapi

Quick Start

from pureapi import PureAPI

app = PureAPI(title="My API", version="1.0.0")

@app.get("/")
def root():
    """Welcome endpoint."""
    return {"message": "Hello, World!"}

@app.get("/users/{user_id:int}")
def get_user(user_id: int):
    """Get user by ID."""
    return {"user_id": user_id}

@app.post("/users")
def create_user(request):
    """Create a new user."""
    data = request.json
    return {"created": True, "data": data}

if __name__ == "__main__":
    app.run()  # Runs on http://127.0.0.1:8888

API Documentation

Once your app is running, visit:

Path Parameters

# String parameter (default)
@app.get("/items/{item_id}")
def get_item(item_id: str):
    return {"item_id": item_id}

# Integer parameter
@app.get("/users/{user_id:int}")
def get_user(user_id: int):
    return {"user_id": user_id}

# Float parameter
@app.get("/prices/{price:float}")
def get_price(price: float):
    return {"price": price}

Request Handling

from pureapi import PureAPI, Request

app = PureAPI()

@app.post("/data")
def handle_data(request: Request):
    # JSON body
    data = request.json
    
    # Query parameters
    params = request.query_params
    
    # Headers
    headers = request.headers
    
    return {"received": data}

Error Handling

from pureapi import HTTPException

@app.get("/items/{item_id:int}")
def get_item(item_id: int):
    if item_id < 0:
        raise HTTPException(status_code=400, detail="Invalid item ID")
    return {"item_id": item_id}

# Custom exception handler
@app.exception_handler(404)
def not_found(request, exc):
    return {"error": "Not found", "path": request.path}

Sub-Routers

from pureapi import PureAPI, Router

app = PureAPI()
api_router = Router()

@api_router.get("/users")
def list_users():
    return []

app.include_router(api_router, prefix="/api/v1")
# Route: /api/v1/users

Running in Production

# With Gunicorn
gunicorn myapp:app -w 4 -b 0.0.0.0:8888

# With uWSGI
uwsgi --http :8888 --wsgi-file myapp.py --callable app

Documentation

See the docs folder for detailed documentation.

License

MIT License - see LICENSE for details.

About

A lightweight and elegant Python web framework for building modern APIs.

Resources

License

Stars

6 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages