Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/graph.py
# Copyright (C) 2020-2021 The Software Heritage developers | # Copyright (C) 2020-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 | ||||
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 unquote, urlparse, urlunparse | |||||
import requests | import requests | ||||
from django.http import QueryDict | |||||
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.renderers import JSONRenderer | from rest_framework.renderers import JSONRenderer | ||||
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 swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.model import Sha1Git | from swh.model.model import Sha1Git | ||||
▲ Show 20 Lines • Show All 111 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 = unquote(graph_query) | |||||
graph_query_url += graph_query | graph_query_url += graph_query | ||||
if request.GET: | |||||
graph_query_url += "?" + request.GET.urlencode(safe="/;:") | parsed_url = urlparse(graph_query_url) | ||||
query_dict = QueryDict(parsed_url.query, mutable=True) | |||||
query_dict.update(request.GET) | |||||
if query_dict: | |||||
graph_query_url = urlunparse( | |||||
seirl: I think it's cleaner to use `urlunparse()` here. | |||||
Done Inline ActionsBetter indeed, thanks ! anlambert: Better indeed, thanks ! | |||||
parsed_url._replace(query=query_dict.urlencode(safe="/;:")) | |||||
) | |||||
response = requests.get(graph_query_url, stream=True) | response = requests.get(graph_query_url, stream=True) | ||||
if response.status_code != 200: | if response.status_code != 200: | ||||
return Response( | return Response( | ||||
response.content, | response.content, | ||||
status=response.status_code, | status=response.status_code, | ||||
content_type=response.headers["Content-Type"], | content_type=response.headers["Content-Type"], | ||||
) | ) | ||||
Show All 20 Lines |
I think it's cleaner to use urlunparse() here.