from functools import partial from django.utils.deprecation import MiddlewareMixin from fwd_api.celery_worker.internal_task import send_response_to_sqs class ResponseMonitorMiddleware(MiddlewareMixin): def process_response(self, request, response): """Monitor responses and send errors to SQS""" if response and (400 <= response.status_code < 600): # Send async to avoid blocking response send_response_to_sqs.delay( response.data if hasattr(response, 'data') else str(response.content), response.status_code ) return response