Skip to content

Commit 1c897c1

Browse files
author
Dario Varotto
committed
Jobs saving to file: raise an exception when API is failing
1 parent e3c99f5 commit 1c897c1

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

ravenpackapi/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ravenpackapi.utils.constants import JSON_AVAILABLE_FIELDS
1515

1616
_VALID_METHODS = ('get', 'post', 'put', 'delete')
17-
VERSION = '1.0.19'
17+
VERSION = '1.0.20'
1818

1919
logger = logging.getLogger("ravenpack.core")
2020

@@ -35,6 +35,7 @@ def __init__(self, api_key=None):
3535
"or set your environment RP_API_KEY with your API KEY."
3636
)
3737
self.api_key = api_key
38+
self.log_curl_commands = True
3839

3940
@property
4041
def headers(self):
@@ -57,6 +58,8 @@ def request(self, endpoint, data=None, params=None, method='get'):
5758
data=json.dumps(data) if data else None,
5859
params=params,
5960
)
61+
if self.log_curl_commands:
62+
logger.info("API query to %s" % to_curl(response.request))
6063
if response.status_code != 200:
6164
logger.error("Error calling the API, we tried: %s" % to_curl(response.request))
6265
raise APIException(

ravenpackapi/models/job.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import requests
77

88
from ravenpackapi.exceptions import api_method, APIException, DataFileTimeout
9+
from ravenpackapi.util import to_curl
910

1011
logger = logging.getLogger(__name__)
1112

@@ -93,12 +94,17 @@ def save_to_file(self, filename):
9394

9495
# this is a different request than the normal API
9596
# streaming the file in chunks
96-
r = requests.get(job.url,
97-
headers=api.headers,
98-
stream=True,
99-
)
100-
101-
for chunk in r.iter_content(chunk_size=self._CHUNK_SIZE):
97+
response = requests.get(job.url,
98+
headers=api.headers,
99+
stream=True,
100+
)
101+
if response.status_code != 200:
102+
logger.error("Error calling the API, we tried: %s" % to_curl(response.request))
103+
raise APIException(
104+
'Got an error {status}: body was \'{error_message}\''.format(
105+
status=response.status_code, error_message=response.text
106+
), response=response)
107+
for chunk in response.iter_content(chunk_size=self._CHUNK_SIZE):
102108
if chunk:
103109
output.write(chunk)
104110

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '1.0.19'
3+
VERSION = '1.0.20'
44

55
with open('README.rst') as readme_file:
66
readme = readme_file.read()

0 commit comments

Comments
 (0)