openfund-server


Nameopenfund-server JSON
Version 0.4.3 PyPI version JSON
download
home_pageNone
SummaryFastAPI struct
upload_time2024-12-12 04:19:51
maintainerNone
docs_urlNone
authoryang99love
requires_python<4.0,>=3.11
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Openfund-server

# Features
- Async SQLAlchemy session
- Custom user class
- Dependencies for specific permissions
- Celery
- Dockerize(Hot reload)
- Event dispatcher
- Cache

## Run

### Launch docker
```shell
> docker-compose -f docker/docker-compose.yml up
```

### Install dependency
```shell
> poetry shell
> poetry install
```

### Apply alembic revision
```shell
> alembic upgrade head
```

### Run server
```shell
> python3 main.py --env local|dev|prod --debug
```

### Run test codes
```shell
> make test
```

### Make coverage report
```shell
> make cov
```

### Formatting

```shell
> pre-commit
```

## SQLAlchemy for asyncio context

```python
from core.db import Transactional, session


@Transactional()
async def create_user(self):
    session.add(User(email="padocon@naver.com"))
```

Do not use explicit `commit()`. `Transactional` class automatically do.

### Query with asyncio.gather()
When executing queries concurrently through `asyncio.gather()`, you must use the `session_factory` context manager rather than the globally used session.

```python
from core.db import session_factory


async def get_by_id(self, *, user_id) -> User:
    stmt = select(User)
    async with session_factory() as read_session:
        return await read_session.execute(query).scalars().first()


async def main() -> None:
    user_1, user_2 = await asyncio.gather(
        get_by_id(user_id=1),
        get_by_id(user_id=2),
    )
```
If you do not use a database connection like `session.add()`, it is recommended to use a globally provided session.

### Multiple databases

Go to `core/config.py` and edit `WRITER_DB_URL` and `READER_DB_URL` in the config class.


If you need additional logic to use the database, refer to the `get_bind()` method of `RoutingClass`.

## Custom user for authentication

```python
from fastapi import Request


@home_router.get("/")
def home(request: Request):
    return request.user.id
```

**Note. you have to pass jwt token via header like `Authorization: Bearer 1234`**

Custom user class automatically decodes header token and store user information into `request.user`

If you want to modify custom user class, you have to update below files.

1. `core/fastapi/schemas/current_user.py`
2. `core/fastapi/middlewares/authentication.py`

### CurrentUser

```python
class CurrentUser(BaseModel):
    id: int = Field(None, description="ID")
```

Simply add more fields based on your needs.

### AuthBackend

```python
current_user = CurrentUser()
```

After line 18, assign values that you added on `CurrentUser`.

## Top-level dependency

**Note. Available from version 0.62 or higher.**

Set a callable function when initialize FastAPI() app through `dependencies` argument.

Refer `Logging` class inside of `core/fastapi/dependencies/logging.py`

## Dependencies for specific permissions

Permissions `IsAdmin`, `IsAuthenticated`, `AllowAll` have already been implemented.

```python
from core.fastapi.dependencies import (
    PermissionDependency,
    IsAdmin,
)


user_router = APIRouter()


@user_router.get(
    "",
    response_model=List[GetUserListResponseSchema],
    response_model_exclude={"id"},
    responses={"400": {"model": ExceptionResponseSchema}},
    dependencies=[Depends(PermissionDependency([IsAdmin]))],  # HERE
)
async def get_user_list(
    limit: int = Query(10, description="Limit"),
    prev: int = Query(None, description="Prev ID"),
):
    pass
```
Insert permission through `dependencies` argument.

If you want to make your own permission, inherit `BasePermission` and implement `has_permission()` function.

**Note. In order to use swagger's authorize function, you must put `PermissionDependency` as an argument of `dependencies`.**

## Event dispatcher

Refer the README of https://github.com/teamhide/fastapi-event

## Cache

### Caching by prefix
```python
from core.helpers.cache import Cache


@Cache.cached(prefix="get_user", ttl=60)
async def get_user():
    ...
```

### Caching by tag
```python
from core.helpers.cache import Cache, CacheTag


@Cache.cached(tag=CacheTag.GET_USER_LIST, ttl=60)
async def get_user():
    ...
```

Use the `Cache` decorator to cache the return value of a function.

Depending on the argument of the function, caching is stored with a different value through internal processing.

### Custom Key builder

```python
from core.helpers.cache.base import BaseKeyMaker


class CustomKeyMaker(BaseKeyMaker):
    async def make(self, function: Callable, prefix: str) -> str:
        ...
```

If you want to create a custom key, inherit the BaseKeyMaker class and implement the make() method.

