Back to all reviewers

Service layer abstraction

prowler-cloud/prowler
Based on 4 comments
Python

Separate business logic and external service interactions from API views by using dedicated service layers. This improves testability, maintainability, and enforces separation of concerns in your API design.

API Python

Reviewer Prompt

Separate business logic and external service interactions from API views by using dedicated service layers. This improves testability, maintainability, and enforces separation of concerns in your API design.

For example, instead of embedding S3 interactions directly in views:

# Don't do this in your views
@action(detail=True, methods=["get"])
def report(self, request, pk=None):
    s3_client = boto3.client("s3")
    # Direct S3 interaction code...

Create a service module:

# services/storage.py
class StorageService:
    def get_report(self, tenant_id, report_id):
        s3_client = self._get_client()
        # S3 interaction code...
        
    def _get_client(self):
        # Client initialization logic

Then use it in your views:

@action(detail=True, methods=["get"])
def report(self, request, pk=None):
    storage_service = StorageService()
    report_data = storage_service.get_report(
        request.tenant_id, pk
    )
    # Handle response...

When extending existing functionality, reuse common methods rather than creating duplicates with similar behavior. This maintains consistency and reduces maintenance burden across your API.

4
Comments Analyzed
Python
Primary Language
API
Category

Source Discussions