Skip to content

Commit

Permalink
Merge pull request #174 from nahara7/nahara/sdk-objects
Browse files Browse the repository at this point in the history
Update Fetching Sdk Objects
  • Loading branch information
sssoleileraaa authored May 19, 2022
2 parents 31ea11e + 20be47f commit 0909cb9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
50 changes: 28 additions & 22 deletions sdclientapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions sdclientapi/sdlocalobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 0909cb9

Please sign in to comment.