Changeset View
Changeset View
Standalone View
Standalone View
swh/web/save_code_now/api_views.py
# Copyright (C) 2018-2022 The Software Heritage developers | # Copyright (C) 2018-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 os | import os | ||||
from typing import Optional, cast | from typing import Optional, cast | ||||
from rest_framework.request import Request | from rest_framework.request import Request | ||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | ||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import APIUrls, api_route | ||||
from swh.web.auth.utils import ( | from swh.web.auth.utils import ( | ||||
API_SAVE_ORIGIN_PERMISSION, | API_SAVE_ORIGIN_PERMISSION, | ||||
SWH_AMBASSADOR_PERMISSION, | SWH_AMBASSADOR_PERMISSION, | ||||
privileged_user, | privileged_user, | ||||
) | ) | ||||
from swh.web.save_code_now.origin_save import ( | from swh.web.save_code_now.origin_save import ( | ||||
create_save_origin_request, | create_save_origin_request, | ||||
get_savable_visit_types, | get_savable_visit_types, | ||||
get_save_origin_requests, | get_save_origin_requests, | ||||
) | ) | ||||
def _savable_visit_types() -> str: | def _savable_visit_types() -> str: | ||||
docstring = "" | docstring = "" | ||||
if os.environ.get("DJANGO_SETTINGS_MODULE") != "swh.web.settings.tests": | if os.environ.get("DJANGO_SETTINGS_MODULE") != "swh.web.settings.tests": | ||||
visit_types = sorted(get_savable_visit_types()) | visit_types = sorted(get_savable_visit_types()) | ||||
docstring = "" | docstring = "" | ||||
for visit_type in visit_types[:-1]: | for visit_type in visit_types[:-1]: | ||||
docstring += f"**{visit_type}**, " | docstring += f"**{visit_type}**, " | ||||
docstring += f"and **{visit_types[-1]}**" | docstring += f"and **{visit_types[-1]}**" | ||||
return docstring | return docstring | ||||
save_code_now_api_urls = APIUrls() | |||||
@api_route( | @api_route( | ||||
r"/origin/save/(?P<visit_type>.+)/url/(?P<origin_url>.+)/", | r"/origin/save/(?P<visit_type>.+)/url/(?P<origin_url>.+)/", | ||||
"api-1-save-origin", | "api-1-save-origin", | ||||
methods=["GET", "POST"], | methods=["GET", "POST"], | ||||
throttle_scope="swh_save_origin", | throttle_scope="swh_save_origin", | ||||
never_cache=True, | never_cache=True, | ||||
api_urls=save_code_now_api_urls, | |||||
) | ) | ||||
@api_doc("/origin/save/", category="Request archival") | @api_doc("/origin/save/", category="Request archival") | ||||
@format_docstring(visit_types=_savable_visit_types()) | @format_docstring(visit_types=_savable_visit_types()) | ||||
def api_save_origin(request: Request, visit_type: str, origin_url: str): | def api_save_origin(request: Request, visit_type: str, origin_url: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | .. http:get:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | ||||
.. http:post:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | .. http:post:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | ||||
▲ Show 20 Lines • Show All 78 Lines • Show Last 20 Lines |