Skip to content

Commit 0c97a1d

Browse files
author
brentru
committed
fix delete_device, adding etag specifier to headers
1 parent 77489e4 commit 0c97a1d

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

adafruit_azureiot.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def get_hub_message(self, device_id):
9797
self._iot_hub_url, device_id, etag, AZ_API_VER)
9898
if reject_message:
9999
path_complete += '&reject'
100-
del_status = self._delete(path_complete, is_c2d=True)
100+
del_status = self._delete(path_complete)
101101
if del_status == 204:
102102
return data[0]
103103
return -1
@@ -139,24 +139,25 @@ def replace_device_twin(self, device_id, properties):
139139

140140
# IoT Hub Service
141141
def get_devices(self):
142-
"""Enumerate devices from the identity registry of your IoT hub.
142+
"""Enumerate devices from the identity registry of your IoT Hub.
143143
"""
144144
path = "{0}/devices/?api-version={1}".format(self._iot_hub_url, AZ_API_VER)
145145
return self._get(path)
146146

147147
def get_device(self, device_id):
148-
"""Gets device information from the identity registry of an IoT hub.
148+
"""Gets device information from the identity registry of an IoT Hub.
149149
:param str device_id: Device Identifier.
150150
"""
151151
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
152152
return self._get(path)
153153

154-
def delete_device(self, device_id):
154+
def delete_device(self, device_id, device_etag):
155155
"""Deletes a specified device from the identity register of an IoT Hub.
156156
:param str device_id: Device Identifier.
157+
:param str device_etag: Device Identity Tag.
157158
"""
158159
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
159-
self._delete(path)
160+
self._delete(path, etag=device_etag)
160161

161162
# HTTP Helper Methods
162163
def _post(self, path, payload, return_response=True):
@@ -188,18 +189,19 @@ def _get(self, path, is_c2d=False):
188189
self._parse_http_status(response.status_code, response.reason)
189190
return response.json()
190191

191-
def _delete(self, path, is_c2d=False):
192+
def _delete(self, path, etag=None):
192193
"""HTTP DELETE
193194
:param str path: Formatted Azure IOT Hub Path.
194-
:param bool is_c2d: Cloud-to-device delete request.
195195
"""
196+
if etag:
197+
data_headers = {"Authorization":self._sas_token, "If-Match":'"%s"'%etag}
198+
else:
199+
data_headers = self._azure_header
196200
response = self._wifi.delete(
197201
path,
198-
headers=self._azure_header)
202+
headers=data_headers)
199203
self._parse_http_status(response.status_code, response.reason)
200-
if is_c2d: # check server response for complete message request
201-
return response.status_code
202-
return response.json()
204+
return response.status_code
203205

204206
def _patch(self, path, payload):
205207
"""HTTP PATCH

0 commit comments

Comments
 (0)