From 2ccc82b15df3e16556ed56e10424ba42f85ba1e9 Mon Sep 17 00:00:00 2001 From: nahara7 Date: Tue, 26 Apr 2022 00:09:20 -0400 Subject: [PATCH 1/2] Update reply and submission retrieval. --- sdclientapi/__init__.py | 50 ++++++++++++++++++++--------------- sdclientapi/sdlocalobjects.py | 10 +++++++ 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/sdclientapi/__init__.py b/sdclientapi/__init__.py index bb8e0a651..5978c47f4 100644 --- a/sdclientapi/__init__.py +++ b/sdclientapi/__init__.py @@ -478,17 +478,19 @@ def get_submission(self, submission: Submission) -> Submission: :param submission: Submission object we want to update. :returns: Updated submission object from the server. """ - path_query = "api/v1/sources/{}/submissions/{}".format( - submission.source_uuid, submission.uuid - ) - method = "GET" + if submission.source_uuid and submission.uuid is not None: - data, status_code, headers = self._send_json_request( - method, - path_query, - headers=self.req_headers, - timeout=self.default_request_timeout, - ) + path_query = "api/v1/sources/{}/submissions/{}".format( + submission.source_uuid, submission.uuid + ) + method = "GET" + + data, status_code, headers = self._send_json_request( + method, + path_query, + headers=self.req_headers, + timeout=self.default_request_timeout, + ) if status_code == 404: raise WrongUUIDError("Missing submission {}".format(submission.uuid)) @@ -760,20 +762,22 @@ def get_reply_from_source(self, source: Source, reply_uuid: str) -> Reply: :param reply_uuid: UUID of the reply. :returns: A reply object """ - path_query = "api/v1/sources/{}/replies/{}".format(source.uuid, reply_uuid) - method = "GET" + if source.uuid and reply_uuid is not None: - data, status_code, headers = self._send_json_request( - method, - path_query, - headers=self.req_headers, - timeout=self.default_request_timeout, - ) + path_query = "api/v1/sources/{}/replies/{}".format(source.uuid, reply_uuid) + method = "GET" - if status_code == 404: - raise WrongUUIDError("Missing source {}".format(source.uuid)) + data, status_code, headers = self._send_json_request( + method, + path_query, + headers=self.req_headers, + timeout=self.default_request_timeout, + ) - reply = Reply(**data) + if status_code == 404: + raise WrongUUIDError("Missing source {}".format(source.uuid)) + + reply = Reply(**data) return reply @@ -834,7 +838,9 @@ def download_reply(self, reply: Reply, path: str = "") -> Tuple[str, str]: if not self.proxy: # This is where we will save our downloaded file - filepath = os.path.join(path, reply.filename) + filepath = os.path.join( + path, headers["Content-Disposition"].split("attachment; filename=")[1] + ) with open(filepath, "wb") as fobj: for chunk in data.iter_content(chunk_size=1024): # Getting 1024 in each chunk if chunk: diff --git a/sdclientapi/sdlocalobjects.py b/sdclientapi/sdlocalobjects.py index c8453ca41..aefd5d847 100644 --- a/sdclientapi/sdlocalobjects.py +++ b/sdclientapi/sdlocalobjects.py @@ -75,6 +75,11 @@ def __init__(self, **kwargs) -> None: # type: ignore self.uuid = kwargs["uuid"] self.filename = kwargs["filename"] return + # Fetch an object only by uuid and soure_uuid. + elif {"uuid", "source_uuid"} == set(kwargs.keys()): + self.uuid = kwargs["uuid"] + self.source_uuid = kwargs["source_uuid"] + return for key in [ "filename", @@ -118,6 +123,11 @@ def __init__(self, **kwargs) -> None: # type: ignore self.uuid = kwargs["uuid"] return + elif ["uuid", "source_uuid"] == list(kwargs.keys()): + self.uuid = kwargs["uuid"] + self.source_uuid = kwargs["source_uuid"] + return + for key in [ "download_url", "filename", From 20be47fc0317f797ddd7d9ffd1137b7f38e4f917 Mon Sep 17 00:00:00 2001 From: nahara7 Date: Tue, 26 Apr 2022 01:30:41 -0400 Subject: [PATCH 2/2] Update comment --- sdclientapi/sdlocalobjects.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdclientapi/sdlocalobjects.py b/sdclientapi/sdlocalobjects.py index aefd5d847..c2f17be93 100644 --- a/sdclientapi/sdlocalobjects.py +++ b/sdclientapi/sdlocalobjects.py @@ -75,7 +75,7 @@ def __init__(self, **kwargs) -> None: # type: ignore self.uuid = kwargs["uuid"] self.filename = kwargs["filename"] return - # Fetch an object only by uuid and soure_uuid. + # Fetch an object only by uuid and soure_uuid. elif {"uuid", "source_uuid"} == set(kwargs.keys()): self.uuid = kwargs["uuid"] self.source_uuid = kwargs["source_uuid"]