Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/client.py
Show First 20 Lines • Show All 468 Lines • ▼ Show 20 Lines | def do_execute(self, method, url, info): | ||||
with open(info["filepath"], "rb") as f: | with open(info["filepath"], "rb") as f: | ||||
return self.do(method, url, data=f, headers=info["headers"]) | return self.do(method, url, data=f, headers=info["headers"]) | ||||
class CreateArchiveDepositClient(BaseCreateDepositClient): | class CreateArchiveDepositClient(BaseCreateDepositClient): | ||||
"""Post an archive (binary) deposit client.""" | """Post an archive (binary) deposit client.""" | ||||
def compute_headers(self, info): | def compute_headers(self, info): | ||||
return { | headers = { | ||||
"SLUG": info["slug"], | |||||
"CONTENT_MD5": info["md5sum"], | "CONTENT_MD5": info["md5sum"], | ||||
"IN-PROGRESS": str(info["in_progress"]), | "IN-PROGRESS": str(info["in_progress"]), | ||||
"CONTENT-TYPE": info["content-type"], | "CONTENT-TYPE": info["content-type"], | ||||
"CONTENT-DISPOSITION": "attachment; filename=%s" % (info["filename"],), | "CONTENT-DISPOSITION": "attachment; filename=%s" % (info["filename"],), | ||||
} | } | ||||
if "slug" in info: | |||||
headers["SLUG"] = info["slug"] | |||||
return headers | |||||
def compute_information(self, *args, **kwargs) -> Dict[str, Any]: | def compute_information(self, *args, **kwargs) -> Dict[str, Any]: | ||||
info = compute_unified_information( | info = compute_unified_information( | ||||
*args, filepath=kwargs["archive_path"], **kwargs | *args, filepath=kwargs["archive_path"], **kwargs | ||||
) | ) | ||||
info["headers"] = self.compute_headers(info) | info["headers"] = self.compute_headers(info) | ||||
return info | return info | ||||
class UpdateArchiveDepositClient(CreateArchiveDepositClient): | class UpdateArchiveDepositClient(CreateArchiveDepositClient): | ||||
"""Update (add/replace) an archive (binary) deposit client.""" | """Update (add/replace) an archive (binary) deposit client.""" | ||||
def compute_url(self, collection, *args, deposit_id=None, **kwargs): | def compute_url(self, collection, *args, deposit_id=None, **kwargs): | ||||
return "/%s/%s/media/" % (collection, deposit_id) | return "/%s/%s/media/" % (collection, deposit_id) | ||||
def compute_method(self, *args, replace=False, **kwargs): | def compute_method(self, *args, replace=False, **kwargs): | ||||
return "put" if replace else "post" | return "put" if replace else "post" | ||||
class CreateMetadataDepositClient(BaseCreateDepositClient): | class CreateMetadataDepositClient(BaseCreateDepositClient): | ||||
"""Post a metadata deposit client.""" | """Post a metadata deposit client.""" | ||||
def compute_headers(self, info): | def compute_headers(self, info): | ||||
return { | headers = { | ||||
"SLUG": info["slug"], | |||||
"IN-PROGRESS": str(info["in_progress"]), | "IN-PROGRESS": str(info["in_progress"]), | ||||
"CONTENT-TYPE": "application/atom+xml;type=entry", | "CONTENT-TYPE": "application/atom+xml;type=entry", | ||||
} | } | ||||
if "slug" in info: | |||||
headers["SLUG"] = info["slug"] | |||||
return headers | |||||
def compute_information(self, *args, **kwargs) -> Dict[str, Any]: | def compute_information(self, *args, **kwargs) -> Dict[str, Any]: | ||||
info = compute_unified_information( | info = compute_unified_information( | ||||
*args, filepath=kwargs["metadata_path"], **kwargs | *args, filepath=kwargs["metadata_path"], **kwargs | ||||
) | ) | ||||
info["headers"] = self.compute_headers(info) | info["headers"] = self.compute_headers(info) | ||||
return info | return info | ||||
Show All 36 Lines | def _multipart_info(self, info, info_meta): | ||||
info_meta["filename"], | info_meta["filename"], | ||||
open(info_meta["filepath"], "rb"), | open(info_meta["filepath"], "rb"), | ||||
"application/atom+xml", | "application/atom+xml", | ||||
), | ), | ||||
), | ), | ||||
] | ] | ||||
headers = { | headers = { | ||||
"SLUG": info["slug"], | |||||
"CONTENT_MD5": info["md5sum"], | "CONTENT_MD5": info["md5sum"], | ||||
"IN-PROGRESS": str(info["in_progress"]), | "IN-PROGRESS": str(info["in_progress"]), | ||||
} | } | ||||
if "slug" in info: | |||||
headers["SLUG"] = info["slug"] | |||||
return files, headers | return files, headers | ||||
def compute_information(self, *args, **kwargs) -> Dict[str, Any]: | def compute_information(self, *args, **kwargs) -> Dict[str, Any]: | ||||
info = compute_unified_information(*args, filepath=kwargs["archive_path"],) | info = compute_unified_information(*args, filepath=kwargs["archive_path"],) | ||||
info_meta = compute_unified_information( | info_meta = compute_unified_information( | ||||
*args, filepath=kwargs["metadata_path"], | *args, filepath=kwargs["metadata_path"], | ||||
) | ) | ||||
Show All 25 Lines | def deposit_status(self, collection: str, deposit_id: int): | ||||
"""Retrieve status information on a deposit.""" | """Retrieve status information on a deposit.""" | ||||
return StatusDepositClient(url=self.base_url, auth=self.auth).execute( | return StatusDepositClient(url=self.base_url, auth=self.auth).execute( | ||||
collection, deposit_id | collection, deposit_id | ||||
) | ) | ||||
def deposit_create( | def deposit_create( | ||||
self, | self, | ||||
collection: str, | collection: str, | ||||
slug: str, | slug: Optional[str], | ||||
archive: Optional[str] = None, | archive: Optional[str] = None, | ||||
metadata: Optional[str] = None, | metadata: Optional[str] = None, | ||||
in_progress: bool = False, | in_progress: bool = False, | ||||
): | ): | ||||
"""Create a new deposit (archive, metadata, both as multipart).""" | """Create a new deposit (archive, metadata, both as multipart).""" | ||||
if archive and not metadata: | if archive and not metadata: | ||||
return CreateArchiveDepositClient( | return CreateArchiveDepositClient( | ||||
url=self.base_url, auth=self.auth | url=self.base_url, auth=self.auth | ||||
Show All 12 Lines | ): | ||||
archive_path=archive, | archive_path=archive, | ||||
metadata_path=metadata, | metadata_path=metadata, | ||||
) | ) | ||||
def deposit_update( | def deposit_update( | ||||
self, | self, | ||||
collection: str, | collection: str, | ||||
deposit_id: int, | deposit_id: int, | ||||
slug: str, | slug: Optional[str], | ||||
archive: Optional[str] = None, | archive: Optional[str] = None, | ||||
metadata: Optional[str] = None, | metadata: Optional[str] = None, | ||||
in_progress: bool = False, | in_progress: bool = False, | ||||
replace: bool = False, | replace: bool = False, | ||||
swhid: Optional[str] = None, | swhid: Optional[str] = None, | ||||
): | ): | ||||
"""Update (add/replace) existing deposit (archive, metadata, both).""" | """Update (add/replace) existing deposit (archive, metadata, both).""" | ||||
r = self.deposit_status(collection, deposit_id) | r = self.deposit_status(collection, deposit_id) | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |