From 1b83d8cdda9417997181eccb72631b157eb8bd8e Mon Sep 17 00:00:00 2001 From: murphy <william.murphy@embl.de> Date: Mon, 27 Jun 2022 17:18:13 +0200 Subject: [PATCH] Fix spacing and use tidier way to specify optional args. --- dma_client/client.py | 99 ++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/dma_client/client.py b/dma_client/client.py index 9afbf25..476e8a3 100644 --- a/dma_client/client.py +++ b/dma_client/client.py @@ -54,11 +54,11 @@ class RESTClient: self.session: Optional[APISession] = None def authenticate( - self, - username: str, - password: str, - scope: Optional[str] = 'USER', - application: Optional[str] = None + self, + username: str, + password: str, + scope: Optional[str] = 'USER', + application: Optional[str] = None ): """ Authenticate with the remote API, store access token. @@ -80,12 +80,12 @@ class RESTClient: @login_required def list_data( # pylint:disable=R0913 - self, - name_contains: Optional[str] = None, - path_contains: Optional[str] = None, - comment_contains: Optional[str] = None, - deleted: Optional[bool] = None, - data_type: Optional[str] = None + self, + name_contains: Optional[str] = None, + path_contains: Optional[str] = None, + comment_contains: Optional[str] = None, + deleted: Optional[bool] = None, + data_type: Optional[str] = None ) -> List[models.Data]: """ List all the data available to the user. @@ -119,11 +119,11 @@ class RESTClient: @login_required def register_data( - self, - path: PosixPath, - comment: Optional[str] = None, - name: Optional[str] = None, - collection_id: Optional[int] = None + self, + path: PosixPath, + comment: Optional[str] = None, + name: Optional[str] = None, + collection_id: Optional[int] = None ) -> models.Data: """ Register a new data item. @@ -160,11 +160,11 @@ class RESTClient: @login_required def request_archive( - self, - data_id: int, - budget_number: str, - delete_data: bool, - comment: str + self, + data_id: int, + budget_number: str, + delete_data: bool, + comment: str ) -> models.Action: """ Request an Archive of the specified data @@ -183,20 +183,21 @@ class RESTClient: @login_required def request_restore( - self, - data_id: int, - archive_id: int, - restore_to: Optional[PosixPath] = None + self, + data_id: int, + archive_id: int, + restore_to: Optional[PosixPath] = None ) -> models.Action: """ Request a Restore of an archive for the specified data. """ payload = { - 'archiveId': archive_id, + # default args + ('archiveId', archive_id), + # optional args + ('restoreTo', restore_to) if restore_to else None, } - if restore_to: - payload['restoreTo'] = restore_to # type: ignore - resp = self.session.post(f'/data/{data_id}/restore', json=payload) # type: ignore + resp = self.session.post(f'/data/{data_id}/restore', json=dict(filter(bool, payload))) # type: ignore if resp.status_code != 200: raise ClientException( f'Failed to request restore: HTTP{resp.status_code}: "{resp.text}".' @@ -205,33 +206,31 @@ class RESTClient: @login_required def request_handover( # pylint:disable=R0913 - self, - data_id: int, - copy_to: PosixPath, - set_owner: str, - comment: str, - delete_original: bool, - set_name: Optional[str] = None, - set_collection: Optional[int] = None, - limit_to: Optional[PosixPath] = None + self, + data_id: int, + copy_to: PosixPath, + set_owner: str, + comment: str, + delete_original: bool, + set_name: Optional[str] = None, + set_collection: Optional[int] = None, + limit_to: Optional[PosixPath] = None ) -> models.Action: """ Request a handover of a data item. """ payload = { - 'copyTo': copy_to, - 'setOwner': set_owner, - 'comment': comment, - 'deleteOriginal': delete_original + # default args + ('copyTo', copy_to), + ('setOwner', set_owner), + ('comment', comment), + ('deleteOriginal', delete_original), + # optional args + ('setName', set_name) if set_name else None, + ('setCollection', set_collection) if set_collection else None, + ('limitTo', limit_to) if limit_to else None, } - if set_name: - payload['setName'] = set_name - if set_collection: - payload['setCollection'] = set_collection - if limit_to: - payload['limitTo'] = limit_to - - resp = self.session.post(f'/data/{data_id}/handover', json=payload) # type: ignore + resp = self.session.post(f'/data/{data_id}/handover', json=dict(filter(bool, payload))) # type: ignore if resp.status_code != 200: raise ClientException( f'Failed to request handover: HTTP{resp.status_code}: "{resp.text}".' -- GitLab