|
1 | | -import json |
| 1 | +from json import dumps |
2 | 2 | from typing import Any, Dict |
3 | 3 |
|
4 | 4 | from .constants.errors import ERRORS |
|
10 | 10 | snake_to_lower_camel, |
11 | 11 | ) |
12 | 12 |
|
| 13 | +try: |
| 14 | + from simplejson.errors import JSONDecodeError |
| 15 | +except ImportError: |
| 16 | + from json import JSONDecodeError |
13 | 17 |
|
14 | 18 | class File(object): |
15 | 19 | def __init__(self, request_obj): |
@@ -47,7 +51,10 @@ def upload(self, file, file_name, options) -> Dict: |
47 | 51 | ) |
48 | 52 |
|
49 | 53 | if resp.status_code > 200: |
50 | | - error = resp.json() |
| 54 | + try: |
| 55 | + error = resp.json() |
| 56 | + except JSONDecodeError: |
| 57 | + error = resp.text |
51 | 58 | response = None |
52 | 59 | else: |
53 | 60 | error = None |
@@ -107,7 +114,7 @@ def update_file_details(self, file_id: str, options: dict): |
107 | 114 | url = "{}/{}/details/".format(URL.BASE_URL.value, file_id) |
108 | 115 | headers = {"Content-Type": "application/json"} |
109 | 116 | headers.update(self.request.get_auth_headers()) |
110 | | - data = json.dumps(request_formatter(options)) |
| 117 | + data = dumps(request_formatter(options)) |
111 | 118 | resp = self.request.request(method="Patch", url=url, headers=headers, data=data) |
112 | 119 | if resp.status_code > 200: |
113 | 120 | error = resp.json() |
@@ -171,7 +178,7 @@ def purge_cache(self, file_url: str = None) -> Dict[str, Any]: |
171 | 178 | headers.update(self.request.get_auth_headers()) |
172 | 179 | body = {"url": file_url} |
173 | 180 | resp = self.request.request( |
174 | | - "Post", headers=headers, url=url, data=json.dumps(body) |
| 181 | + "Post", headers=headers, url=url, data=dumps(body) |
175 | 182 | ) |
176 | 183 | formatted_resp = camel_dict_to_snake_dict(resp.json()) |
177 | 184 | if resp.status_code > 204: |
|
0 commit comments