Name | fasted JSON |
Version |
0.2404.2
JSON |
| download |
home_page | None |
Summary | FastAPI dependencies and utilities. |
upload_time | 2024-04-12 20:29:41 |
maintainer | None |
docs_url | None |
author | Peter Volf |
requires_python | <4.0,>=3.10 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
![Tests](https://github.com/volfpeter/fasted/actions/workflows/tests.yml/badge.svg)
![Linters](https://github.com/volfpeter/fasted/actions/workflows/linters.yml/badge.svg)
![Documentation](https://github.com/volfpeter/fasted/actions/workflows/build-docs.yml/badge.svg)
![PyPI package](https://img.shields.io/pypi/v/fasted?color=%2334D058&label=PyPI%20Package)
**Source code**: [https://github.com/volfpeter/fasted](https://github.com/volfpeter/fasted)
**Documentation and examples**: [https://volfpeter.github.io/fasted](https://volfpeter.github.io/fasted/)
# FastED
FastAPI dependencies and utilities.
## Installation
The package is available on PyPI and can be installed with:
```console
$ pip install fasted
```
## Features
### `selfdependent`
Decorator that let's you use your business objects' instance methods as FastAPI dependencies without writing any additional code.
Supports:
- **Sync and async** instance **methods**.
- **Sync and async generator** methods.
- **Inheritence** and **`super()`** calls is decorated methods.
- An **optional factory** (FastAPI dependency) for creating the `self` instance. If not set, the class' `__init__()` method will serve as the dependency for creating the `self` instance.
- **Decorated** instance **methods will behave as expected** if called directly.
Example use:
```python
from typing import Annotated
from fastapi import Depends, FastAPI
from fasted import selfdependent
def double() -> "Multiplier":
# Dependency that returns a Multiplier with base = 2.
return Multiplier(2)
class Multiplier:
def __init__(self, base: float) -> None:
self.base = base
@selfdependent()
def multiply(self, mul: float) -> float:
# `__init__()` will be used as the dependency to create `self`, so the route
# where this method is used will have a `base` and a `mul` query parameter.
return self.base * mul
@selfdependent(factory=double)
async def double(self, mul: float) -> float:
# `double()` will be used as the dependency to create `self`, so the route
# where this method is used will only have a `mul` query parameter.
return self.base * mul
app = FastAPI()
@app.get("/multiply")
def multiply_route(value: Annotated[float, Depends(Multiplier.multiply)]) -> float:
# FastAPI will create the `Multiplier` instance based on `Multiplier.__init__()` and
# automatically feed this `instance` as `self` to `Multiplier.multiply()` to calculate
# the value of the dependency.
return value
@app.get("/double")
def double_route(value: Annotated[float, Depends(Multiplier.double)]) -> float:
# FastAPI will create the `Multiplier` instance using the `double()` factory (dependency)
# and automatically feed this instance as `self` to `Multiplier.multiply()` to
# calculate the value of the dependency.
return value
```
### `Dependency`
Generic type for FastAPI dependencies.
Example use:
```python
from typing import Annotated, Generator
from fastapi import FastAPI, APIRouter
from fasted import Dependency
# from x import Session
def make_api(make_session: Dependency[Session]) -> APIRouter:
DependsSession = Annotated[Session, Depends(make_session)]
api = APIRouter()
@api.get("/")
def get(session: DependsSession) -> int:
return 4
return api
def make_db_session() -> Generator[Session, None, None]:
with Session(database) as session:
yield session
app = FastAPI()
app.include_router(make_api(make_db_session), prefix="/random-number")
```
## Dependencies
Being a FastAPI utility library, the only dependency is (and will remain) `fastapi`.
## Development
Use `ruff` for linting and formatting, `mypy` for static code analysis, and `pytest` for testing.
The documentation is built with `mkdocs-material` and `mkdocstrings`.
## Contributing
All contributions are welcome.
## License - MIT
The package is open-sourced under the conditions of the [MIT license](https://choosealicense.com/licenses/mit/).
Raw data
{
"_id": null,
"home_page": null,
"name": "fasted",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Peter Volf",
"author_email": "do.volfp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/2c/3b/85486aeff22948f2439483f080843e4431a40100172d8fabcb5cb0ce0b22/fasted-0.2404.2.tar.gz",
"platform": null,
"description": "![Tests](https://github.com/volfpeter/fasted/actions/workflows/tests.yml/badge.svg)\n![Linters](https://github.com/volfpeter/fasted/actions/workflows/linters.yml/badge.svg)\n![Documentation](https://github.com/volfpeter/fasted/actions/workflows/build-docs.yml/badge.svg)\n![PyPI package](https://img.shields.io/pypi/v/fasted?color=%2334D058&label=PyPI%20Package)\n\n**Source code**: [https://github.com/volfpeter/fasted](https://github.com/volfpeter/fasted)\n\n**Documentation and examples**: [https://volfpeter.github.io/fasted](https://volfpeter.github.io/fasted/)\n\n# FastED\n\nFastAPI dependencies and utilities.\n\n## Installation\n\nThe package is available on PyPI and can be installed with:\n\n```console\n$ pip install fasted\n```\n\n## Features\n\n### `selfdependent`\n\nDecorator that let's you use your business objects' instance methods as FastAPI dependencies without writing any additional code.\n\nSupports:\n\n- **Sync and async** instance **methods**.\n- **Sync and async generator** methods.\n- **Inheritence** and **`super()`** calls is decorated methods.\n- An **optional factory** (FastAPI dependency) for creating the `self` instance. If not set, the class' `__init__()` method will serve as the dependency for creating the `self` instance.\n- **Decorated** instance **methods will behave as expected** if called directly.\n\nExample use:\n\n```python\nfrom typing import Annotated\n\nfrom fastapi import Depends, FastAPI\nfrom fasted import selfdependent\n\n\ndef double() -> \"Multiplier\":\n # Dependency that returns a Multiplier with base = 2.\n return Multiplier(2)\n\n\nclass Multiplier:\n def __init__(self, base: float) -> None:\n self.base = base\n\n @selfdependent()\n def multiply(self, mul: float) -> float:\n # `__init__()` will be used as the dependency to create `self`, so the route\n # where this method is used will have a `base` and a `mul` query parameter.\n return self.base * mul\n\n @selfdependent(factory=double)\n async def double(self, mul: float) -> float:\n # `double()` will be used as the dependency to create `self`, so the route\n # where this method is used will only have a `mul` query parameter.\n return self.base * mul\n\n\napp = FastAPI()\n\n\n@app.get(\"/multiply\")\ndef multiply_route(value: Annotated[float, Depends(Multiplier.multiply)]) -> float:\n # FastAPI will create the `Multiplier` instance based on `Multiplier.__init__()` and\n # automatically feed this `instance` as `self` to `Multiplier.multiply()` to calculate\n # the value of the dependency.\n return value\n\n\n@app.get(\"/double\")\ndef double_route(value: Annotated[float, Depends(Multiplier.double)]) -> float:\n # FastAPI will create the `Multiplier` instance using the `double()` factory (dependency)\n # and automatically feed this instance as `self` to `Multiplier.multiply()` to\n # calculate the value of the dependency.\n return value\n```\n\n### `Dependency`\n\nGeneric type for FastAPI dependencies.\n\nExample use:\n\n```python\nfrom typing import Annotated, Generator\n\nfrom fastapi import FastAPI, APIRouter\nfrom fasted import Dependency\n# from x import Session\n\n\ndef make_api(make_session: Dependency[Session]) -> APIRouter:\n DependsSession = Annotated[Session, Depends(make_session)]\n\n api = APIRouter()\n\n @api.get(\"/\")\n def get(session: DependsSession) -> int:\n return 4\n\n return api\n\n\ndef make_db_session() -> Generator[Session, None, None]:\n with Session(database) as session:\n yield session\n\n\napp = FastAPI()\napp.include_router(make_api(make_db_session), prefix=\"/random-number\")\n```\n\n## Dependencies\n\nBeing a FastAPI utility library, the only dependency is (and will remain) `fastapi`.\n\n## Development\n\nUse `ruff` for linting and formatting, `mypy` for static code analysis, and `pytest` for testing.\n\nThe documentation is built with `mkdocs-material` and `mkdocstrings`.\n\n## Contributing\n\nAll contributions are welcome.\n\n## License - MIT\n\nThe package is open-sourced under the conditions of the [MIT license](https://choosealicense.com/licenses/mit/).\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "FastAPI dependencies and utilities.",
"version": "0.2404.2",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a68e60b17fe1ea4c1c95c98042adadf88035ecb1a86d60c8adedafa3edea161a",
"md5": "e1ad77487c8c76889f783a5e495fc851",
"sha256": "d4f59dbc1393817559c5fa0c0e226e72178c1eccb8b0e2c2a143d4d47cac19cb"
},
"downloads": -1,
"filename": "fasted-0.2404.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e1ad77487c8c76889f783a5e495fc851",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 7034,
"upload_time": "2024-04-12T20:29:39",
"upload_time_iso_8601": "2024-04-12T20:29:39.995928Z",
"url": "https://files.pythonhosted.org/packages/a6/8e/60b17fe1ea4c1c95c98042adadf88035ecb1a86d60c8adedafa3edea161a/fasted-0.2404.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2c3b85486aeff22948f2439483f080843e4431a40100172d8fabcb5cb0ce0b22",
"md5": "b3b4da19ee353188068551a916ed9ac1",
"sha256": "f08145ac89edd1a7ac25f1e8a76ae260c524959a5195c23509a103c3a92353fb"
},
"downloads": -1,
"filename": "fasted-0.2404.2.tar.gz",
"has_sig": false,
"md5_digest": "b3b4da19ee353188068551a916ed9ac1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 5790,
"upload_time": "2024-04-12T20:29:41",
"upload_time_iso_8601": "2024-04-12T20:29:41.353291Z",
"url": "https://files.pythonhosted.org/packages/2c/3b/85486aeff22948f2439483f080843e4431a40100172d8fabcb5cb0ce0b22/fasted-0.2404.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-12 20:29:41",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "fasted"
}