feat: added get_password_protected_file method
Some checks failed
Pylint / Pylint (3.12) (push) Failing after 36s

This commit is contained in:
SeaswimmerTheFsh 2023-12-22 21:01:59 -05:00
parent 0de2fcd024
commit ec9509e5ea
Signed by: cswimr
GPG key ID: 1EBC234EEDA901AE
4 changed files with 51 additions and 5 deletions

View file

@ -65,7 +65,11 @@ class RestAdapter:
try: # Deserialize JSON output to Python object, or return failed Result on exception
data_out = response.json()
except (ValueError, JSONDecodeError) as e:
raise PyZiplineError("Could not decode response from Zipline server") from e
if response.headers.get('Content-Type') == 'application/octet-stream':
# If the response contains a valid file, return success Result with file content
return Result(success=True, status_code=response.status_code, message=response.reason, data=response.content)
else:
raise PyZiplineError("Could not decode response from Zipline server") from e
# If status_code in 200-299 range, return success Result with data, otherwise return failed Result with message
is_success = 299 >= response.status_code >= 200

View file

@ -182,7 +182,6 @@ class ZiplineApi:
Forbidden: Raised if the authenticated user is not an administrator
NotFound: Raised if the invite code does not exist
PyZiplineError: Raised if the API changes, causing a breaking change in this method
ValueError: Raised if the invite code is not provided
Returns:
Invite: An object containing the deleted invite
@ -192,8 +191,8 @@ class ZiplineApi:
return Invite(**result.data)
if result.message == 'invite not found':
raise NotFound(result.message)
if result.message == 'no code':
raise ValueError('invite code not provided')
if result.message == 'not an administrator':
raise Forbidden(result.message)
raise PyZiplineError(f"{result.status_code}: {result.message}\n{result.data}")
def get_exif(self, file_id: int) -> dict:
@ -258,6 +257,30 @@ class ZiplineApi:
if result.status_code == 401:
raise Forbidden(result.message)
def get_password_protected_file(self, file_id: int, password: str) -> bytes:
"""Get a password protected file
Args:
file_id (int): ID of the file to get
password (str): Password for the file
Raises:
Forbidden: The password is incorrect
NotFound: The file does not exist
PyZiplineError: Raised if the API changes, causing a breaking change in this method
Returns:
bytes: The file, in bytes
"""
result: Result = self._rest_adapter.get(endpoint=f"auth/image", params={'id': file_id, 'password': password})
if result.status_code == 200:
return result.data
if result.message == 'image not found':
raise NotFound(result.message)
if result.message == 'wrong password':
raise Forbidden(result.message)
raise PyZiplineError(f"{result.status_code}: {result.message}\n{result.data}")
def get_invites(self) -> list[Invite]:
"""Get a list of invites