This project/library contains useful elements related to APIs...
Project description
core-apis
This project/library contains useful elements related to APIs and provides basic structures to speed up the implementation of an API service using the FastApi framework as base…
Installation
Install from PyPI using pip:
pip install core-apis
uv pip install core-apis # Or using UV...
Features
FastAPI Server Management
Quick server setup with uvicorn ASGI server
Environment-based configuration (HOST, PORT, DEBUG, LOG_LEVEL)
Secure defaults (localhost-only binding)
Application Factory
AppConfig dataclass consolidating all application parameters (name, version, description, base_path, cors_config, debug)
CorsConfig dataclass for CORS middleware (origins, methods, headers)
create_application(app_config: AppConfig) factory with safe defaults
Router Management
Plugin architecture: register routers at import time via add_router()
Runtime addition: attach routers to an existing app via app.include_router()
reset_routers() for clearing the registry between tests
Built-in health check endpoint (/api/ping)
Response Standardization
@wrap_response decorator for consistent API responses
ApiResponse TypedDict contract: code, status, result, error
HTTP status code mapping (2XX success, 4XX error, 5XX failure)
Automatic exception handling for ServiceException and InternalServerError
Testing Utilities
BaseApiTestCases class for FastAPI testing with pre-configured TestClient
setUp() calls reset_routers() automatically to prevent cross-test pollution
init_client(with_cors=True) for flexible test application initialization
How to use it
The simple way to spin up the FastAPI server locally using uvicorn:
# -*- coding: utf-8 -*-
from core_apis.api import server
server.run()
Creating an application with custom configuration (AppConfig / CorsConfig):
# -*- coding: utf-8 -*-
from core_apis.api import create_application, AppConfig, CorsConfig
app = create_application(
app_config=AppConfig(
name="My Service",
version="1.0.0",
cors_config=CorsConfig(
enabled=True,
origins=["https://example.com"],
),
),
)
Adding custom routers — plugin architecture (recommended):
Use add_router() to register routers at import time. They are collected by create_application() when the app is built. This is the recommended pattern for plugins and reusable components that don’t need a reference to the app instance.
# -*- coding: utf-8 -*-
from fastapi import APIRouter
from core_apis.api import server
from core_apis.api.routers import add_router
router = APIRouter()
add_router(router)
@router.get(path="/server_status")
def get_server_status():
return {"status": "OK"}
server.run()
Adding routers at runtime (post-creation):
Use app.include_router() directly on the FastAPI instance after the application has already been created. Useful for dynamic or conditional route registration.
# -*- coding: utf-8 -*-
from fastapi import APIRouter
from core_apis.api import create_application
app = create_application()
late_router = APIRouter()
@late_router.get(path="/status")
def get_status():
return {"status": "OK"}
app.include_router(late_router, prefix="/api")
Using @wrap_response — standardized response contract (ApiResponse):
# -*- coding: utf-8 -*-
from core_https import HTTPStatus
from core_apis.decorators import wrap_response
@wrap_response
def get_item(item_id: int):
return HTTPStatus.OK, {"id": item_id, "name": "Widget"}
# Returns an ApiResponse dict:
# {"code": 200, "status": "success", "result": {"id": 1, "name": "Widget"}, "error": None}
For an example of the structure of a production-ready project check: https://gitlab.com/bytecode-solutions/examples/fastapi-project
Quick Start
Installation
Install the package:
pip install core-apis
uv pip install core-apis # Or using UV...
pip install -e ".[dev]" # For development...
Setting Up Environment
Install required libraries:
pip install --upgrade pip
pip install virtualenv
Create Python virtual environment:
virtualenv --python=python3.12 .venv
Activate the virtual environment:
source .venv/bin/activate
Install packages
pip install .
pip install -e ".[dev]"
Check tests and coverage
python manager.py run-tests
python manager.py run-coverage
Contributing
Contributions are welcome! Please:
Fork the repository
Create a feature branch
Write tests for new functionality
Ensure all tests pass: pytest -n auto
Run linting: pylint core_apis
Run security checks: bandit -r core_apis
Submit a pull request
License
This project is licensed under the MIT License. See the LICENSE file for details.
Links
Support
For questions or support, please open an issue on GitLab or contact the maintainers.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file core_apis-2.0.1.tar.gz.
File metadata
- Download URL: core_apis-2.0.1.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd635bad7ace3ff537cfcd6c53b90f337ccc355fed15c9dc8d3d16190e819fdb
|
|
| MD5 |
efd13ebbc0757bde4f2907459f0fe37f
|
|
| BLAKE2b-256 |
c10e4875bdd8119854300d165f548823068b2f84d1ed7cbb8488a406cbd02e7d
|
File details
Details for the file core_apis-2.0.1-py3-none-any.whl.
File metadata
- Download URL: core_apis-2.0.1-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c53d22bfd570e26605150d48bc38b9f37ff1f670af1521bbb8653f3bf694d4c8
|
|
| MD5 |
182819c3d6997bb84e3a45cca24aef77
|
|
| BLAKE2b-256 |
a060fbc232ea4eff796bdd1772eb79abc050f3cb5f3ae5a37d8fe98fbf49c64e
|