Skip to content

Pylint update #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
source actions-ci/install.sh
- name: Pip install pylint, black, & Sphinx
run: |
pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme
pip install pylint black==19.10b0 Sphinx sphinx-rtd-theme
- name: Library version
run: git describe --dirty --always --tags
- name: PyLint
Expand Down
77 changes: 36 additions & 41 deletions adafruit_azureiot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AzureIoT.git"

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


class IOT_Hub:
"""
Provides access to a Microsoft Azure IoT Hub.
https://docs.microsoft.com/en-us/rest/api/iothub/
"""

def __init__(self, wifi_manager, iot_hub_name, sas_token, device_id):
""" Creates an instance of an Azure IoT Hub Client.
:param wifi_manager: WiFiManager object from ESPSPI_WiFiManager.
Expand All @@ -58,14 +60,14 @@ def __init__(self, wifi_manager, iot_hub_name, sas_token, device_id):
:param str device_id: Unique Azure IoT Device Identifier.
"""
_wifi_type = str(type(wifi_manager))
if 'ESPSPI_WiFiManager' in _wifi_type:
if "ESPSPI_WiFiManager" in _wifi_type:
self._wifi = wifi_manager
else:
raise TypeError("This library requires a WiFiManager object.")
self._iot_hub_url = "https://{0}.azure-devices.net".format(iot_hub_name)
self._sas_token = sas_token
self._device_id = device_id
self._azure_header = {"Authorization":self._sas_token}
self._azure_header = {"Authorization": self._sas_token}

@property
def device_id(self):
Expand Down Expand Up @@ -97,19 +99,20 @@ def get_hub_message(self):
"""
reject_message = True
# get a device-bound notification
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}".format(self._iot_hub_url,
self._device_id,
AZ_API_VER)
path = "{0}/devices/{1}/messages/deviceBound?api-version={2}".format(
self._iot_hub_url, self._device_id, AZ_API_VER
)
data = self._get(path, is_c2d=True)
if data == 204: # device's message queue is empty
if data == 204: # device's message queue is empty
return None
etag = data[1]['etag']
if etag: # either complete or nack the message
etag = data[1]["etag"]
if etag: # either complete or nack the message
reject_message = False
path_complete = "{0}/devices/{1}/messages/deviceBound/{2}?api-version={3}".format(
self._iot_hub_url, self._device_id, etag.strip('\'"'), AZ_API_VER)
self._iot_hub_url, self._device_id, etag.strip("'\""), AZ_API_VER
)
if reject_message:
path_complete += '&reject'
path_complete += "&reject"
del_status = self._delete(path_complete)
if del_status == 204:
return data[0]
Expand All @@ -120,33 +123,37 @@ def send_device_message(self, message):
"""Sends a device-to-cloud message.
:param string message: Message to send to Azure IoT.
"""
path = "{0}/devices/{1}/messages/events?api-version={2}".format(self._iot_hub_url,
self._device_id, AZ_API_VER)
path = "{0}/devices/{1}/messages/events?api-version={2}".format(
self._iot_hub_url, self._device_id, AZ_API_VER
)
self._post(path, message, return_response=False)

# Device Twin
def get_device_twin(self):
"""Returns the device's device twin information in JSON format.
"""
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url,
self._device_id, AZ_API_VER)
path = "{0}/twins/{1}?api-version={2}".format(
self._iot_hub_url, self._device_id, AZ_API_VER
)
return self._get(path)

def update_device_twin(self, properties):
"""Updates tags and desired properties of the device's device twin.
:param str properties: Device Twin Properties
(https://docs.microsoft.com/en-us/rest/api/iothub/service/updatetwin#twinproperties)
"""
path = "{0}/twins/{1}?api-version={2}".format(self._iot_hub_url,
self._device_id, AZ_API_VER)
path = "{0}/twins/{1}?api-version={2}".format(
self._iot_hub_url, self._device_id, AZ_API_VER
)
return self._patch(path, properties)

def replace_device_twin(self, properties):
"""Replaces tags and desired properties of a device twin.
:param str properties: Device Twin Properties.
"""
path = "{0}/twins/{1}?api-version-{2}".format(self._iot_hub_url,
self._device_id, AZ_API_VER)
path = "{0}/twins/{1}?api-version-{2}".format(
self._iot_hub_url, self._device_id, AZ_API_VER
)
return self._put(path, properties)

# IoT Hub Service
Expand All @@ -160,8 +167,9 @@ def get_device(self):
"""Gets device information from the identity
registry of an IoT Hub.
"""
path = "{0}/devices/{1}?api-version={2}".format(self._iot_hub_url,
self._device_id, AZ_API_VER)
path = "{0}/devices/{1}?api-version={2}".format(
self._iot_hub_url, self._device_id, AZ_API_VER
)
return self._get(path)

# HTTP Helper Methods
Expand All @@ -170,10 +178,7 @@ def _post(self, path, payload, return_response=True):
:param str path: Formatted Azure IOT Hub Path.
:param str payload: JSON-formatted Data Payload.
"""
response = self._wifi.post(
path,
json=payload,
headers=self._azure_header)
response = self._wifi.post(path, json=payload, headers=self._azure_header)
self._parse_http_status(response.status_code, response.reason)
if return_response:
return response.json()
Expand All @@ -184,9 +189,7 @@ def _get(self, path, is_c2d=False):
:param str path: Formatted Azure IOT Hub Path.
:param bool is_c2d: Cloud-to-device get request.
"""
response = self._wifi.get(
path,
headers=self._azure_header)
response = self._wifi.get(path, headers=self._azure_header)
status_code = response.status_code
if is_c2d:
if status_code == 200:
Expand All @@ -205,12 +208,10 @@ def _delete(self, path, etag=None):
:param str path: Formatted Azure IOT Hub Path.
"""
if etag:
data_headers = {"Authorization":self._sas_token, "If-Match":'"%s"'%etag}
data_headers = {"Authorization": self._sas_token, "If-Match": '"%s"' % etag}
else:
data_headers = self._azure_header
response = self._wifi.delete(
path,
headers=data_headers)
response = self._wifi.delete(path, headers=data_headers)
self._parse_http_status(response.status_code, response.reason)
status_code = response.status_code
response.close()
Expand All @@ -221,10 +222,7 @@ def _patch(self, path, payload):
:param str path: Formatted Azure IOT Hub Path.
:param str payload: JSON-formatted payload.
"""
response = self._wifi.patch(
path,
json=payload,
headers=self._azure_header)
response = self._wifi.patch(path, json=payload, headers=self._azure_header)
self._parse_http_status(response.status_code, response.reason)
json_data = response.json()
response.close()
Expand All @@ -235,10 +233,7 @@ def _put(self, path, payload=None):
:param str path: Formatted Azure IOT Hub Path.
:param str payload: JSON-formatted payload.
"""
response = self._wifi.put(
path,
json=payload,
headers=self._azure_header)
response = self._wifi.put(path, json=payload, headers=self._azure_header)
self._parse_http_status(response.status_code, response.reason)
json_data = response.json()
response.close()
Expand Down
Loading