Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/graph.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 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 | ||||
from distutils.util import strtobool | from distutils.util import strtobool | ||||
import json | import json | ||||
from typing import Dict, Iterator, Union | from typing import Dict, Iterator, Union | ||||
from urllib.parse import quote | |||||
import requests | import requests | ||||
from django.http.response import StreamingHttpResponse | from django.http.response import StreamingHttpResponse | ||||
from rest_framework.decorators import renderer_classes | from rest_framework.decorators import renderer_classes | ||||
from rest_framework.request import Request | from rest_framework.request import Request | ||||
from rest_framework.response import Response | from rest_framework.response import Response | ||||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | ) -> Union[Response, StreamingHttpResponse]: | ||||
if request.get_host() != SWH_WEB_INTERNAL_SERVER_NAME: | if request.get_host() != SWH_WEB_INTERNAL_SERVER_NAME: | ||||
if not bool(request.user and request.user.is_authenticated): | if not bool(request.user and request.user.is_authenticated): | ||||
return Response("Authentication credentials were not provided.", status=401) | return Response("Authentication credentials were not provided.", status=401) | ||||
if not request.user.has_perm(API_GRAPH_PERM): | if not request.user.has_perm(API_GRAPH_PERM): | ||||
return Response( | return Response( | ||||
"You do not have permission to perform this action.", status=403 | "You do not have permission to perform this action.", status=403 | ||||
) | ) | ||||
graph_query_url = get_config()["graph"]["server_url"] | graph_query_url = get_config()["graph"]["server_url"] | ||||
graph_query_url += graph_query | graph_query_url += quote(graph_query) | ||||
if request.GET: | if request.GET: | ||||
graph_query_url += "?" + request.GET.urlencode(safe="/;:") | graph_query_url += "?" + request.GET.urlencode(safe="/;:") | ||||
response = requests.get(graph_query_url, stream=True) | response = requests.get(graph_query_url, stream=True) | ||||
# graph stats and counter endpoint responses are not streamed | # graph stats and counter endpoint responses are not streamed | ||||
if response.headers.get("Transfer-Encoding") != "chunked": | if response.headers.get("Transfer-Encoding") != "chunked": | ||||
return Response( | return Response( | ||||
response.text, | response.text, | ||||
status=response.status_code, | status=response.status_code, | ||||
Show All 14 Lines |