Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/api/sword_edit.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-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 General Public License version 3, or any later version | # License: GNU 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 typing import Dict, Optional, Tuple | from typing import Optional, Tuple | ||||
from rest_framework import status | from rest_framework import status | ||||
from swh.storage import get_storage | from swh.storage import get_storage | ||||
from swh.storage.interface import StorageInterface | from swh.storage.interface import StorageInterface | ||||
from ..config import EDIT_IRI, EM_IRI | from ..config import EDIT_IRI, EM_IRI | ||||
from ..parsers import SWHAtomEntryParser, SWHMultiPartParser | from ..parsers import SWHAtomEntryParser, SWHMultiPartParser | ||||
from .common import APIPost, ParsedRequestHeaders | from .common import APIPost, ParsedRequestHeaders, Receipt | ||||
class SwordEditAPI(APIPost): | class SwordEditAPI(APIPost): | ||||
"""Deposit request class defining api endpoints for sword deposit. | """Deposit request class defining api endpoints for sword deposit. | ||||
What's known as 'SE-IRI' in the sword specification. | What's known as 'SE-IRI' in the sword specification. | ||||
HTTP verbs supported: POST | HTTP verbs supported: POST | ||||
Show All 9 Lines | def __init__(self): | ||||
) | ) | ||||
def process_post( | def process_post( | ||||
self, | self, | ||||
request, | request, | ||||
headers: ParsedRequestHeaders, | headers: ParsedRequestHeaders, | ||||
collection_name: str, | collection_name: str, | ||||
deposit_id: Optional[int] = None, | deposit_id: Optional[int] = None, | ||||
) -> Tuple[int, str, Dict]: | ) -> Tuple[int, str, Receipt]: | ||||
"""Add new metadata/archive to existing deposit. | """Add new metadata/archive to existing deposit. | ||||
This allows the following scenarios to occur: | This allows the following scenarios to occur: | ||||
- multipart: Add new metadata and archive to a deposit in status partial with | - multipart: Add new metadata and archive to a deposit in status partial with | ||||
the provided ones. | the provided ones. | ||||
- empty atom: Allows to finalize a deposit in status partial (transition to | - empty atom: Allows to finalize a deposit in status partial (transition to | ||||
Show All 10 Lines | ) -> Tuple[int, str, Receipt]: | ||||
deposit. And the response headers will contain an entry | deposit. And the response headers will contain an entry | ||||
'Location' with the EM-IRI. | 'Location' with the EM-IRI. | ||||
For the empty post case, this returns a 200. | For the empty post case, this returns a 200. | ||||
""" # noqa | """ # noqa | ||||
assert deposit_id is not None | assert deposit_id is not None | ||||
if request.content_type.startswith("multipart/"): | if request.content_type.startswith("multipart/"): | ||||
data = self._multipart_upload( | receipt = self._multipart_upload( | ||||
request, headers, collection_name, deposit_id=deposit_id | request, headers, collection_name, deposit_id=deposit_id | ||||
) | ) | ||||
return (status.HTTP_201_CREATED, EM_IRI, data) | return (status.HTTP_201_CREATED, EM_IRI, receipt) | ||||
content_length = headers.content_length or 0 | content_length = headers.content_length or 0 | ||||
if content_length == 0 and headers.in_progress is False: | if content_length == 0 and headers.in_progress is False: | ||||
# check for final empty post | # check for final empty post | ||||
data = self._empty_post(request, headers, collection_name, deposit_id) | receipt = self._empty_post(request, headers, collection_name, deposit_id) | ||||
return (status.HTTP_200_OK, EDIT_IRI, data) | return (status.HTTP_200_OK, EDIT_IRI, receipt) | ||||
data = self._atom_entry( | receipt = self._atom_entry( | ||||
request, headers, collection_name, deposit_id=deposit_id | request, headers, collection_name, deposit_id=deposit_id | ||||
) | ) | ||||
return (status.HTTP_201_CREATED, EM_IRI, data) | return (status.HTTP_201_CREATED, EM_IRI, receipt) |