Added BaseAPIView: Introduced a generic base view class to standardize CRUD operations, simplifying the creation of new API endpoints with minimal boilerplate code.
Implemented ExtendSchemaMeta Metaclass: Developed a metaclass that automatically applies @extend_schema decorators to CRUD methods (list, retrieve, create, partial_update, destroy), enhancing OpenAPI documentation generation.
Dynamic Primary Key Typing: Enabled dynamic typing for primary keys (pk_type), ensuring accurate type annotations and improved API documentation based on the specified pk_type.
Refactored Existing Views: Updated existing API views (e.g., CurrencyAPIView) to inherit from BaseAPIView, reducing code duplication and improving maintainability.
Updated Linter Configuration: Modified pyproject.toml to move deprecated ruff linter settings to the new [tool.ruff.lint] section, ensuring compliance with the latest linter configuration standards.
Example Usage:
Now, creating a new CRUD API view is straightforward. Here's how you can define CurrencyAPIView using the newly added BaseAPIView:
from src.application.currency.commands.add_currency.add_currency_command import AddCurrencyCommand
from src.application.currency.commands.edit_currency.edit_currency_command import EditCurrencyCommand
from src.application.currency.commands.remove_currency.remove_currency_command import RemoveCurrencyCommand
from src.application.currency.queries.get_currencies_list.get_currencies_list_query import GetCurrenciesListQuery
from src.application.currency.queries.get_currency_detail.get_currency_detail_query import GetCurrencyDetailQuery
from src.domain.value_objects import CurrencyId # CurrencyId = int
from src.presentation.rest_api.apps.currency.serializers import (
CreateCurrencyRequestSerializer,
DetailCurrencyResponseSerializer,
ListCurrencyResponseSerializer,
UpdateCurrencyRequestSerializer,
)
from src.presentation.rest_api.apps.common.base_api_view import BaseAPIView
class CurrencyAPIView(BaseAPIView[
ListCurrencyResponseSerializer,
DetailCurrencyResponseSerializer,
CreateCurrencyRequestSerializer,
UpdateCurrencyRequestSerializer,
GetCurrenciesListQuery,
GetCurrencyDetailQuery,
AddCurrencyCommand,
EditCurrencyCommand,
RemoveCurrencyCommand,
CurrencyId, # PKType
]):
authentication_classes = ()
list_serializer = ListCurrencyResponseSerializer
detail_serializer = DetailCurrencyResponseSerializer
create_serializer = CreateCurrencyRequestSerializer
update_serializer = UpdateCurrencyRequestSerializer
list_query = GetCurrenciesListQuery
detail_query = GetCurrencyDetailQuery
create_command = AddCurrencyCommand
update_command = EditCurrencyCommand
remove_command = RemoveCurrencyCommand
pk_type = CurrencyId # CurrencyId = int
Benefits:
Simplified CRUD Creation: By inheriting from BaseAPIView and specifying the required serializers, queries, commands, and pk_type, you can rapidly set up new CRUD endpoints without repetitive code.
Automatic Documentation: The ExtendSchemaMeta metaclass ensures that all CRUD methods are properly documented in OpenAPI, reducing the need for manual documentation updates.
Enhanced Maintainability: Centralizing common functionalities in BaseAPIView and using a metaclass for schema extensions makes the codebase easier to maintain and extend.
Summary:
This Pull Request enhances the API development workflow by introducing a reusable BaseAPIView and an ExtendSchemaMeta metaclass. These additions streamline the creation of CRUD endpoints, ensure consistent OpenAPI documentation, and improve overall code maintainability.
Added
BaseAPIView
: Introduced a generic base view class to standardize CRUD operations, simplifying the creation of new API endpoints with minimal boilerplate code.Implemented
ExtendSchemaMeta
Metaclass: Developed a metaclass that automatically applies@extend_schema
decorators to CRUD methods (list
,retrieve
,create
,partial_update
,destroy
), enhancing OpenAPI documentation generation.Dynamic Primary Key Typing: Enabled dynamic typing for primary keys (
pk_type
), ensuring accurate type annotations and improved API documentation based on the specifiedpk_type
.Refactored Existing Views: Updated existing API views (e.g.,
CurrencyAPIView
) to inherit fromBaseAPIView
, reducing code duplication and improving maintainability.Updated Linter Configuration: Modified
pyproject.toml
to move deprecatedruff
linter settings to the new[tool.ruff.lint]
section, ensuring compliance with the latest linter configuration standards.Example Usage:
Now, creating a new CRUD API view is straightforward. Here's how you can define
CurrencyAPIView
using the newly addedBaseAPIView
:Benefits:
Simplified CRUD Creation: By inheriting from
BaseAPIView
and specifying the required serializers, queries, commands, andpk_type
, you can rapidly set up new CRUD endpoints without repetitive code.Automatic Documentation: The
ExtendSchemaMeta
metaclass ensures that all CRUD methods are properly documented in OpenAPI, reducing the need for manual documentation updates.Enhanced Maintainability: Centralizing common functionalities in
BaseAPIView
and using a metaclass for schema extensions makes the codebase easier to maintain and extend.Summary:
This Pull Request enhances the API development workflow by introducing a reusable
BaseAPIView
and anExtendSchemaMeta
metaclass. These additions streamline the creation of CRUD endpoints, ensure consistent OpenAPI documentation, and improve overall code maintainability.