Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/apiresponse.py
# Copyright (C) 2017-2019 The Software Heritage developers | # Copyright (C) 2017-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | import json | ||||
import logging | import logging | ||||
import traceback | import traceback | ||||
from typing import Any, Dict, Optional | from typing import Any, Dict, Optional | ||||
import sentry_sdk | import sentry_sdk | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.utils.cache import add_never_cache_headers | |||||
from django.utils.html import escape | from django.utils.html import escape | ||||
from rest_framework.exceptions import APIException | from rest_framework.exceptions import APIException | ||||
from rest_framework.request import Request | from rest_framework.request import Request | ||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
from rest_framework.utils.encoders import JSONEncoder | from rest_framework.utils.encoders import JSONEncoder | ||||
from swh.storage.exc import StorageAPIError, StorageDBError | from swh.storage.exc import StorageAPIError, StorageDBError | ||||
from swh.web.api import utils | from swh.web.api import utils | ||||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | if request.accepted_media_type == "text/html": | ||||
# generate breadcrumbs data | # generate breadcrumbs data | ||||
if "route" in doc_data: | if "route" in doc_data: | ||||
doc_data["endpoint_path"] = gen_path_info(doc_data["route"]) | doc_data["endpoint_path"] = gen_path_info(doc_data["route"]) | ||||
for i in range(len(doc_data["endpoint_path"]) - 1): | for i in range(len(doc_data["endpoint_path"]) - 1): | ||||
doc_data["endpoint_path"][i]["path"] += "/doc/" | doc_data["endpoint_path"][i]["path"] += "/doc/" | ||||
if not doc_data["noargs"]: | if not doc_data["noargs"]: | ||||
doc_data["endpoint_path"][-1]["path"] += "/doc/" | doc_data["endpoint_path"][-1]["path"] += "/doc/" | ||||
return render( | response = render( | ||||
request, "api/apidoc.html", doc_data, status=doc_data["status_code"] | request, "api/apidoc.html", doc_data, status=doc_data["status_code"] | ||||
) | ) | ||||
# otherwise simply return the raw data and let DRF picks | # otherwise simply return the raw data and let DRF picks | ||||
# the correct renderer (JSON or YAML) | # the correct renderer (JSON or YAML) | ||||
else: | else: | ||||
return Response( | response = Response( | ||||
data, | data, | ||||
headers=headers, | headers=headers, | ||||
content_type=request.accepted_media_type, | content_type=request.accepted_media_type, | ||||
status=doc_data["status_code"], | status=doc_data["status_code"], | ||||
) | ) | ||||
if getattr(request, "never_cache", False): | |||||
add_never_cache_headers(response) | |||||
return response | |||||
def error_response( | def error_response( | ||||
request: Request, exception: Exception, doc_data: Dict[str, Any] | request: Request, exception: Exception, doc_data: Dict[str, Any] | ||||
) -> HttpResponse: | ) -> HttpResponse: | ||||
"""Private function to create a custom error response. | """Private function to create a custom error response. | ||||
Args: | Args: | ||||
request: a DRF Request object | request: a DRF Request object | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |