Skip to content

Commit 4c21745

Browse files
author
John Miller
committed
Output response body when API returns an error
1 parent 09a89a9 commit 4c21745

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

setup.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import subprocess
55

66
setup(name="stitchclient",
7-
version="0.4.1",
7+
version="0.4.2",
88
description="A Stitch API client for Python",
99
author="Stitch",
1010
author_email="[email protected]",
1111
url="https://github.com/stitchdata/python-stitch-client",
1212
classifiers=['Programming Language :: Python :: 3 :: Only'],
1313
packages=find_packages(),
14-
install_requires=["python-dateutil", "msgpack-python"])
14+
install_requires=[
15+
"python-dateutil",
16+
"msgpack-python",
17+
"request==2.12.4",
18+
])

stitchclient/client.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import collections
55

6-
import urllib.request
6+
import requests
77

88
from collections import deque
99
from io import StringIO
@@ -122,25 +122,19 @@ def _serialize_entries(self, entries):
122122
def _stitch_request(self, body):
123123
headers = {'Authorization': 'Bearer {}'.format(self.token),
124124
'Content-Type': 'application/transit+json'}
125-
req = urllib.request.Request(self.stitch_url, body.encode("utf8"), headers)
126-
127-
try:
128-
with urllib.request.urlopen(req) as response:
129-
return response
130-
except urllib.error.HTTPError as e:
131-
logger.error(e.read())
132-
raise e
125+
return requests.post(self.stitch_url, headers=headers, data=body)
133126

134127
def _send_batch(self, batch):
135128
logger.debug("Sending batch of %s entries", len(batch))
136129
body = self._serialize_entries(batch)
137130
response = self._stitch_request(body)
138-
if response.status < 300:
131+
132+
if response.status_code < 300:
139133
if self.callback_function is not None:
140134
self.callback_function([x.callback_arg for x in batch])
141135
else:
142-
raise RuntimeError("Error sending data to the Stitch API, with response status code {}".format(response.status))
143-
136+
raise RuntimeError("Error sending data to the Stitch API. {0.status_code} - {0.content}"
137+
.format(response))
144138

145139
def flush(self):
146140
while True:

0 commit comments

Comments
 (0)