Skip to main content

Utilities for developing RESTful API, Open API projects with FastAPI and Python

Project description

ReachCollective / APIs / Utils

Utilities for developing RESTful API, Open API projects with FastAPI and Python

Requirements

  • sqlmodel (>=0.0.22,<0.0.23)
  • fastapi (>=0.115.8,<0.116.0)

Installation

Poetry

poetry add reachcollective-utils

Pip

pip install reachcollective-utils

Components

Handling Errors

Copy and paste this fragment into main.py of your FastAPI project.

# Handling Errors
# If an APIException, HTTPException is not defined in the router, the following line is executed
app.add_exception_handler(ValidationError, APIRender.pydantic_validation_handler) # type: ignore  # code: 422
app.add_exception_handler(RequestValidationError, APIRender.request_validation_handler) # type: ignore  # code: 422 
app.add_exception_handler(APIException, APIRender.api_exception_handler) # type: ignore
app.add_exception_handler(ValueError, APIRender.value_error_handler) # type: ignore # code: 400 
app.add_exception_handler(HTTPException, APIRender.http_exception_handler) # type: ignore
app.add_exception_handler(Exception, APIRender.generic_exception_handler) # type: ignore # code: 500 

DataGrid

List and paginate data from a model, accepting filters, sorting, and relationships.

1. Modify models

Add to_dict() function to each SQL Model, to_dict() gets relationships.

class Post(SQLModel, table=True):
    __tablename__ = "posts"

    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
    name: str | None
    status: str | None
    comments: list["Comment"] | None = Relationship(back_populates="post", sa_relationship_kwargs={"lazy": "raise"})

    def to_dict(self, *args, **kwargs):
        data = super().model_dump(*args, **kwargs)
        if 'comments' in self.__dict__:
            data['comments'] = []
            if self.quota_groups:
                data['comments'] = [quota_group.to_dict(*args, **kwargs) for quota_group in self.quota_groups]

        return data


class Comment(SQLModel, table=True):
    __tablename__ = "comments"

    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True)
    description: str | None
    post: Post | None = Relationship(back_populates="comments", sa_relationship_kwargs={"lazy": "raise"})

    def to_dict(self, *args, **kwargs):
        data = super().model_dump(*args, **kwargs)
        if 'post' in self.__dict__:
            data['post'] = {}
            if self.survey:
                data['post'] = self.survey.model_dump(*args, **kwargs)

        return data

2. Basic use

# URL: /posts?sort=-created&filter[status]=active&view=paginate


from app.models import Post
from reachcollective.utils.datagrid import DataGrid

@router.get('/posts')
async def list_(
    request: Request,
    db: AsyncSession = Depends(get_session)
):
    return await DataGrid(db, Post, request).init().get()

2. Custom filters

# URL: /posts?sort=-created&filter[status]=active&filter[name]=demo&view=paginate


from app.models import Post
from reachcollective.utils.datagrid import DataGrid

@router.get('/posts')
async def list_(
    request: Request,
    db: AsyncSession = Depends(get_session)
):
    datagrid = DataGrid(db, Post, request)
    datagrid.qp.filters.personalize = ['name']
    datagrid.init()

    for key, value in datagrid.params['filters']['customize'].items():
        match key:
            case 'name':
                datagrid.qb.stmt = datagrid.qb.stmt.where(Post.name.ilike(f'%{value}%'))

    return await datagrid.get()

3. Relationships

# URL: /posts?sort=-created&filter[status]=active&view=paginate&with=comments


from app.models import Post
from reachcollective.utils.datagrid import DataGrid

@router.get('/posts')
async def list_(
    request: Request,
    db: AsyncSession = Depends(get_session)
):
    return await DataGrid(db, Post, request).init().get()

4. Results

