ResponseMixin
Mixin providing standardized API response methods.
Ensures consistent response formatting across all API endpoints and provides response methods that correspond to the standardized schema objects defined in this module.
Usage
class MyView(ResponseMixin, APIView): def get(self, request): return self.success_response({"data": "example"})
Schema Integration
Each method in this mixin corresponds to a schema object: - success_response() -> successResponseSchema (200) - accepted_response() -> acceptedResponseSchema (202) - no_content_response() -> noContentResponseSchema (204) - bad_request_response() -> badRequestResponseSchema (400) - not_found_response() -> notFoundResponseSchema (404) - not_acceptable_response() -> notAcceptableResponseSchema (406)
Source code in polarrouteserver/route_api/responses.py
class ResponseMixin:
"""
Mixin providing standardized API response methods.
Ensures consistent response formatting across all API endpoints and
provides response methods that correspond to the standardized schema objects
defined in this module.
Usage:
class MyView(ResponseMixin, APIView):
def get(self, request):
return self.success_response({"data": "example"})
Schema Integration:
Each method in this mixin corresponds to a schema object:
- success_response() -> successResponseSchema (200)
- accepted_response() -> acceptedResponseSchema (202)
- no_content_response() -> noContentResponseSchema (204)
- bad_request_response() -> badRequestResponseSchema (400)
- not_found_response() -> notFoundResponseSchema (404)
- not_acceptable_response() -> notAcceptableResponseSchema (406)
"""
def success_response(self, data, status_code=rest_framework.status.HTTP_200_OK):
"""
Return standardized success response.
Corresponds to: successResponseSchema (200)
"""
return Response(
data,
headers={"Content-Type": "application/json"},
status=status_code,
)
def accepted_response(self, data):
"""
Return standardized accepted response.
Corresponds to: acceptedResponseSchema (202)
"""
return Response(
data,
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_202_ACCEPTED,
)
def no_content_response(self, data=None, message=None):
"""
Return standardized no content response.
Corresponds to: noContentResponseSchema (204)
"""
response_data = data or {}
if message:
response_data["message"] = message
return Response(
data=response_data,
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_204_NO_CONTENT,
)
def bad_request_response(
self, error_message, status_code=rest_framework.status.HTTP_400_BAD_REQUEST
):
"""
Return standardized bad request response.
Corresponds to: badRequestResponseSchema (400)
"""
return Response(
{"error": error_message},
headers={"Content-Type": "application/json"},
status=status_code,
)
def not_found_response(self, message):
"""
Return standardized not found response.
Corresponds to: notFoundResponseSchema (404)
"""
return Response(
{"error": message},
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_404_NOT_FOUND,
)
def not_acceptable_response(self, message):
"""
Return standardized not acceptable response.
Corresponds to: notAcceptableResponseSchema (406)
"""
return Response(
{"error": message},
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_406_NOT_ACCEPTABLE,
)
accepted_response(data)
Return standardized accepted response. Corresponds to: acceptedResponseSchema (202)
Source code in polarrouteserver/route_api/responses.py
def accepted_response(self, data):
"""
Return standardized accepted response.
Corresponds to: acceptedResponseSchema (202)
"""
return Response(
data,
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_202_ACCEPTED,
)
bad_request_response(error_message, status_code=rest_framework.status.HTTP_400_BAD_REQUEST)
Return standardized bad request response. Corresponds to: badRequestResponseSchema (400)
Source code in polarrouteserver/route_api/responses.py
def bad_request_response(
self, error_message, status_code=rest_framework.status.HTTP_400_BAD_REQUEST
):
"""
Return standardized bad request response.
Corresponds to: badRequestResponseSchema (400)
"""
return Response(
{"error": error_message},
headers={"Content-Type": "application/json"},
status=status_code,
)
no_content_response(data=None, message=None)
Return standardized no content response. Corresponds to: noContentResponseSchema (204)
Source code in polarrouteserver/route_api/responses.py
def no_content_response(self, data=None, message=None):
"""
Return standardized no content response.
Corresponds to: noContentResponseSchema (204)
"""
response_data = data or {}
if message:
response_data["message"] = message
return Response(
data=response_data,
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_204_NO_CONTENT,
)
not_acceptable_response(message)
Return standardized not acceptable response. Corresponds to: notAcceptableResponseSchema (406)
Source code in polarrouteserver/route_api/responses.py
def not_acceptable_response(self, message):
"""
Return standardized not acceptable response.
Corresponds to: notAcceptableResponseSchema (406)
"""
return Response(
{"error": message},
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_406_NOT_ACCEPTABLE,
)
not_found_response(message)
Return standardized not found response. Corresponds to: notFoundResponseSchema (404)
Source code in polarrouteserver/route_api/responses.py
def not_found_response(self, message):
"""
Return standardized not found response.
Corresponds to: notFoundResponseSchema (404)
"""
return Response(
{"error": message},
headers={"Content-Type": "application/json"},
status=rest_framework.status.HTTP_404_NOT_FOUND,
)
success_response(data, status_code=rest_framework.status.HTTP_200_OK)
Return standardized success response. Corresponds to: successResponseSchema (200)
Source code in polarrouteserver/route_api/responses.py
def success_response(self, data, status_code=rest_framework.status.HTTP_200_OK):
"""
Return standardized success response.
Corresponds to: successResponseSchema (200)
"""
return Response(
data,
headers={"Content-Type": "application/json"},
status=status_code,
)