Skip to content

Commit 3eac209

Browse files
brentrubrentru
brentru
authored and
brentru
committed
all responses need to be closed, returning responses causes errors
1 parent a7b05b8 commit 3eac209

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

adafruit_azureiot.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ def get_hub_message(self):
100100
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}".format(self._iot_hub_url,
101101
self._device_id,
102102
AZ_API_VER)
103-
try:
104-
data = self._get(path, is_c2d=True)
105-
except RuntimeError:
106-
raise RuntimeError('HTTP C2D messages are HEAVILY throttled - poll every 25 mins.')
103+
data = self._get(path, is_c2d=True)
107104
if data == 204: # device's message queue is empty
108105
return -1
109106
etag = data[1]['etag']
@@ -190,12 +187,18 @@ def _get(self, path, is_c2d=False):
190187
response = self._wifi.get(
191188
path,
192189
headers=self._azure_header)
193-
if is_c2d: # check status of azure message queue
194-
if response.status_code == 200:
195-
return response.text, response.headers
196-
return response.status_code
197-
self._parse_http_status(response.status_code, response.reason)
198-
return response.json()
190+
status_code = response.status_code
191+
if is_c2d:
192+
if status_code == 200:
193+
data = response.text
194+
headers = response.headers
195+
response.close()
196+
return data, headers
197+
response.close()
198+
return status_code
199+
json = response.json()
200+
response.close()
201+
return json
199202

200203
def _delete(self, path, etag=None):
201204
"""HTTP DELETE
@@ -209,7 +212,9 @@ def _delete(self, path, etag=None):
209212
path,
210213
headers=data_headers)
211214
self._parse_http_status(response.status_code, response.reason)
212-
return response.status_code
215+
status_code = response.status_code
216+
response.close()
217+
return status_code
213218

214219
def _patch(self, path, payload):
215220
"""HTTP PATCH
@@ -221,7 +226,9 @@ def _patch(self, path, payload):
221226
json=payload,
222227
headers=self._azure_header)
223228
self._parse_http_status(response.status_code, response.reason)
224-
return response.json()
229+
json_data = response.json()
230+
response.close()
231+
return json_data
225232

226233
def _put(self, path, payload=None):
227234
"""HTTP PUT
@@ -233,4 +240,6 @@ def _put(self, path, payload=None):
233240
json=payload,
234241
headers=self._azure_header)
235242
self._parse_http_status(response.status_code, response.reason)
236-
return response.json()
243+
json_data = response.json()
244+
response.close()
245+
return json_data

0 commit comments

Comments
 (0)