Skip to main content

Beta version of JEC API

Project description

JEC-API

Features

  • Class-Based Routes: Group related endpoints into a single class.
  • Strict Method Mapping: Methods named get, post, put, delete, patch, options, or head are automatically mapped to their respective HTTP verbs.
  • Data Object Support: Built-in support for Pydantic models. Type hints are automatically used for request body validation and response model registry.
  • FastAPI Native: Fully compatible with FastAPI dependencies and OpenAPI generation.

Installation

pip install jec-api

Quick Start

  1. Define a Route Class
# routes.py
from pydantic import BaseModel
from jec_api import Route

class UserResponse(BaseModel):
    id: int
    name: str

class CreateUser(BaseModel):
    name: str

class Users(Route):
    # Strictly mapped to GET /users
    async def get(self) -> list[UserResponse]:
        """List all users"""
        return [UserResponse(id=1, name="Alice")]

    # Strictly mapped to POST /users
    async def post(self, data: CreateUser) -> UserResponse:
        """Create a user"""
        return UserResponse(id=2, name=data.name)

class UserDetail(Route):
    # Custom path with parameter
    path = "/users/{user_id}"

    async def get(self, user_id: int) -> UserResponse:
        """Get user by ID"""
        return UserResponse(id=user_id, name="Alice")
  1. Create the App
# main.py
from jec_api import Core

core = Core(title="My API")

# Auto-discover routes from a module/package
core.discover("routes")

# Or register manually
from routes import Users
core.register(Users)
  1. Run it
uvicorn main:core --reload

Usage Guide

Defining Routes

Inherit from jec_api.Route to create a route group. The class name is automatically converted to kebab-case to form the base path (e.g., UserProfiles -> /user-profiles), unless you override it with the path attribute.

Method Mapping

JEC-API maps methods to HTTP verbs based on their exact names:

Method Name HTTP Verb Path
get() GET /
post() POST /
put() PUT /
delete() DELETE /
patch() PATCH /
options() OPTIONS /
head() HEAD /

Note: Methods with other names (e.g., get_users, helper_method) are ignored and will not be registered as API endpoints.

Path Parameters

To define routes with path parameters, override the path attribute on the Route class.

class Users(Route):
    path = "/users/{id}"
    
    async def get(self, id: int):
        return {"id": id}

Data Validation and Objects

JEC-API leverages Pydantic for data validation and schema generation.

  1. Request Body: The type hint of the first non-self parameter is used as the request body.
  2. Response Model: The return type hint is used as the response_model for the OpenAPI schema and serialization.
class Users(Route):
    async def post(self, data: UserSchema) -> UserResponse:
        # data is automatically validated and parsed
        return UserResponse(...)

Manual Registration

You can register routes manually if you prefer not to use auto-discovery:

from my_routes import MyRoute
app.register(MyRoute, tags=["Custom Tag"])

Auto-Discovery

The discover() method recursively searches the specified package for any classes inheriting from Route and registers them.

app.discover("src.routes")

License

MIT License

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

jec_api-0.0.4.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

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

jec_api-0.0.4-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file jec_api-0.0.4.tar.gz.

File metadata

  • Download URL: jec_api-0.0.4.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for jec_api-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a6146373a527e14b9f2dd57f960802706c9656402d1a5f308be66d75fcc4154f
MD5 5f1f067a04cd70896931c3487bf28a68
BLAKE2b-256 4268272ba36b802e51781de7a9013275ccea4e79b3729b2bfbd36ca09552c3df

See more details on using hashes here.

File details

Details for the file jec_api-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: jec_api-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for jec_api-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 250cd36d8c6fad1c92179eb9ab4b081a9a9ed49696b5b30ff4205daa2bf522be
MD5 fb4ad608f7ce379c3e2fa24724290aae
BLAKE2b-256 ec388d3f4a5b38d7dc7a3fa23f0b1536bd65e1afd696ab1af5f18ce4edf77b5d

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