|
13 | 13 | import hashlib |
14 | 14 |
|
15 | 15 | from .version import VERSION |
| 16 | +from .studio_exception import StudioException |
16 | 17 |
|
17 | 18 | API_HEADER_KEY = 'X-SLT-API-KEY' |
18 | 19 | API_HEADER_CLIENT = 'X-SLT-API-CLIENT' |
@@ -117,26 +118,39 @@ def _api_request(self, endpoint, http_method, **kwargs): |
117 | 118 | headers=headers, |
118 | 119 | ) |
119 | 120 |
|
120 | | - if http_method == 'POST': |
121 | | - if data: |
122 | | - response = requests.post(path, data=data, **req_kw) |
123 | | - else: |
124 | | - response = requests.post(path, **req_kw) |
125 | | - elif http_method == 'PUT': |
126 | | - response = requests.put(path, data=data, **req_kw) |
127 | | - elif http_method == 'DELETE': |
128 | | - response = requests.delete(path, **req_kw) |
129 | | - else: |
130 | | - response = requests.get(path, data=data, **req_kw) |
131 | | - |
132 | | - LOGGER.debug('\tresponse code:%s', response.status_code) |
133 | | - |
134 | 121 | try: |
135 | | - LOGGER.debug('\tresponse: %s', response.json()) |
136 | | - except ValueError: |
137 | | - LOGGER.debug('\tresponse: %s', response.content) |
138 | | - |
139 | | - return response |
| 122 | + if http_method == 'POST': |
| 123 | + if data: |
| 124 | + response = requests.post(path, data=data, **req_kw) |
| 125 | + else: |
| 126 | + response = requests.post(path, **req_kw) |
| 127 | + elif http_method == 'PUT': |
| 128 | + response = requests.put(path, data=data, **req_kw) |
| 129 | + elif http_method == 'DELETE': |
| 130 | + response = requests.delete(path, **req_kw) |
| 131 | + else: |
| 132 | + response = requests.get(path, data=data, **req_kw) |
| 133 | + |
| 134 | + LOGGER.debug('\tresponse code:%s', response.status_code) |
| 135 | + |
| 136 | + try: |
| 137 | + LOGGER.debug('\tresponse: %s', response.json()) |
| 138 | + except ValueError: |
| 139 | + LOGGER.debug('\tresponse: %s', response.content) |
| 140 | + |
| 141 | + if not response.ok: |
| 142 | + status_code = response.status_code |
| 143 | + message = response.json()['message'] |
| 144 | + |
| 145 | + raise StudioException(status_code, message) |
| 146 | + except (StudioException, Exception) as e: |
| 147 | + formatted_response = { |
| 148 | + "message": e.message, |
| 149 | + "status": e.status_code |
| 150 | + } |
| 151 | + return formatted_response |
| 152 | + |
| 153 | + return response.json() |
140 | 154 |
|
141 | 155 | def list_jobs(self): |
142 | 156 | """ API call to get all jobs """ |
@@ -239,7 +253,7 @@ def list_photos(self): |
239 | 253 | 'GET' |
240 | 254 | ) |
241 | 255 |
|
242 | | - def get_upload_url(self, payload={"use_cache_upload": False}): |
| 256 | + def _get_upload_url(self, payload={"use_cache_upload": False}): |
243 | 257 | return self._api_request('photos/upload_url', 'GET', payload=payload) |
244 | 258 |
|
245 | 259 | # todo privatize this method and test photo_upload |
@@ -303,7 +317,7 @@ def upload_photo(self, photo_path, model, id): |
303 | 317 | } |
304 | 318 |
|
305 | 319 | # Ask studio for a presigned url |
306 | | - upload_url_resp = self.get_upload_url(payload=payload) |
| 320 | + upload_url_resp = self._get_upload_url(payload=payload) |
307 | 321 | upload_url = upload_url_resp.json()['url'] |
308 | 322 |
|
309 | 323 | # PUT request to presigned url with image data |
|
0 commit comments