Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin_save.py
# Copyright (C) 2018-2021 The Software Heritage developers | # Copyright (C) 2018-2021 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 logging | |||||
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 api_route | ||||
from swh.web.auth.utils import privileged_user | from swh.web.auth.utils import privileged_user | ||||
from swh.web.common.origin_save import ( | from swh.web.common.origin_save import ( | ||||
create_save_origin_request, | create_save_origin_request, | ||||
get_save_origin_requests, | get_save_origin_requests, | ||||
) | ) | ||||
logger = logging.getLogger(__name__) | |||||
@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, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | .. http:post:: /api/1/origin/save/(visit_type)/url/(origin_url)/ | ||||
:statuscode 200: no error | :statuscode 200: no error | ||||
:statuscode 400: an invalid visit type or origin url has been provided | :statuscode 400: an invalid visit type or origin url has been provided | ||||
:statuscode 403: the provided origin url is blacklisted | :statuscode 403: the provided origin url is blacklisted | ||||
:statuscode 404: no save requests have been found for a given origin | :statuscode 404: no save requests have been found for a given origin | ||||
""" | """ | ||||
data = request.data or {} | |||||
if request.method == "POST": | if request.method == "POST": | ||||
sor = create_save_origin_request( | sor = create_save_origin_request( | ||||
visit_type, origin_url, privileged_user(request), user_id=request.user.id | visit_type, | ||||
origin_url, | |||||
privileged_user(request), | |||||
user_id=request.user.id, | |||||
**data, | |||||
) | ) | ||||
del sor["id"] | del sor["id"] | ||||
else: | else: | ||||
sor = get_save_origin_requests(visit_type, origin_url) | sor = get_save_origin_requests(visit_type, origin_url) | ||||
for s in sor: | for s in sor: | ||||
del s["id"] | del s["id"] | ||||
return sor | return sor |