Skip to content

Commit ab27552

Browse files
author
brentru
committed
pylint a bit
1 parent 0c36033 commit ab27552

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

adafruit_azureiot.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
__version__ = "0.0.0-auto.0"
4343
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT.git"
4444

45-
AZURE_API_VER = "2018-06-30" # Azure URI API Version Identifier
45+
AZ_API_VER = "2018-06-30" # Azure URI API Version Identifier
4646
AZURE_HTTP_ERROR_CODES = [400, 401, 404, 403, 412, 429, 500] # Azure HTTP Status Codes
4747

4848
class IOT_HUB:
@@ -76,23 +76,29 @@ def _parse_http_status(status_code, status_reason):
7676
raise TypeError("Error {0}: {1}".format(status_code, status_reason))
7777

7878
def get_hub_message(self, device_id):
79-
"""Gets a message from a Microsoft Azure IoT Hub (Cloud-to-Device).
80-
NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25 minutes, or more.
81-
:param int device_id: Device identifier.
82-
"""
83-
# GET device-bound notification
84-
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
85-
data = self._get(path, is_c2d = True)
86-
# check for etag in header
87-
print(data)
88-
etag = data[1]['etag']
89-
if etag is not None:
90-
print('etag: ', etag)
91-
# DELETE https://fully-qualified-iothubname.azure-devices.net/devices/{id}/messages/deviceBound/{etag}?api-version=2018-06-30
92-
path_complete = "https://{0}.azure-devices.net/devices/{1}/messages/deviceBound/{2}?api-version=2018-06-30".format(self._iot_hub_url, device_id, etag)
93-
print(path_complete)
94-
self._delete(path_complete)
95-
print('deleted!')
79+
"""Gets a message from a Microsoft Azure IoT Hub (Cloud-to-Device).
80+
NOTE: HTTP Cloud-to-Device messages are throttled. Poll every 25 minutes, or more.
81+
:param int device_id: Device identifier.
82+
"""
83+
# GET device-bound notification
84+
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}".format(self._iot_hub_url,
85+
device_id, AZ_API_VER)
86+
data = self._get(path, is_c2d=True)
87+
reject_message = True
88+
# check for etag in header
89+
print(data)
90+
etag = data[1]['etag']
91+
if etag: # either complete or nack the message
92+
reject_message = False
93+
etag = etag.strip('\'"')
94+
path_complete = "https://{0}.azure-devices.net/devices/{1}/messages/ \
95+
deviceBound/{2}?api-version={3}".format(self._iot_hub_url,
96+
device_id, etag, AZ_API_VER)
97+
print(path_complete)
98+
if reject_message:
99+
path_complete += '&reject'
100+
self._delete(path_complete)
101+
print('deleted!')
96102

97103
# Device Messaging
98104
def send_device_message(self, device_id, message):
@@ -101,15 +107,15 @@ def send_device_message(self, device_id, message):
101107
:param string message: Message.
102108
"""
103109
path = "{0}/devices/{1}/messages/events?api-version={2}".format(self._iot_hub_url,
104-
device_id, AZURE_API_VER)
110+
device_id, AZ_API_VER)
105111
self._post(path, message)
106112

107113
# Device Twin
108114
def get_device_twin(self, device_id):
109115
"""Returns a device twin
110116
:param str device_id: Device Identifier.
111117
"""
112-
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
118+
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
113119
return self._get(path)
114120

115121
def update_device_twin(self, device_id, properties):
@@ -118,36 +124,36 @@ def update_device_twin(self, device_id, properties):
118124
:param str properties: Device Twin Properties
119125
(https://docs.microsoft.com/en-us/rest/api/iothub/service/updatetwin#twinproperties)
120126
"""
121-
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
127+
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
122128
return self._patch(path, properties)
123129

124130
def replace_device_twin(self, device_id, properties):
125131
"""Replaces tags and desired properties of a device twin.
126132
:param str device_id: Device Identifier.
127133
:param str properties: Device Twin Properties.
128134
"""
129-
path = "{0}/twins/{1}?api-version-{2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
135+
path = "{0}/twins/{1}?api-version-{2}".format(self._iot_hub_url, device_id, AZ_API_VER)
130136
return self._put(path, properties)
131137

132138
# IoT Hub Service
133139
def get_devices(self):
134140
"""Enumerate devices from the identity registry of your IoT hub.
135141
"""
136-
path = "{0}/devices/?api-version={1}".format(self._iot_hub_url, AZURE_API_VER)
142+
path = "{0}/devices/?api-version={1}".format(self._iot_hub_url, AZ_API_VER)
137143
return self._get(path)
138144

139145
def get_device(self, device_id):
140146
"""Gets device information from the identity registry of an IoT hub.
141147
:param str device_id: Device Identifier.
142148
"""
143-
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
149+
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
144150
return self._get(path)
145151

146152
def delete_device(self, device_id):
147153
"""Deletes a specified device from the identity register of an IoT Hub.
148154
:param str device_id: Device Identifier.
149155
"""
150-
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZURE_API_VER)
156+
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url, device_id, AZ_API_VER)
151157
self._delete(path)
152158

153159
# HTTP Helper Methods
@@ -162,7 +168,6 @@ def _post(self, path, payload):
162168
headers=self._azure_header)
163169
self._parse_http_status(response.status_code, response.reason)
164170
return response.json()
165-
response.close()
166171

167172
def _get(self, path, is_c2d=False):
168173
"""HTTP GET
@@ -178,7 +183,6 @@ def _get(self, path, is_c2d=False):
178183
raise TypeError('No data within message queue')
179184
self._parse_http_status(response.status_code, response.reason)
180185
return response.json()
181-
response.close()
182186

183187
def _delete(self, path):
184188
"""HTTP DELETE
@@ -190,7 +194,6 @@ def _delete(self, path):
190194
print(response.status_code, response.reason)
191195
self._parse_http_status(response.status_code, response.reason)
192196
return response.json()
193-
response.close()
194197

195198
def _patch(self, path, payload):
196199
"""HTTP PATCH
@@ -203,7 +206,6 @@ def _patch(self, path, payload):
203206
headers=self._azure_header)
204207
self._parse_http_status(response.status_code, response.reason)
205208
return response.json()
206-
response.close()
207209

208210
def _put(self, path, payload=None):
209211
"""HTTP PUT
@@ -217,4 +219,3 @@ def _put(self, path, payload=None):
217219
self._parse_http_status(response.status_code, response.reason)
218220
print('Resp:', response.status_code, response.reason)
219221
return response.json()
220-
response.close()

0 commit comments

Comments
 (0)