{
  "current_page": 1,
  "data": [
    {
      "id": "18a6ac49-cb2f-41c7-ac14-ff5360210c65",
      "name": "demo",
      "status": "active"
    },
    {
      "id": "81f08423-288f-4e74-bcbb-edf5ef37f1fe",
      "name": "demo",
      "status": "active"
    },
    {
      "id": "a017c74f-f0bb-4f65-9faf-36581812bbe7",
      "name": "Test 4",
      "status": "active"
    }
  ],
  "total": 683,
  "per_page": 15,
  "total_pages": 46
}
{
  "current_page": 1,
  "data": [
    {
      "id": "18a6ac49-cb2f-41c7-ac14-ff5360210c65",
      "name": "demo",
      "status": "active",
      "comments": [
        {
          "id": "28a6ac49-cb2f-41c7-ac14-ff5360210c15",
          "name": "Hello"
        },
        {
          "id": "38a6ac49-cb2f-41c7-ac14-ff5360210c25",
          "name": "World"
        }
      ]
    },
    {
      "id": "81f08423-288f-4e74-bcbb-edf5ef37f1fe",
      "name": "demo",
      "status": "active",
      "comments": []
    },
    {
      "id": "a017c74f-f0bb-4f65-9faf-36581812bbe7",
      "name": "Test 4",
      "status": "active",
      "comments": [
        {
          "id": "38a6ac49-cb2f-41c7-ac14-ff5360210c35",
          "name": "Hello"
        }
      ]
    }
  ],
  "total": 683,
  "per_page": 15,
  "total_pages": 46
}

Query Params

Parses, sanitizes and formats data from a URL query params. NOTE: Does not require sqlmodel

# URL: /query-params?sort=-last_updated&with=survey,profile&size=2&filter[name]=young&filter[status]=active|deactivate&filter[survey_id]=994c231c|8a900c77&view=paginate

from reachcollective.utils.datagrid import QueryParams
from reachcollective.utils import APIRender

@router.get('/query-params')
async def test_query_params(request: Request):
    try:
        return QueryParams(request).get()
    except HTTPException as e:
        return APIRender.error(e.detail, e.status_code)

Results

{
  "filters": {
    "init": {},
    "equals": {
      "name": "young",
      "status": [
        "active",
        "deactivate"
      ],
      "survey_id": [
        "994c231c",
        "8a900c77"
      ]
    },
    "customize": {}
  },
  "with": [
    "survey",
    "profile"
  ],
  "sort": [
    {
      "col": "last_updated",
      "dir": "desc",
      "by": "-last_updated"
    }
  ],
  "view": "paginate",
  "page": 1,
  "size": 2
}

APIRender

Centralizes fastAPI and third-party responses into a single class. Used in FastaPI routes. NOTE: Require pydantic

from reachcollective.utils import APIRender

@router.get('/api-render')
async def test_query_params(request: Request):
    try:
        # TODO: Your code here
    except HTTPException as e:
        return APIRender.error(e.detail, e.status_code)

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

reachcollective_utils-0.1.20.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reachcollective_utils-0.1.20-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file reachcollective_utils-0.1.20.tar.gz.

File metadata

  • Download URL: reachcollective_utils-0.1.20.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.13.5 Darwin/24.6.0

File hashes

Hashes for reachcollective_utils-0.1.20.tar.gz
Algorithm Hash digest
SHA256 4eb0edcd9356374ed903818b463e0b167930984a49f60590b7a4d6be0bc646b2
MD5 0afdd2e58933c79a93a7c7d1e640f5e8
BLAKE2b-256 0e8bf5b27e0dc631dad79d11cb466a4b595e9a7a8e6ad24f2e06ff226088124f

See more details on using hashes here.

File details

Details for the file reachcollective_utils-0.1.20-py3-none-any.whl.

File metadata

File hashes

Hashes for reachcollective_utils-0.1.20-py3-none-any.whl
Algorithm Hash digest
SHA256 0893c8ced3ddc0a58524721187b3a720bbbe048d0edb5a6f8269f0b703806454
MD5 a4de04ae5b81dc37bf877cb1e3ae452d
BLAKE2b-256 df8fd5ae3a0a3ffa97e0500af0992890252002aeb14aea92c7a84ad46d62d9e7

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