Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 64 additions & 48 deletions imagekitio/file.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
from json import dumps
from typing import Any, Dict

Expand Down Expand Up @@ -162,10 +163,10 @@ def get_file_versions(self, file_id: str = None) -> ListFileResult:
elif resp.status_code == 404:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand All @@ -191,10 +192,10 @@ def get_file_version_details(
elif resp.status_code == 404:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand Down Expand Up @@ -288,8 +289,10 @@ def delete_file_version(self, file_id, version_id) -> ResponseMetadataResult:
elif resp.status_code == 400 or resp.status_code == 404:
response = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = response["message"] if type(response) == dict else ""
response_help = response["help"] if type(response) == dict else ""
if type(response) == str:
response = ast.literal_eval(response)
error_message = response['message'] if type(response) == dict else ""
response_help = response['help'] if type(response) == dict else ""
if resp.status_code == 400:
raise BadRequestException(
error_message, response_help, response_meta_data
Expand Down Expand Up @@ -327,7 +330,8 @@ def copy_file(
url = "{}/v1/files/copy".format(URL.API_BASE_URL)
headers = {"Content-Type": "application/json"}
headers.update(self.request.create_headers())
formatted_options = dumps(request_formatter(options.__dict__)) if options is not None else dict()
formatted_options = dumps(request_formatter(options.__dict__))
print("here formatted options:--->", formatted_options)
resp = self.request.request(
method="Post", url=url, headers=headers, data=formatted_options
)
Expand All @@ -337,8 +341,10 @@ def copy_file(
elif resp.status_code == 404:
response = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = response["message"] if type(response) == dict else ""
response_help = response["help"] if type(response) == dict else ""
if type(response) == str:
response = ast.literal_eval(response)
error_message = response['message'] if type(response) == dict else ""
response_help = response['help'] if type(response) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand All @@ -360,8 +366,10 @@ def move_file(
elif resp.status_code == 404:
response = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = response["message"] if type(response) == dict else ""
response_help = response["help"] if type(response) == dict else ""
if type(response) == str:
response = ast.literal_eval(response)
error_message = response['message'] if type(response) == dict else ""
response_help = response['help'] if type(response) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand All @@ -383,8 +391,10 @@ def rename_file(self, options: RenameFileRequestOptions = None) -> RenameFileRes
elif resp.status_code == 409:
response = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = response["message"] if type(response) == dict else ""
response_help = response["help"] if type(response) == dict else ""
if type(response) == str:
response = ast.literal_eval(response)
error_message = response['message'] if type(response) == dict else ""
response_help = response['help'] if type(response) == dict else ""
raise ConflictException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand All @@ -408,8 +418,10 @@ def restore_file_version(
elif resp.status_code == 404:
response = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = response["message"] if type(response) == dict else ""
response_help = response["help"] if type(response) == dict else ""
if type(response) == str:
response = ast.literal_eval(response)
error_message = response['message'] if type(response) == dict else ""
response_help = response['help'] if type(response) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand All @@ -430,8 +442,10 @@ def create_folder(
else:
response = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = response["message"] if type(response) == dict else ""
response_help = response["help"] if type(response) == dict else ""
if type(response) == str:
response = ast.literal_eval(response)
error_message = response['message'] if type(response) == dict else ""
response_help = response['help'] if type(response) == dict else ""
raise UnknownException(error_message, response_help, response_meta_data)

def delete_folder(
Expand All @@ -450,10 +464,10 @@ def delete_folder(
elif resp.status_code == 404:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand All @@ -473,10 +487,10 @@ def copy_folder(self, options: CopyFolderRequestOptions = None) -> FolderResult:
elif resp.status_code == 404:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand All @@ -497,10 +511,10 @@ def move_folder(self, options: MoveFolderRequestOptions = None) -> FolderResult:
elif resp.status_code == 404:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
raise NotFoundException(error_message, response_help, response_meta_data)
else:
general_api_throw_exception(resp)
Expand Down Expand Up @@ -609,10 +623,10 @@ def create_custom_metadata_fields(
else:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
if resp.status_code == 400:
raise BadRequestException(
error_message, response_help, response_meta_data
Expand All @@ -625,7 +639,7 @@ def get_custom_metadata_fields(
) -> ListCustomMetadataFieldsResult:
"""get custom metadata fields"""
url = "{}/v1/customMetadataFields".format(URL.API_BASE_URL)
param = {"includeDeleted": include_deleted}
param = {"includeDeleted": str(include_deleted).lower()}
resp = self.request.request(
method="GET", url=url, headers=self.request.create_headers(), params=param
)
Expand All @@ -637,8 +651,10 @@ def get_custom_metadata_fields(
else:
response = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = response["message"] if type(response) == dict else ""
response_help = response["help"] if type(response) == dict else ""
if type(response) == str:
response = ast.literal_eval(response)
error_message = response['message'] if type(response) == dict else ""
response_help = response['help'] if type(response) == dict else ""
raise UnknownException(error_message, response_help, response_meta_data)


Expand Down Expand Up @@ -668,10 +684,10 @@ def update_custom_metadata_fields(
else:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
if resp.status_code == 400:
raise BadRequestException(
error_message, response_help, response_meta_data
Expand All @@ -695,10 +711,10 @@ def delete_custom_metadata_field(self, field_id: str) -> ResponseMetadataResult:
else:
response_json = get_response_json(resp)
response_meta_data = populate_response_metadata(resp)
error_message = (
response_json["message"] if type(response_json) == dict else ""
)
response_help = response_json["help"] if type(response_json) == dict else ""
if type(response_json) == str:
response_json = ast.literal_eval(response_json)
error_message = response_json['message'] if type(response_json) == dict else ""
response_help = response_json['help'] if type(response_json) == dict else ""
if resp.status_code == 404:
raise NotFoundException(
error_message, response_help, response_meta_data
Expand Down
13 changes: 9 additions & 4 deletions imagekitio/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
from json import loads, dumps
from requests.models import Response

Expand Down Expand Up @@ -36,8 +37,10 @@ def populate_response_metadata(response: Response):
def general_api_throw_exception(response: Response):
resp = get_response_json(response)
response_meta_data = populate_response_metadata(response)
error_message = resp["message"] if type(resp) == dict else ""
response_help = resp["help"] if type(resp) == dict and "help" in resp else ""
if type(resp) == str:
resp = ast.literal_eval(resp)
error_message = resp['message'] if type(resp) == dict else ""
response_help = resp['help'] if type(resp) == dict and 'help' in resp else ""
if response.status_code == 400:
raise BadRequestException(error_message, response_help, response_meta_data)
elif response.status_code == 401:
Expand All @@ -60,8 +63,10 @@ def general_api_throw_exception(response: Response):
def throw_other_exception(response: Response):
resp = get_response_json(response)
response_meta_data = populate_response_metadata(response)
error_message = resp["message"] if type(resp) == dict else ""
response_help = resp["help"] if type(resp) == dict else ""
if type(resp) == str:
resp = ast.literal_eval(resp)
error_message = resp['message'] if type(resp) == dict else ""
response_help = resp['help'] if type(resp) == dict else ""
if response.status_code == 207:
raise PartialSuccessException(error_message, response_help, response_meta_data)
elif response.status_code == 404:
Expand Down
5 changes: 5 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import re
import unittest
from unittest.mock import patch

Expand Down Expand Up @@ -38,3 +39,7 @@ def get_auth_headers_for_test():
"utf-8"
)
return {"Authorization": "Basic {}".format(encoded_private_key)}


def make_string_to_single_line(multiline_string):
return re.sub(r'\s(?=\s)', '', re.sub(r'\s', ' ', multiline_string)).replace('{ ', '{').replace(' }', '}')
Loading