{"id":15527281,"url":"https://github.com/pandede/fastapifactory","last_synced_at":"2026-05-01T01:31:13.693Z","repository":{"id":178890327,"uuid":"662525974","full_name":"Pandede/FastAPIFactory","owner":"Pandede","description":"Some simple utilities for building FastAPI application.","archived":false,"fork":false,"pushed_at":"2023-08-08T05:32:24.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-04T22:17:37.622Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Pandede.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-05T10:17:56.000Z","updated_at":"2023-07-05T10:21:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"60f5ab37-7964-4613-9f16-e0e46daf86b0","html_url":"https://github.com/Pandede/FastAPIFactory","commit_stats":{"total_commits":13,"total_committers":1,"mean_commits":13.0,"dds":0.0,"last_synced_commit":"4d9c9e991f72102d5ea2fed95da467ec62b9f1c4"},"previous_names":["pandede/fastapifactory"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pandede%2FFastAPIFactory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pandede%2FFastAPIFactory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pandede%2FFastAPIFactory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Pandede%2FFastAPIFactory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Pandede","download_url":"https://codeload.github.com/Pandede/FastAPIFactory/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246100581,"owners_count":20723479,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-02T11:05:22.183Z","updated_at":"2026-05-01T01:31:13.661Z","avatar_url":"https://github.com/Pandede.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI-Factory\n\nSome simple utilities for building `FastAPI` application.\n\n## Installation\nInstall via `pip`\n```bash\npip install git+https://github.com/Pandede/fastapi-factory\n```\n\n## Usage\n### Exports the metrics to `Prometheus`\n```python\nfrom fastapi import FastAPI\nfrom fastapi_factory import set_prometheus_exporter\n\n\napp = FastAPI()\nset_prometheus_exporter(app)\n```\n\n### Add a shared instance across the application\n\nThe instance can be accessed in endpoints or middlewares with attribute `request`.\n\nFor example, assume `redis.Redis` is going to be shared across the application, in `main.py`:\n```python\nfrom fastapi import FastAPI\nfrom fastapi_factory import set_shared_object\nfrom redis import Redis\n\nfrom routers import user_router\napp = FastAPI()\n\n# Share `redis.Redis`\nredis_client = Redis()\nset_shared_object(app, redis_client, 'redis')\n```\nThen, access the shared instance in the endpoint, in `routers.py`:\n```python\nfrom fastapi import APIRouter, Request\nfrom fastapi_factory import get_shared_object\n\nuser_router = APIRouter()\n\n@user_router.get('/user')\ndef get_info(request: Request, user: str) -\u003e str:\n    redis_client = get_shared_object(request, 'redis')\n    return redis_client.get(user)\n\n@user_router.post('/user')\ndef set_info(request: Request, user: str, info: str):\n    redis_client = get_shared_object(request, 'redis')\n    return redis_client.set(user, info)\n```\n\n### Catch the exception with specific status code\n```python\nfrom fastapi import FastAPI, status\nfrom fastapi.testclient import TestClient\n\nfrom fastapi_factory import set_exception_status\n\napp = FastAPI()\nset_exception_status(app, ZeroDivisionError, status.HTTP_400_BAD_REQUEST)\nset_exception_status(app, ValueError, status.HTTP_406_NOT_ACCEPTABLE)\n\n\n@app.get('/inv')\ndef get_inv(n):\n    if n == 'nan':\n        raise TypeError('nan')\n    return 1 / float(n)\n\n\ntest_client = TestClient(app, raise_server_exceptions=False)\n\n# Normal request\nres = test_client.get('/inv', params={'n': 4})\nassert res.status_code == status.HTTP_200_OK\nassert res.json() == 0.25\n\n# Zero, must raise ZeroDivisionError\nres_with_zero_div = test_client.get('/inv', params={'n': 0})\nassert res_with_zero_div.status_code == status.HTTP_400_BAD_REQUEST\n\n# String, must raise ValueError\nres_with_val_err = test_client.get('/inv', params={'n': 'foo'})\nassert res_with_val_err.status_code == status.HTTP_406_NOT_ACCEPTABLE\nassert res_with_val_err.json() == {'detail': 'could not convert string to float: \\'foo\\''}\n\n# nan, must raise TypeError\n# Returns 500 as it's unexpected error\nres_with_type_err = test_client.get('/inv', params={'n': 'nan'})\nassert res_with_type_err.status_code == status.HTTP_500_INTERNAL_SERVER_ERROR\n```\n\n### Create a simple homepage\n```python\nfrom fastapi import FastAPI, status\nfrom fastapi.testclient import TestClient\n\nfrom fastapi_factory import set_home\n\napp = FastAPI(title='fastapi_factory', version='0.1.0')\nset_home(app)\n\ntest_client = TestClient(app)\nres = test_client.get('/')\nassert res.status_code == status.HTTP_200_OK\nassert res.json() == {'message': 'Hello from fastapi_factory 0.1.0!'}\n```\n\n## Test\nInstall the following packages before testing:\n```bash\npip install pytest pytest-cov pytest-mock\n```\nExecuting the tests:\n```bash\npython3 -m pytest tests --cov\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpandede%2Ffastapifactory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpandede%2Ffastapifactory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpandede%2Ffastapifactory/lists"}