Skip to content

MrThearMan/drf-pipeline-views

Repository files navigation

Django REST Framework Pipeline Views

Coverage Status Workflow Status PyPI Licence Last Commit Issues Downloads

Python Version Django Version

pip install drf-pipeline-views

Documentation: https://mrthearman.github.io/drf-pipeline-views/

Source Code: https://github.com/MrThearMan/drf-pipeline-views/


Inspired by a talk on The Clean Architecture in Python by Brandon Rhodes, drf-pipeline-views aims to simplify writing testable API endpoints with [Django REST framework][drf] using the Pipeline Design Pattern.

The main idea behind the pipeline pattern is to process data in steps. Input from the previous step is passed to the next, resulting in a collection of "data-in, data-out" -functions. These functions can be easily unit tested, since none of the functions depend on the state of the objects in the other parts of the pipeline. Furthermore, IO can be separated into its own step, making the other parts of the logic simpler and faster to test by not having to mock or do any other special setup around the IO. This also means that the IO block, or in fact any other part of the application, can be replaced as long as the data flowing through the pipeline remains the same.

from pipeline_views import BasePipelineView

from .my_serializers import InputSerializer, OutputSerializer
from .my_validators import validator
from .my_services import io_func, logging_func, integration_func


class SomeView(BasePipelineView):
    pipelines = {
        "GET": [
            InputSerializer,
            validator,
            io_func,
            integration_func,
            logging_func,
            OutputSerializer,
        ],
    }

Have a look at the quickstart section in the documentation on basic usage.