Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/apiresponse.py
# Copyright (C) 2017-2021 The Software Heritage developers | # Copyright (C) 2017-2022 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 | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import render | from django.shortcuts import render | ||||
from django.urls import get_resolver | |||||
from django.utils.cache import add_never_cache_headers | 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 | ||||
▲ Show 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | if accepted_media_type == "text/html": | ||||
doc_data["response_data"] = json.dumps( | doc_data["response_data"] = json.dumps( | ||||
data, cls=JSONEncoder, sort_keys=True, indent=4, separators=(",", ": ") | data, cls=JSONEncoder, sort_keys=True, indent=4, separators=(",", ": ") | ||||
) | ) | ||||
doc_data["heading"] = shorten_path(str(request.path)) | doc_data["heading"] = shorten_path(str(request.path)) | ||||
# generate breadcrumbs data | # generate breadcrumbs data | ||||
if "route" in doc_data: | if "route" in doc_data: | ||||
all_view_names = set(get_resolver().reverse_dict.keys()) | |||||
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): | ||||
view_name = "api-1-" + "-".join( | |||||
[doc_data["endpoint_path"][i]["name"] for i in range(i + 1)] | |||||
) | |||||
if view_name in all_view_names: | |||||
doc_data["endpoint_path"][i]["path"] += "/doc/" | doc_data["endpoint_path"][i]["path"] += "/doc/" | ||||
else: | |||||
doc_data["endpoint_path"][i]["path"] = "" | |||||
if not doc_data["noargs"]: | if not doc_data["noargs"]: | ||||
doc_data["endpoint_path"][-1]["path"] += "/doc/" | doc_data["endpoint_path"][-1]["path"] += "/doc/" | ||||
response = render( | response = render( | ||||
request, "apidoc.html", doc_data, status=doc_data["status_code"] | request, "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 | ||||
▲ Show 20 Lines • Show All 65 Lines • Show Last 20 Lines |