### Custom Backend

```python
from core.helpers.cache.base import BaseBackend


class RedisBackend(BaseBackend):
    async def get(self, key: str) -> Any:
        ...

    async def set(self, response: Any, key: str, ttl: int = 60) -> None:
        ...

    async def delete_startswith(self, value: str) -> None:
        ...
```

If you want to create a custom key, inherit the BaseBackend class and implement the `get()`, `set()`, `delete_startswith()` method.

Pass your custom backend or keymaker as an argument to init. (`/app/server.py`)

```python
def init_cache() -> None:
    Cache.init(backend=RedisBackend(), key_maker=CustomKeyMaker())
```

### Remove all cache by prefix/tag

```python
from core.helpers.cache import Cache, CacheTag


await Cache.remove_by_prefix(prefix="get_user_list")
await Cache.remove_by_tag(tag=CacheTag.GET_USER_LIST)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "openfund-server",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": null,
    "author": "yang99love",
    "author_email": "yang99love@hotmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c1/7d/ff3aa496666ce9bb3306c560ea0b547f592e1e64054760c35fa7ace9d0dc/openfund_server-0.4.3.tar.gz",
    "platform": null,
    "description": "# Openfund-server\n\n# Features\n- Async SQLAlchemy session\n- Custom user class\n- Dependencies for specific permissions\n- Celery\n- Dockerize(Hot reload)\n- Event dispatcher\n- Cache\n\n## Run\n\n### Launch docker\n```shell\n> docker-compose -f docker/docker-compose.yml up\n```\n\n### Install dependency\n```shell\n> poetry shell\n> poetry install\n```\n\n### Apply alembic revision\n```shell\n> alembic upgrade head\n```\n\n### Run server\n```shell\n> python3 main.py --env local|dev|prod --debug\n```\n\n### Run test codes\n```shell\n> make test\n```\n\n### Make coverage report\n```shell\n> make cov\n```\n\n### Formatting\n\n```shell\n> pre-commit\n```\n\n## SQLAlchemy for asyncio context\n\n```python\nfrom core.db import Transactional, session\n\n\n@Transactional()\nasync def create_user(self):\n    session.add(User(email=\"padocon@naver.com\"))\n```\n\nDo not use explicit `commit()`. `Transactional` class automatically do.\n\n### Query with asyncio.gather()\nWhen executing queries concurrently through `asyncio.gather()`, you must use the `session_factory` context manager rather than the globally used session.\n\n```python\nfrom core.db import session_factory\n\n\nasync def get_by_id(self, *, user_id) -> User:\n    stmt = select(User)\n    async with session_factory() as read_session:\n        return await read_session.execute(query).scalars().first()\n\n\nasync def main() -> None:\n    user_1, user_2 = await asyncio.gather(\n        get_by_id(user_id=1),\n        get_by_id(user_id=2),\n    )\n```\nIf you do not use a database connection like `session.add()`, it is recommended to use a globally provided session.\n\n### Multiple databases\n\nGo to `core/config.py` and edit `WRITER_DB_URL` and `READER_DB_URL` in the config class.\n\n\nIf you need additional logic to use the database, refer to the `get_bind()` method of `RoutingClass`.\n\n## Custom user for authentication\n\n```python\nfrom fastapi import Request\n\n\n@home_router.get(\"/\")\ndef home(request: Request):\n    return request.user.id\n```\n\n**Note. you have to pass jwt token via header like `Authorization: Bearer 1234`**\n\nCustom user class automatically decodes header token and store user information into `request.user`\n\nIf you want to modify custom user class, you have to update below files.\n\n1. `core/fastapi/schemas/current_user.py`\n2. `core/fastapi/middlewares/authentication.py`\n\n### CurrentUser\n\n```python\nclass CurrentUser(BaseModel):\n    id: int = Field(None, description=\"ID\")\n```\n\nSimply add more fields based on your needs.\n\n### AuthBackend\n\n```python\ncurrent_user = CurrentUser()\n```\n\nAfter line 18, assign values that you added on `CurrentUser`.\n\n## Top-level dependency\n\n**Note. Available from version 0.62 or higher.**\n\nSet a callable function when initialize FastAPI() app through `dependencies` argument.\n\nRefer `Logging` class inside of `core/fastapi/dependencies/logging.py`\n\n## Dependencies for specific permissions\n\nPermissions `IsAdmin`, `IsAuthenticated`, `AllowAll` have already been implemented.\n\n```python\nfrom core.fastapi.dependencies import (\n    PermissionDependency,\n    IsAdmin,\n)\n\n\nuser_router = APIRouter()\n\n\n@user_router.get(\n    \"\",\n    response_model=List[GetUserListResponseSchema],\n    response_model_exclude={\"id\"},\n    responses={\"400\": {\"model\": ExceptionResponseSchema}},\n    dependencies=[Depends(PermissionDependency([IsAdmin]))],  # HERE\n)\nasync def get_user_list(\n    limit: int = Query(10, description=\"Limit\"),\n    prev: int = Query(None, description=\"Prev ID\"),\n):\n    pass\n```\nInsert permission through `dependencies` argument.\n\nIf you want to make your own permission, inherit `BasePermission` and implement `has_permission()` function.\n\n**Note. In order to use swagger's authorize function, you must put `PermissionDependency` as an argument of `dependencies`.**\n\n## Event dispatcher\n\nRefer the README of https://github.com/teamhide/fastapi-event\n\n## Cache\n\n### Caching by prefix\n```python\nfrom core.helpers.cache import Cache\n\n\n@Cache.cached(prefix=\"get_user\", ttl=60)\nasync def get_user():\n    ...\n```\n\n### Caching by tag\n```python\nfrom core.helpers.cache import Cache, CacheTag\n\n\n@Cache.cached(tag=CacheTag.GET_USER_LIST, ttl=60)\nasync def get_user():\n    ...\n```\n\nUse the `Cache` decorator to cache the return value of a function.\n\nDepending on the argument of the function, caching is stored with a different value through internal processing.\n\n### Custom Key builder\n\n```python\nfrom core.helpers.cache.base import BaseKeyMaker\n\n\nclass CustomKeyMaker(BaseKeyMaker):\n    async def make(self, function: Callable, prefix: str) -> str:\n        ...\n```\n\nIf you want to create a custom key, inherit the BaseKeyMaker class and implement the make() method.\n\n### Custom Backend\n\n```python\nfrom core.helpers.cache.base import BaseBackend\n\n\nclass RedisBackend(BaseBackend):\n    async def get(self, key: str) -> Any:\n        ...\n\n    async def set(self, response: Any, key: str, ttl: int = 60) -> None:\n        ...\n\n    async def delete_startswith(self, value: str) -> None:\n        ...\n```\n\nIf you want to create a custom key, inherit the BaseBackend class and implement the `get()`, `set()`, `delete_startswith()` method.\n\nPass your custom backend or keymaker as an argument to init. (`/app/server.py`)\n\n```python\ndef init_cache() -> None:\n    Cache.init(backend=RedisBackend(), key_maker=CustomKeyMaker())\n```\n\n### Remove all cache by prefix/tag\n\n```python\nfrom core.helpers.cache import Cache, CacheTag\n\n\nawait Cache.remove_by_prefix(prefix=\"get_user_list\")\nawait Cache.remove_by_tag(tag=CacheTag.GET_USER_LIST)\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "FastAPI struct",
    "version": "0.4.3",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c0a5389f4858cc2c33493fe649be6049257f40acb848337cc8f25935aa851088",
                "md5": "08a078b4f2e452b6c22a1ca0d0d56a72",
                "sha256": "7487de66bc3a0c7bb4d8c35885e93e91ecae6ca50cd6eeb75b06709e25afa997"
            },
            "downloads": -1,
            "filename": "openfund_server-0.4.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "08a078b4f2e452b6c22a1ca0d0d56a72",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 56373,
            "upload_time": "2024-12-12T04:19:47",
            "upload_time_iso_8601": "2024-12-12T04:19:47.158499Z",
            "url": "https://files.pythonhosted.org/packages/c0/a5/389f4858cc2c33493fe649be6049257f40acb848337cc8f25935aa851088/openfund_server-0.4.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c17dff3aa496666ce9bb3306c560ea0b547f592e1e64054760c35fa7ace9d0dc",
                "md5": "16e3c64259a87e1306b78f9d8b905992",
                "sha256": "e0cb16e878338d9728f7e37748e3a05b40ea9cada584eb9a55de25543090c72a"
            },
            "downloads": -1,
            "filename": "openfund_server-0.4.3.tar.gz",
            "has_sig": false,
            "md5_digest": "16e3c64259a87e1306b78f9d8b905992",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 34720,
            "upload_time": "2024-12-12T04:19:51",
            "upload_time_iso_8601": "2024-12-12T04:19:51.955808Z",
            "url": "https://files.pythonhosted.org/packages/c1/7d/ff3aa496666ce9bb3306c560ea0b547f592e1e64054760c35fa7ace9d0dc/openfund_server-0.4.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-12 04:19:51",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "openfund-server"
}
        
Elapsed time: 0.76222s