Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/apiurls.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 functools | import functools | ||||
from typing import Dict, List, Optional | from typing import Dict, List, Optional | ||||
from django.http.response import HttpResponseBase | from django.http.response import HttpResponseBase | ||||
from django.utils.cache import add_never_cache_headers | |||||
from rest_framework.decorators import api_view | from rest_framework.decorators import api_view | ||||
from swh.web.api import throttling | from swh.web.api import throttling | ||||
from swh.web.api.apiresponse import make_api_response | from swh.web.api.apiresponse import make_api_response | ||||
from swh.web.common.urlsindex import UrlsIndex | from swh.web.common.urlsindex import UrlsIndex | ||||
class APIUrls(UrlsIndex): | class APIUrls(UrlsIndex): | ||||
▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | ): | ||||
url_pattern = "^" + api_version + url_pattern + "$" | url_pattern = "^" + api_version + url_pattern + "$" | ||||
def decorator(f): | def decorator(f): | ||||
# create a DRF view from the wrapped function | # create a DRF view from the wrapped function | ||||
@api_view(methods) | @api_view(methods) | ||||
@throttling.throttle_scope(throttle_scope) | @throttling.throttle_scope(throttle_scope) | ||||
@functools.wraps(f) | @functools.wraps(f) | ||||
def api_view_f(request, **kwargs): | def api_view_f(request, **kwargs): | ||||
# never_cache will be handled in apiresponse module | |||||
request.never_cache = never_cache | |||||
response = f(request, **kwargs) | response = f(request, **kwargs) | ||||
doc_data = None | doc_data = None | ||||
# check if response has been forwarded by api_doc decorator | # check if response has been forwarded by api_doc decorator | ||||
if isinstance(response, dict) and "doc_data" in response: | if isinstance(response, dict) and "doc_data" in response: | ||||
doc_data = response["doc_data"] | doc_data = response["doc_data"] | ||||
response = response["data"] | response = response["data"] | ||||
# check if HTTP response needs to be created | # check if HTTP response needs to be created | ||||
if not isinstance(response, HttpResponseBase): | if not isinstance(response, HttpResponseBase): | ||||
api_response = make_api_response( | api_response = make_api_response( | ||||
request, data=response, doc_data=doc_data | request, data=response, doc_data=doc_data | ||||
) | ) | ||||
else: | else: | ||||
api_response = response | api_response = response | ||||
if never_cache: | |||||
add_never_cache_headers(api_response) | |||||
return api_response | return api_response | ||||
# small hacks for correctly generating API endpoints index doc | # small hacks for correctly generating API endpoints index doc | ||||
api_view_f.__name__ = f.__name__ | api_view_f.__name__ = f.__name__ | ||||
api_view_f.http_method_names = methods | api_view_f.http_method_names = methods | ||||
# register the route and its view in the endpoints index | # register the route and its view in the endpoints index | ||||
APIUrls.add_url_pattern(url_pattern, api_view_f, view_name) | APIUrls.add_url_pattern(url_pattern, api_view_f, view_name) | ||||
if checksum_args: | if checksum_args: | ||||
APIUrls.add_redirect_for_checksum_args( | APIUrls.add_redirect_for_checksum_args( | ||||
view_name, [url_pattern], checksum_args | view_name, [url_pattern], checksum_args | ||||
) | ) | ||||
return f | return f | ||||
return decorator | return decorator |