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.2.tar.gz (9.0 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.2-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jec_api-0.0.2.tar.gz
  • Upload date:
  • Size: 9.0 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.2.tar.gz
Algorithm Hash digest
SHA256 6dbd89ba5e862e1b7d21d11e4906a4ab6df5f64722d213e3839a45b957950f04
MD5 79df07f501dbe02fdac60711a387cade
BLAKE2b-256 9db1b48dd3d900aea386f61761789c1d8ec39249549f7a97d64217f94549e930

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jec_api-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.6 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 94a7f1575a9efb771c795b2c25e0f58a087830dcbd21c1bc9809bd539b8bbd28
MD5 5c9730e87649d174b70483c06c4b434a
BLAKE2b-256 c195baeb969beaf50579cf8a0fcbdf93f14e62335e25627ec9790bbde266e5cd

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