Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBV In fastAPI #27

Open
yezz123 opened this issue Mar 1, 2024 · 0 comments · May be fixed by #26
Open

CBV In fastAPI #27

yezz123 opened this issue Mar 1, 2024 · 0 comments · May be fixed by #26
Assignees
Labels
enhancement New feature or request Large

Comments

@yezz123
Copy link
Owner

yezz123 commented Mar 1, 2024

Building all the flow based on this issue. yezz123/fastapi-class#104

  • Class Based Views: In Django, views are the heart of the application. Class Based Views (CBVs) are a way to define views as classes rather than functions. This provides several benefits such as better code organization, reusability, and the ability to use mixins to add common functionality. There are several types of CBVs available in Django including APIViews, GenericViews, and ViewSets.

    • APIViews: These views are designed specifically for building RESTful APIs. They provide features like parsing request data, rendering responses, and handling common API errors. You can subclass the APIView to create your custom views.

    • GenericViews: These views provide some common functionality for CRUD operations. You can use them to quickly create views for listing, creating, updating, and deleting objects. They are designed to be flexible and customizable.

    • ViewSets: ViewSets are similar to GenericViews but provide even more flexibility. They allow you to define a set of related views for a particular model or resource. You can define different actions for different HTTP methods like GET, POST, PUT, PATCH, and DELETE.

  • Openapi ID simplification: OpenAPI is a specification for building APIs. It provides a way to describe the structure of the API and the endpoints it exposes. One of the challenges with OpenAPI is that it can be lengthy and difficult to read. OpenAPI ID simplification is a way to simplify the structure of OpenAPI files to make them easier to work with.

  • 'Smart' and fast serialization using orjson: Serialization is the process of converting data from one format to another. In Django, serialization is used to convert Python objects to JSON format for use in APIs. orjson is a fast and efficient JSON serializer that Django can use. It provides some additional features like automatic serialization of date and datetime objects.

  • Http Problem Details implementation: HTTP Problem Details is a standard way of representing errors in APIs. It provides a structured way of describing errors including a type, title, and detail. This makes it easier for API consumers to understand what went wrong and how to fix it. Django provides built-in support for HTTP Problem Details.

  • Automatic prometheus metrics exporter: Prometheus is a monitoring system and time series database. It provides a way to collect and store metrics from applications. The prometheus-client library provides a way to export metrics from Django applications. The exporter can be configured to collect metrics and make them available to Prometheus automatically.

  • Pluggable healthcheck helper: Health checks are a way to monitor the health of an application. They can be used to check if the application is running correctly and if any issues need to be addressed. Django provides a pluggable health check framework that makes it easy to add health checks to an application. You can use the framework to create custom health checks and configure them to run at regular intervals.

from typing import Optional
from uuid import UUID

from fastapi import APIRouter, FastAPI

from fastango import HealthCheck, PydanticSerializer, configure_app, register_view
from fastango.views import APIViewSet, L


class PersonSchema(PydanticSerializer):
    id: UUID
    name: str
    age: int


app = FastAPI(
    title="FastAPI Class Example",
    description="Example of FastAPI Class",
    version="0.1.0",
)
router = APIRouter(tags=["Person"])

persons = {}


class MyView(APIViewSet):
    serializer = PersonSchema

    async def list(self) -> L:
        return list(persons.values())

    async def create(self, person: PersonSchema) -> PersonSchema:
        persons[person.id] = person
        return person

    async def retrieve(self, id: UUID) -> Optional[PersonSchema]:
        return persons.get(id)

    async def update(self, person: PersonSchema):
        persons[person.id] = person

    async def destroy(self, id: UUID) -> None:
        try:
            if id not in persons:
                raise KeyError
            persons.pop(id, None)
        except KeyError as e:
            raise ValueError(f"Person with id {id} not found") from e


view = register_view(router, MyView, "/persons")

app.include_router(router)

configure_app(
    app,
    enable_error_handlers=True,
    simplify_openapi_ids=True,
    side_services=None,
    healthcheck=HealthCheck(endpoint="/health"),
)
@yezz123 yezz123 added enhancement New feature or request Large labels Mar 1, 2024
@yezz123 yezz123 self-assigned this Mar 1, 2024
@yezz123 yezz123 linked a pull request Mar 1, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Large
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant