4242__version__ = "0.0.0-auto.0"
4343__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT.git"
4444
45- AZ_API_VER = "2018-06-30" # Azure URI API Version Identifier
46- AZURE_HTTP_ERROR_CODES = [400 , 401 , 404 , 403 , 412 , 429 , 500 ] # Azure HTTP Status Codes
45+ AZ_API_VER = "2018-06-30" # Azure URI API Version Identifier
46+ AZURE_HTTP_ERROR_CODES = [400 , 401 , 404 , 403 , 412 , 429 , 500 ] # Azure HTTP Status Codes
47+
4748
4849class IOT_Hub :
4950 """
5051 Provides access to a Microsoft Azure IoT Hub.
5152 https://docs.microsoft.com/en-us/rest/api/iothub/
5253 """
54+
5355 def __init__ (self , wifi_manager , iot_hub_name , sas_token , device_id ):
5456 """ Creates an instance of an Azure IoT Hub Client.
5557 :param wifi_manager: WiFiManager object from ESPSPI_WiFiManager.
@@ -58,14 +60,14 @@ def __init__(self, wifi_manager, iot_hub_name, sas_token, device_id):
5860 :param str device_id: Unique Azure IoT Device Identifier.
5961 """
6062 _wifi_type = str (type (wifi_manager ))
61- if ' ESPSPI_WiFiManager' in _wifi_type :
63+ if " ESPSPI_WiFiManager" in _wifi_type :
6264 self ._wifi = wifi_manager
6365 else :
6466 raise TypeError ("This library requires a WiFiManager object." )
6567 self ._iot_hub_url = "https://{0}.azure-devices.net" .format (iot_hub_name )
6668 self ._sas_token = sas_token
6769 self ._device_id = device_id
68- self ._azure_header = {"Authorization" :self ._sas_token }
70+ self ._azure_header = {"Authorization" : self ._sas_token }
6971
7072 @property
7173 def device_id (self ):
@@ -97,19 +99,20 @@ def get_hub_message(self):
9799 """
98100 reject_message = True
99101 # get a device-bound notification
100- path = "{0}/devices/{1}/messages/deviceBound?api-version={2}" .format (self . _iot_hub_url ,
101- self ._device_id ,
102- AZ_API_VER )
102+ path = "{0}/devices/{1}/messages/deviceBound?api-version={2}" .format (
103+ self . _iot_hub_url , self ._device_id , AZ_API_VER
104+ )
103105 data = self ._get (path , is_c2d = True )
104- if data == 204 : # device's message queue is empty
106+ if data == 204 : # device's message queue is empty
105107 return None
106- etag = data [1 ][' etag' ]
107- if etag : # either complete or nack the message
108+ etag = data [1 ][" etag" ]
109+ if etag : # either complete or nack the message
108110 reject_message = False
109111 path_complete = "{0}/devices/{1}/messages/deviceBound/{2}?api-version={3}" .format (
110- self ._iot_hub_url , self ._device_id , etag .strip ('\' "' ), AZ_API_VER )
112+ self ._iot_hub_url , self ._device_id , etag .strip ("'\" " ), AZ_API_VER
113+ )
111114 if reject_message :
112- path_complete += ' &reject'
115+ path_complete += " &reject"
113116 del_status = self ._delete (path_complete )
114117 if del_status == 204 :
115118 return data [0 ]
@@ -120,33 +123,37 @@ def send_device_message(self, message):
120123 """Sends a device-to-cloud message.
121124 :param string message: Message to send to Azure IoT.
122125 """
123- path = "{0}/devices/{1}/messages/events?api-version={2}" .format (self ._iot_hub_url ,
124- self ._device_id , AZ_API_VER )
126+ path = "{0}/devices/{1}/messages/events?api-version={2}" .format (
127+ self ._iot_hub_url , self ._device_id , AZ_API_VER
128+ )
125129 self ._post (path , message , return_response = False )
126130
127131 # Device Twin
128132 def get_device_twin (self ):
129133 """Returns the device's device twin information in JSON format.
130134 """
131- path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url ,
132- self ._device_id , AZ_API_VER )
135+ path = "{0}/twins/{1}?api-version={2}" .format (
136+ self ._iot_hub_url , self ._device_id , AZ_API_VER
137+ )
133138 return self ._get (path )
134139
135140 def update_device_twin (self , properties ):
136141 """Updates tags and desired properties of the device's device twin.
137142 :param str properties: Device Twin Properties
138143 (https://docs.microsoft.com/en-us/rest/api/iothub/service/updatetwin#twinproperties)
139144 """
140- path = "{0}/twins/{1}?api-version={2}" .format (self ._iot_hub_url ,
141- self ._device_id , AZ_API_VER )
145+ path = "{0}/twins/{1}?api-version={2}" .format (
146+ self ._iot_hub_url , self ._device_id , AZ_API_VER
147+ )
142148 return self ._patch (path , properties )
143149
144150 def replace_device_twin (self , properties ):
145151 """Replaces tags and desired properties of a device twin.
146152 :param str properties: Device Twin Properties.
147153 """
148- path = "{0}/twins/{1}?api-version-{2}" .format (self ._iot_hub_url ,
149- self ._device_id , AZ_API_VER )
154+ path = "{0}/twins/{1}?api-version-{2}" .format (
155+ self ._iot_hub_url , self ._device_id , AZ_API_VER
156+ )
150157 return self ._put (path , properties )
151158
152159 # IoT Hub Service
@@ -160,8 +167,9 @@ def get_device(self):
160167 """Gets device information from the identity
161168 registry of an IoT Hub.
162169 """
163- path = "{0}/devices/{1}?api-version={2}" .format (self ._iot_hub_url ,
164- self ._device_id , AZ_API_VER )
170+ path = "{0}/devices/{1}?api-version={2}" .format (
171+ self ._iot_hub_url , self ._device_id , AZ_API_VER
172+ )
165173 return self ._get (path )
166174
167175 # HTTP Helper Methods
@@ -170,10 +178,7 @@ def _post(self, path, payload, return_response=True):
170178 :param str path: Formatted Azure IOT Hub Path.
171179 :param str payload: JSON-formatted Data Payload.
172180 """
173- response = self ._wifi .post (
174- path ,
175- json = payload ,
176- headers = self ._azure_header )
181+ response = self ._wifi .post (path , json = payload , headers = self ._azure_header )
177182 self ._parse_http_status (response .status_code , response .reason )
178183 if return_response :
179184 return response .json ()
@@ -184,9 +189,7 @@ def _get(self, path, is_c2d=False):
184189 :param str path: Formatted Azure IOT Hub Path.
185190 :param bool is_c2d: Cloud-to-device get request.
186191 """
187- response = self ._wifi .get (
188- path ,
189- headers = self ._azure_header )
192+ response = self ._wifi .get (path , headers = self ._azure_header )
190193 status_code = response .status_code
191194 if is_c2d :
192195 if status_code == 200 :
@@ -205,12 +208,10 @@ def _delete(self, path, etag=None):
205208 :param str path: Formatted Azure IOT Hub Path.
206209 """
207210 if etag :
208- data_headers = {"Authorization" :self ._sas_token , "If-Match" :'"%s"' % etag }
211+ data_headers = {"Authorization" : self ._sas_token , "If-Match" : '"%s"' % etag }
209212 else :
210213 data_headers = self ._azure_header
211- response = self ._wifi .delete (
212- path ,
213- headers = data_headers )
214+ response = self ._wifi .delete (path , headers = data_headers )
214215 self ._parse_http_status (response .status_code , response .reason )
215216 status_code = response .status_code
216217 response .close ()
@@ -221,10 +222,7 @@ def _patch(self, path, payload):
221222 :param str path: Formatted Azure IOT Hub Path.
222223 :param str payload: JSON-formatted payload.
223224 """
224- response = self ._wifi .patch (
225- path ,
226- json = payload ,
227- headers = self ._azure_header )
225+ response = self ._wifi .patch (path , json = payload , headers = self ._azure_header )
228226 self ._parse_http_status (response .status_code , response .reason )
229227 json_data = response .json ()
230228 response .close ()
@@ -235,10 +233,7 @@ def _put(self, path, payload=None):
235233 :param str path: Formatted Azure IOT Hub Path.
236234 :param str payload: JSON-formatted payload.
237235 """
238- response = self ._wifi .put (
239- path ,
240- json = payload ,
241- headers = self ._azure_header )
236+ response = self ._wifi .put (path , json = payload , headers = self ._azure_header )
242237 self ._parse_http_status (response .status_code , response .reason )
243238 json_data = response .json ()
244239 response .close ()
0 commit comments