зеркало из
1
0
Форкнуть 0
* Updated docstring patches for modified APIs
* Updated version
* Updated classifier
* Updated repository URL
* Added migration guide
* Renamed APIs to use "start" and "stop", rather than "enable" and "disable"
This commit is contained in:
Carter Tinney 2022-11-18 16:34:55 -08:00 коммит произвёл GitHub
Родитель 937e8aed11
Коммит bf1506e9e1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
17 изменённых файлов: 370 добавлений и 227 удалений

Просмотреть файл

@ -16,12 +16,48 @@ def execute_patch_for_async():
connect.__doc__ = IoTHubDeviceClient.connect.__doc__
setattr(IoTHubDeviceClient, "connect", connect)
async def stop_method_request_receive(self):
return await super(IoTHubDeviceClient, self).stop_method_request_receive()
stop_method_request_receive.__doc__ = IoTHubDeviceClient.stop_method_request_receive.__doc__
setattr(IoTHubDeviceClient, "stop_method_request_receive", stop_method_request_receive)
async def stop_twin_desired_properties_patch_receive(self):
return await super(IoTHubDeviceClient, self).stop_twin_desired_properties_patch_receive()
stop_twin_desired_properties_patch_receive.__doc__ = (
IoTHubDeviceClient.stop_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubDeviceClient,
"stop_twin_desired_properties_patch_receive",
stop_twin_desired_properties_patch_receive,
)
async def disconnect(self):
return await super(IoTHubDeviceClient, self).disconnect()
disconnect.__doc__ = IoTHubDeviceClient.disconnect.__doc__
setattr(IoTHubDeviceClient, "disconnect", disconnect)
async def start_method_request_receive(self):
return await super(IoTHubDeviceClient, self).start_method_request_receive()
start_method_request_receive.__doc__ = IoTHubDeviceClient.start_method_request_receive.__doc__
setattr(IoTHubDeviceClient, "start_method_request_receive", start_method_request_receive)
async def start_twin_desired_properties_patch_receive(self):
return await super(IoTHubDeviceClient, self).start_twin_desired_properties_patch_receive()
start_twin_desired_properties_patch_receive.__doc__ = (
IoTHubDeviceClient.start_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubDeviceClient,
"start_twin_desired_properties_patch_receive",
start_twin_desired_properties_patch_receive,
)
async def get_twin(self):
return await super(IoTHubDeviceClient, self).get_twin()
@ -130,12 +166,48 @@ def execute_patch_for_async():
connect.__doc__ = IoTHubModuleClient.connect.__doc__
setattr(IoTHubModuleClient, "connect", connect)
async def stop_method_request_receive(self):
return await super(IoTHubModuleClient, self).stop_method_request_receive()
stop_method_request_receive.__doc__ = IoTHubModuleClient.stop_method_request_receive.__doc__
setattr(IoTHubModuleClient, "stop_method_request_receive", stop_method_request_receive)
async def stop_twin_desired_properties_patch_receive(self):
return await super(IoTHubModuleClient, self).stop_twin_desired_properties_patch_receive()
stop_twin_desired_properties_patch_receive.__doc__ = (
IoTHubModuleClient.stop_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubModuleClient,
"stop_twin_desired_properties_patch_receive",
stop_twin_desired_properties_patch_receive,
)
async def disconnect(self):
return await super(IoTHubModuleClient, self).disconnect()
disconnect.__doc__ = IoTHubModuleClient.disconnect.__doc__
setattr(IoTHubModuleClient, "disconnect", disconnect)
async def start_method_request_receive(self):
return await super(IoTHubModuleClient, self).start_method_request_receive()
start_method_request_receive.__doc__ = IoTHubModuleClient.start_method_request_receive.__doc__
setattr(IoTHubModuleClient, "start_method_request_receive", start_method_request_receive)
async def start_twin_desired_properties_patch_receive(self):
return await super(IoTHubModuleClient, self).start_twin_desired_properties_patch_receive()
start_twin_desired_properties_patch_receive.__doc__ = (
IoTHubModuleClient.start_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubModuleClient,
"start_twin_desired_properties_patch_receive",
start_twin_desired_properties_patch_receive,
)
async def get_twin(self):
return await super(IoTHubModuleClient, self).get_twin()

Просмотреть файл

@ -6,7 +6,7 @@
"""This module defines constants for use across the azure-iot-device package
"""
VERSION = "2.12.0"
VERSION = "3.0.0b1"
IOTHUB_IDENTIFIER = "azure-iot-device-iothub-py"
PROVISIONING_IDENTIFIER = "azure-iot-device-provisioning-py"
IOTHUB_API_VERSION = "2019-10-01"

Просмотреть файл

@ -372,27 +372,27 @@ class AbstractIoTHubClient(abc.ABC):
pass
@abc.abstractmethod
def enable_message_receive(self):
def start_message_receive(self):
pass
@abc.abstractmethod
def disable_message_receive(self):
def stop_message_receive(self):
pass
@abc.abstractmethod
def enable_method_request_receive(self):
def start_method_request_receive(self):
pass
@abc.abstractmethod
def disable_method_request_receive(self):
def stop_method_request_receive(self):
pass
@abc.abstractmethod
def enable_twin_desired_properties_patch_receive(self):
def start_twin_desired_properties_patch_receive(self):
pass
@abc.abstractmethod
def disable_twin_desired_properties_patch_receive(self):
def stop_twin_desired_properties_patch_receive(self):
pass
@property

Просмотреть файл

@ -204,9 +204,6 @@ class GenericIoTHubClient(AbstractIoTHubClient):
async def disconnect(self):
"""Disconnect the client from the Azure IoT Hub or Azure IoT Edge Hub instance.
It is recommended that you make sure to call this coroutine when you are completely done
with the your client instance.
:raises: :class:`azure.iot.device.exceptions.ClientError` if there is an unexpected failure
during execution.
"""
@ -290,9 +287,6 @@ class GenericIoTHubClient(AbstractIoTHubClient):
async def send_message(self, message):
"""Sends a message to the default events endpoint on the Azure IoT Hub or Azure IoT Edge Hub instance.
If the connection to the service has not previously been opened by a call to connect, this
function will open the connection before sending the event.
:param message: The actual message to send. Anything passed that is not an instance of the
Message class will be converted to Message object.
:type message: :class:`azure.iot.device.Message` or str
@ -321,9 +315,6 @@ class GenericIoTHubClient(AbstractIoTHubClient):
async def send_method_response(self, method_response):
"""Send a response to a method request via the Azure IoT Hub or Azure IoT Edge Hub.
If the connection to the service has not previously been opened by a call to connect, this
function will open the connection before sending the event.
:param method_response: The MethodResponse to send
:type method_response: :class:`azure.iot.device.MethodResponse`
@ -404,9 +395,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
logger.info("Successfully sent twin patch")
async def enable_method_request_receive(self):
async def start_method_request_receive(self):
"""
Enable the client's ability to receive method requests from IoTHub.
Start receiving method requests from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -417,9 +408,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.METHODS]:
await self._enable_feature(pipeline_constant.METHODS)
async def disable_method_request_receive(self):
async def stop_method_request_receive(self):
"""
Disable the client's ability to receive method requests from IoTHub.
Stop receiving method requests from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -430,9 +421,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
if self._mqtt_pipeline.feature_enabled[pipeline_constant.METHODS]:
await self._disable_feature(pipeline_constant.METHODS)
async def enable_twin_desired_properties_patch_receive(self):
async def start_twin_desired_properties_patch_receive(self):
"""
Enable the client's ability to receive twin desired property patches from IoTHub.
Start receiving twin desired property patches from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -443,9 +434,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.TWIN_PATCHES]:
await self._enable_feature(pipeline_constant.TWIN_PATCHES)
async def disable_twin_desired_properties_patch_receive(self):
async def stop_twin_desired_properties_patch_receive(self):
"""
Disable the client's ability to receive twin desired property patches from IoTHub.
Stop receiving twin desired property patches from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -514,9 +505,9 @@ class IoTHubDeviceClient(GenericIoTHubClient, AbstractIoTHubDeviceClient):
await handle_result(callback)
logger.info("Successfully notified blob upload status")
async def enable_message_receive(self):
async def start_message_receive(self):
"""
Enable the client's ability to receive cloud-to-device messages from IoTHub.
Start receiving cloud-to-device messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -527,9 +518,9 @@ class IoTHubDeviceClient(GenericIoTHubClient, AbstractIoTHubDeviceClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.C2D_MSG]:
await self._enable_feature(pipeline_constant.C2D_MSG)
async def disable_message_receive(self):
async def stop_message_receive(self):
"""
Disable the client's ability to receive cloud-to-device messages from IoTHub.
Stop receiving cloud-to-device messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -561,9 +552,6 @@ class IoTHubModuleClient(GenericIoTHubClient, AbstractIoTHubModuleClient):
These are outgoing events and are meant to be "output events"
If the connection to the service has not previously been opened by a call to connect, this
function will open the connection before sending the event.
:param message: Message to send to the given output. Anything passed that is not an
instance of the Message class will be converted to Message object.
:type message: :class:`azure.iot.device.Message` or str
@ -617,9 +605,9 @@ class IoTHubModuleClient(GenericIoTHubClient, AbstractIoTHubModuleClient):
logger.info("Successfully invoked method")
return method_response
async def enable_message_receive(self):
async def start_message_receive(self):
"""
Enable the client's ability to receive input messages from IoTHub.
Start receiving input messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -630,9 +618,9 @@ class IoTHubModuleClient(GenericIoTHubClient, AbstractIoTHubModuleClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.INPUT_MSG]:
await self._enable_feature(pipeline_constant.INPUT_MSG)
async def disable_message_receive(self):
async def stop_message_receive(self):
"""
Disable the client's ability to receive input messages from IoTHub.
Stop receiving input messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected

Просмотреть файл

@ -207,9 +207,6 @@ class GenericIoTHubClient(AbstractIoTHubClient):
def disconnect(self):
"""Disconnect the client from the Azure IoT Hub or Azure IoT Edge Hub instance.
It is recommended that you make sure to call this function when you are completely done
with the your client instance.
This is a synchronous call, meaning that this function will not return until the connection
to the service has been completely closed.
@ -324,9 +321,6 @@ class GenericIoTHubClient(AbstractIoTHubClient):
This is a synchronous event, meaning that this function will not return until the event
has been sent to the service and the service has acknowledged receipt of the event.
If the connection to the service has not previously been opened by a call to connect, this
function will open the connection before sending the event.
:param method_response: The MethodResponse to send.
:type method_response: :class:`azure.iot.device.MethodResponse`
@ -401,9 +395,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
logger.info("Successfully patched twin")
def enable_method_request_receive(self):
def start_method_request_receive(self):
"""
Enable the client's ability to receive method requests from IoTHub.
Start receiving method requests from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -414,9 +408,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.METHODS]:
self._enable_feature(pipeline_constant.METHODS)
def disable_method_request_receive(self):
def stop_method_request_receive(self):
"""
Disable the client's ability to receive method requests from IoTHub.
Stop receiving method requests from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -427,9 +421,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
if self._mqtt_pipeline.feature_enabled[pipeline_constant.METHODS]:
self._disable_feature(pipeline_constant.METHODS)
def enable_twin_desired_properties_patch_receive(self):
def start_twin_desired_properties_patch_receive(self):
"""
Enable the client's ability to receive twin desired property patches from IoTHub.
Start receiving twin desired property patches from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -440,9 +434,9 @@ class GenericIoTHubClient(AbstractIoTHubClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.TWIN_PATCHES]:
self._enable_feature(pipeline_constant.TWIN_PATCHES)
def disable_twin_desired_properties_patch_receive(self):
def stop_twin_desired_properties_patch_receive(self):
"""
Disable the client's ability to receive twin desired property patches from IoTHub.
Stop receiving twin desired property patches from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -510,9 +504,9 @@ class IoTHubDeviceClient(GenericIoTHubClient, AbstractIoTHubDeviceClient):
handle_result(callback)
logger.info("Successfully notified blob upload status")
def enable_message_receive(self):
def start_message_receive(self):
"""
Enable the client's ability to receive cloud-to-device messages from IoTHub.
Start receiving cloud-to-device messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -523,9 +517,9 @@ class IoTHubDeviceClient(GenericIoTHubClient, AbstractIoTHubDeviceClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.C2D_MSG]:
self._enable_feature(pipeline_constant.C2D_MSG)
def disable_message_receive(self):
def stop_message_receive(self):
"""
Disable the client's ability to receive cloud-to-device messages from IoTHub.
Stop receiving cloud-to-device messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -562,9 +556,6 @@ class IoTHubModuleClient(GenericIoTHubClient, AbstractIoTHubModuleClient):
This is a synchronous event, meaning that this function will not return until the event
has been sent to the service and the service has acknowledged receipt of the event.
If the connection to the service has not previously been opened by a call to connect, this
function will open the connection before sending the event.
:param message: Message to send to the given output. Anything passed that is not an instance of the
Message class will be converted to Message object.
:type message: :class:`azure.iot.device.Message` or str
@ -614,9 +605,9 @@ class IoTHubModuleClient(GenericIoTHubClient, AbstractIoTHubModuleClient):
logger.info("Successfully invoked method")
return invoke_method_response
def enable_message_receive(self):
def start_message_receive(self):
"""
Enable the client's ability to receive input messages from IoTHub.
Start receiving input messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected
@ -627,9 +618,9 @@ class IoTHubModuleClient(GenericIoTHubClient, AbstractIoTHubModuleClient):
if not self._mqtt_pipeline.feature_enabled[pipeline_constant.INPUT_MSG]:
self._enable_feature(pipeline_constant.INPUT_MSG)
def disable_message_receive(self):
def stop_message_receive(self):
"""
Disable the client's ability to receive input messages from IoTHub.
Stop receiving input messages from IoTHub.
:raises: :class:`azure.iot.device.exceptions.NoConnectionError` if the client is not
connected

Просмотреть файл

@ -16,12 +16,48 @@ def execute_patch_for_sync():
connect.__doc__ = IoTHubDeviceClient.connect.__doc__
setattr(IoTHubDeviceClient, "connect", connect)
def stop_method_request_receive(self):
return super(IoTHubDeviceClient, self).stop_method_request_receive()
stop_method_request_receive.__doc__ = IoTHubDeviceClient.stop_method_request_receive.__doc__
setattr(IoTHubDeviceClient, "stop_method_request_receive", stop_method_request_receive)
def stop_twin_desired_properties_patch_receive(self):
return super(IoTHubDeviceClient, self).stop_twin_desired_properties_patch_receive()
stop_twin_desired_properties_patch_receive.__doc__ = (
IoTHubDeviceClient.stop_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubDeviceClient,
"stop_twin_desired_properties_patch_receive",
stop_twin_desired_properties_patch_receive,
)
def disconnect(self):
return super(IoTHubDeviceClient, self).disconnect()
disconnect.__doc__ = IoTHubDeviceClient.disconnect.__doc__
setattr(IoTHubDeviceClient, "disconnect", disconnect)
def start_method_request_receive(self):
return super(IoTHubDeviceClient, self).start_method_request_receive()
start_method_request_receive.__doc__ = IoTHubDeviceClient.start_method_request_receive.__doc__
setattr(IoTHubDeviceClient, "start_method_request_receive", start_method_request_receive)
def start_twin_desired_properties_patch_receive(self):
return super(IoTHubDeviceClient, self).start_twin_desired_properties_patch_receive()
start_twin_desired_properties_patch_receive.__doc__ = (
IoTHubDeviceClient.start_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubDeviceClient,
"start_twin_desired_properties_patch_receive",
start_twin_desired_properties_patch_receive,
)
def get_twin(self):
return super(IoTHubDeviceClient, self).get_twin()
@ -130,12 +166,48 @@ def execute_patch_for_sync():
connect.__doc__ = IoTHubModuleClient.connect.__doc__
setattr(IoTHubModuleClient, "connect", connect)
def stop_method_request_receive(self):
return super(IoTHubModuleClient, self).stop_method_request_receive()
stop_method_request_receive.__doc__ = IoTHubModuleClient.stop_method_request_receive.__doc__
setattr(IoTHubModuleClient, "stop_method_request_receive", stop_method_request_receive)
def stop_twin_desired_properties_patch_receive(self):
return super(IoTHubModuleClient, self).stop_twin_desired_properties_patch_receive()
stop_twin_desired_properties_patch_receive.__doc__ = (
IoTHubModuleClient.stop_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubModuleClient,
"stop_twin_desired_properties_patch_receive",
stop_twin_desired_properties_patch_receive,
)
def disconnect(self):
return super(IoTHubModuleClient, self).disconnect()
disconnect.__doc__ = IoTHubModuleClient.disconnect.__doc__
setattr(IoTHubModuleClient, "disconnect", disconnect)
def start_method_request_receive(self):
return super(IoTHubModuleClient, self).start_method_request_receive()
start_method_request_receive.__doc__ = IoTHubModuleClient.start_method_request_receive.__doc__
setattr(IoTHubModuleClient, "start_method_request_receive", start_method_request_receive)
def start_twin_desired_properties_patch_receive(self):
return super(IoTHubModuleClient, self).start_twin_desired_properties_patch_receive()
start_twin_desired_properties_patch_receive.__doc__ = (
IoTHubModuleClient.start_twin_desired_properties_patch_receive.__doc__
)
setattr(
IoTHubModuleClient,
"start_twin_desired_properties_patch_receive",
start_twin_desired_properties_patch_receive,
)
def get_twin(self):
return super(IoTHubModuleClient, self).get_twin()

Просмотреть файл

@ -1,144 +1,164 @@
# IoTHub Python SDK Migration Guide
This guide details the migration plan to move from the IoTHub Python v1 code base to the new and improved v2
code base. Note that this guide assumes the use of asynchronous code.
This guide details how to update existing code that uses an `azure-iot-device` V2 release to use a V3 release instead. While the APIs remain mostly the same, there are a few differences you may need to account for in your application, as we have removed some of the implicit behaviors present in V2 in order to provide a more reliable and consistent user experience.
## Installing the IoTHub Python SDK
## Connecting to IoTHub
One of the primary changes in V3 is the removal of automatic connections when invoking other APIs on the `IoTHubDeviceClient` and `IoTHubModuleClient`. You must now make an explicit manual connection before sending or receiving any data.
- v1
```Shell
pip install azure-iothub-device-client
### V2
```python
from azure.iot.device import IoTHubDeviceClient
client = IoTHubDeviceClient.create_from_connection_string("<Your Connection String>")
client.send_message("some message")
```
- v2
### V3
```python
from azure.iot.device import IoTHubDeviceClient
```Shell
pip install azure-iot-device
client = IoTHubDeviceClient.create_from_connection_string("<Your Connection String>")
client.connect()
client.send_message("some message")
```
## Creating a device client
Note that many people using V2 may already have been doing manual connects, as for some time, this has been our recommended practice.
When creating a device client on the V1 client the protocol was specified on in the constructor. With the v2 SDK we are
currently only supporting the MQTT protocol so it only requires to supply the connection string when you create the client.
Note also that this change does *not* affect automatic reconnection attempts in the case of network failure. Once the manual connect has been successful, the client will (under default settings) still attempt to retain that connected state as it did in V2.
### Symmetric Key authentication
- v1
## Receiving data from IoTHub
Similarly to the above, there is an additional explicit step you must now make when trying to receive data. In addition to setting your handler, you must explicitly start/stop receiving. Note also that the above step of manually connecting must also be done before starting to receive data.
```Python
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue
### V2
```python
from azure.iot.device import IoTHubDeviceClient
client = IoTHubClient(connection_string, IoTHubTransportProvider.MQTT)
client = IoTHubDeviceClient.create_from_connection_string("<Your Connection String>")
# define behavior for receiving a message
def message_handler(message):
print("the data in the message received was ")
print(message.data)
print("custom properties are")
print(message.custom_properties)
# set the message handler on the client
client.on_message_received = message_handler
```
- v2
### V3
```python
from azure.iot.device import IoTHubDeviceClient
```Python
from azure.iot.device.aio import IoTHubDeviceClient
from azure.iot.device import Message
client = IoTHubDeviceClient.create_from_connection_string("<Your Connection String>")
client = IoTHubDeviceClient.create_from_connection_string(connection_string)
await device_client.connect()
# define behavior for receiving a message
def message_handler(message):
print("the data in the message received was ")
print(message.data)
print("custom properties are")
print(message.custom_properties)
# set the message handler on the client
client.on_message_received = message_handler
# connect and start receiving messages
client.connect()
client.start_message_receive()
```
### x.509 authentication
Note that this must be done not just for receiving messages, but receiving any data. Consult the chart below to see which APIs you will need for the type of data you are receiving.
For x.509 device the v1 SDK required the user to supply the certificates in a call to set_options. Moving forward in the v2
SDK, we only require for the user to call the create function with an x.509 object containing the path to the x.509 file and
key file with the optional pass phrase if necessary.
- v1
| Data Type | Handler name | Start Receive API | Stop Receive API |
|---------------------------------|----------------------------------------------|--------------------------------------------------|-------------------------------------------------|
| Messages | `.on_message_received` | `.start_message_receive()` | `.stop_message_receive()` |
| Method Requests | `.on_method_request_received` | `.start_method_request_receive()` | `.stop_method_request_receive()` |
| Twin Desired Properties Patches | `.on_twin_desired_properties_patch_received` | `.start_twin_desired_properties_patch_receive()` | `.stop_twin_desired_properties_patch_receive()` |
```Python
from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue
client = IoTHubClient(connection_string, IoTHubTransportProvider.MQTT)
# Get the x.509 certificate information
client.set_option("x509certificate", X509_CERTIFICATE)
client.set_option("x509privatekey", X509_PRIVATEKEY)
Finally, it should be clarified that the following receive APIs that were deprecated in V2 have been fully removed in V3:
* `.receive_message()`
* `.receive_message_on_input()`
* `.receive_method_request()`
* `.receive_twin_desired_properties_patch()`
All receives should now be done using the handlers in the table above.
## Modified Client Options - IoTHubDeviceClient/IoTHubModuleClient
Some keyword arguments provided at client creation have changed or been removed
| V2 | V3 | Explanation |
|-----------------------------|-------------|----------------------------------------|
| `auto_connect` | **REMOVED** | Initial manual connection now required |
| `ensure_desired_properties` | **REMOVED** | No more implicit twin updates |
## Shutting down - IoTHubDeviceClient/IoTHubModuleClient
While using the `.shutdown()` method when you are completely finished with an instance of the client has been a highly recommended practice for some time, some early versions of V2 did not require it. As of V3, in order to ensure a graceful exit, you must make an explicit shutdown.
### V2
```python
from azure.iot.device import IoTHubDeviceClient
client = IoTHubDeviceClient.create_from_connection_string("<Your Connection String>")
# ...
#<do things>
# ...
```
- v2
### V3
```python
from azure.iot.device import IoTHubDeviceClient
```Python
from azure.iot.device.aio import IoTHubDeviceClient
from azure.iot.device import Message
client = IoTHubDeviceClient.create_from_connection_string("<Your Connection String>")
# Get the x.509 certificate path from the environment
x509 = X509(
cert_file=os.getenv("X509_CERT_FILE"),
key_file=os.getenv("X509_KEY_FILE"),
pass_phrase=os.getenv("X509_PASS_PHRASE")
# ...
#<do things>
# ...
client.shutdown()
```
## Shutting down - ProvisioningDeviceClient
As with the IoTHub clients mentioned above, the Provisioning clients now also require shutdown. This was implicit in V2, but now it must be explicit and manual to ensure graceful exit.
### V2
```python
from azure.iot.device import ProvisioningDeviceClient
client = ProvisioningDeviceClient.create_from_symmetric_key(
provisioning_host="<Your provisioning host>",
registration_id="<Your registration id>",
id_scope="<Your id scope>",
symmetric_key="<Your symmetric key">,
)
client = IoTHubDeviceClient.create_from_x509_certificate(hostname=hostname, device_id=device_id, x509=x509)
await device_client.connect()
registration_result = client.register()
# Shutdown is implicit upon successful registration
```
## Sending Telemetry to IoTHub
### V3
```python
from azure.iot.device import ProvisioningDeviceClient
- v1
client = ProvisioningDeviceClient.create_from_symmetric_key(
provisioning_host="<Your provisioning host>",
registration_id="<Your registration id>",
id_scope="<Your id scope>",
symmetric_key="<Your symmetric key">,
)
```Python
# create the device client
registration_result = client.register()
message = IoTHubMessage("telemetry message")
message.message_id = "message id"
message.correlation_id = "correlation-id"
prop_map = message.properties()
prop_map.add("property", "property_value")
client.send_event_async(message, send_confirmation_callback, user_ctx)
```
- v2
```Python
# create the device client
message = Message("telemetry message")
message.message_id = "message id"
message.correlation_id = "correlation id"
message.custom_properties["property"] = "property_value"
await client.send_message(message)
```
## Receiving a Message from IoTHub
- v1
```Python
# create the device client
def receive_message_callback(message, counter):
global RECEIVE_CALLBACKS
message = message.get_bytearray()
size = len(message_buffer)
print ( "the data in the message received was : <<<%s>>> & Size=%d" % (message_buffer[:size].decode('utf-8'), size) )
map_properties = message.properties()
key_value_pair = map_properties.get_internals()
print ( "custom properties are: %s" % key_value_pair )
return IoTHubMessageDispositionResult.ACCEPTED
client.set_message_callback(message_listener_callback, RECEIVE_CONTEXT)
```
- v2
```Python
# create the device client
# define behavior for receiving a message
def message_handler(message):
print("the data in the message received was ")
print(message.data)
print("custom properties are")
print(message.custom_properties)
# set the message handler on the client
client.on_message_received = message_handler
```
# Manual shutdown for graceful exit
client.shutdown()
```

Просмотреть файл

@ -53,13 +53,13 @@ setup(
description="Microsoft Azure IoT Device Library",
license="MIT License",
license_files=("LICENSE",),
url="https://github.com/Azure/azure-iot-sdk-python/",
url="https://github.com/Azure/azure-iot-sdk-python/tree/v3",
author="Microsoft Corporation",
author_email="opensource@microsoft.com",
long_description=_long_description,
long_description_content_type="text/markdown",
classifiers=[
"Development Status :: 5 - Production/Stable",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries :: Python Modules",
"License :: OSI Approved :: MIT License",

Просмотреть файл

@ -35,7 +35,7 @@ class TestReceiveC2d(object):
event_loop.call_soon_threadsafe(received.set)
client.on_message_received = handle_on_message_received
await client.enable_message_receive()
await client.start_message_receive()
await service_helper.send_c2d(message, {})

Просмотреть файл

@ -63,7 +63,7 @@ class TestMethods(object):
)
client.on_method_request_received = handle_on_method_request_received
await client.enable_method_request_receive()
await client.start_method_request_receive()
# invoke the method call
method_response = await service_helper.invoke_method(method_name, request_payload)

Просмотреть файл

@ -981,7 +981,7 @@ class TestDesiredProperties(object):
event_loop.call_soon_threadsafe(received.set)
client.on_twin_desired_properties_patch_received = handle_on_patch_received
await client.enable_twin_desired_properties_patch_receive()
await client.start_twin_desired_properties_patch_receive()
random_dict = get_random_dict()
await service_helper.set_desired_properties(

Просмотреть файл

@ -173,7 +173,7 @@ class TestTwinStress(object):
asyncio.run_coroutine_threadsafe(patches.put(patch), event_loop)
client.on_twin_desired_properties_patch_received = handle_on_patch_received
await client.enable_twin_desired_properties_patch_receive()
await client.start_twin_desired_properties_patch_receive()
for i in range(iteration_count):
logger.info("Iteration {} of {}".format(i, iteration_count))
@ -218,7 +218,7 @@ class TestTwinStress(object):
asyncio.run_coroutine_threadsafe(patches.put(patch), event_loop)
client.on_twin_desired_properties_patch_received = handle_on_patch_received
await client.enable_twin_desired_properties_patch_receive()
await client.start_twin_desired_properties_patch_receive()
props = {"key_{}".format(k): None for k in range(0, batch_size)}

Просмотреть файл

@ -33,7 +33,7 @@ class TestReceiveC2d(object):
received.set()
client.on_message_received = handle_on_message_received
client.enable_message_receive()
client.start_message_receive()
service_helper.send_c2d(message, {})

Просмотреть файл

@ -59,7 +59,7 @@ class TestMethods(object):
)
client.on_method_request_received = handle_on_method_request_received
client.enable_method_request_receive()
client.start_method_request_receive()
# invoke the method call
method_response = service_helper.invoke_method(method_name, request_payload)

Просмотреть файл

@ -990,7 +990,7 @@ class TestDesiredProperties(object):
received_patches.put(patch)
client.on_twin_desired_properties_patch_received = handle_on_patch_received
client.enable_twin_desired_properties_patch_receive()
client.start_twin_desired_properties_patch_receive()
# erase all old desired properties. Otherwise our random dict will only
# be part of the twin we get when we call `get_twin` below (because of

Просмотреть файл

@ -1637,52 +1637,52 @@ class TestIoTHubDeviceClientNotifyBlobUploadStatus(IoTHubDeviceClientTestsConfig
assert e_info.value.__cause__ is my_pipeline_error
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .enable_message_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .start_message_receive()")
class TestIoTHubDeviceClientEnableMessageReceive(
IoTHubDeviceClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_message_receive
return client.start_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.C2D_MSG
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .disable_message_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .stop_message_receive()")
class TestIoTHubDeviceClientDisableMessageReceive(
IoTHubDeviceClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_message_receive
return client.stop_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.C2D_MSG
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .enable_method_request_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .start_method_request_receive()")
class TestIoTHubDeviceClientEnableMethodRequestReceive(
IoTHubDeviceClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_method_request_receive
return client.start_method_request_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.METHODS
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .disable_method_request_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Asynchronous) - .stop_method_request_receive()")
class TestIoTHubDeviceClientDisableMethodRequestReceive(
IoTHubDeviceClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_method_request_receive
return client.stop_method_request_receive
@pytest.fixture
def feature_name(self):
@ -1690,14 +1690,14 @@ class TestIoTHubDeviceClientDisableMethodRequestReceive(
@pytest.mark.describe(
"IoTHubDeviceClient (Asynchronous) - .enable_twin_desired_properties_patch_receive()"
"IoTHubDeviceClient (Asynchronous) - .start_twin_desired_properties_patch_receive()"
)
class TestIoTHubDeviceClientEnableTwinPatchtReceive(
IoTHubDeviceClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_twin_desired_properties_patch_receive
return client.start_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):
@ -1705,14 +1705,14 @@ class TestIoTHubDeviceClientEnableTwinPatchtReceive(
@pytest.mark.describe(
"IoTHubDeviceClient (Asynchronous) - .disable_twin_desired_properties_patch_receive()"
"IoTHubDeviceClient (Asynchronous) - .stop_twin_desired_properties_patch_receive()"
)
class TestIoTHubDeviceClientDisableTwinPatchtReceive(
IoTHubDeviceClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_twin_desired_properties_patch_receive
return client.stop_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):
@ -2261,52 +2261,52 @@ class TestIoTHubModuleClientInvokeMethod(IoTHubModuleClientTestsConfig):
assert e_info.value.__cause__ is my_pipeline_error
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .enable_message_receive()")
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .start_message_receive()")
class TestIoTHubModuleClientEnableMessageReceive(
IoTHubModuleClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_message_receive
return client.start_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.INPUT_MSG
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .disable_message_receive()")
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .stop_message_receive()")
class TestIoTHubModuleClientDisableMessageReceive(
IoTHubModuleClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_message_receive
return client.stop_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.INPUT_MSG
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .enable_method_request_receive()")
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .start_method_request_receive()")
class TestIoTHubModuleClientEnableMethodRequestReceive(
IoTHubModuleClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_method_request_receive
return client.start_method_request_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.METHODS
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .disable_method_request_receive()")
@pytest.mark.describe("IoTHubModuleClient (Asynchronous) - .stop_method_request_receive()")
class TestIoTHubModuleClientDisableMethodRequestReceive(
IoTHubModuleClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_method_request_receive
return client.stop_method_request_receive
@pytest.fixture
def feature_name(self):
@ -2314,14 +2314,14 @@ class TestIoTHubModuleClientDisableMethodRequestReceive(
@pytest.mark.describe(
"IoTHubModuleClient (Asynchronous) - .enable_twin_desired_properties_patch_receive()"
"IoTHubModuleClient (Asynchronous) - .start_twin_desired_properties_patch_receive()"
)
class TestIoTHubModuleClientEnableTwinPatchReceive(
IoTHubModuleClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_twin_desired_properties_patch_receive
return client.start_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):
@ -2329,14 +2329,14 @@ class TestIoTHubModuleClientEnableTwinPatchReceive(
@pytest.mark.describe(
"IoTHubModuleClient (Asynchronous) - .disable_twin_desired_properties_patch_receive()"
"IoTHubModuleClient (Asynchronous) - .stop_twin_desired_properties_patch_receive()"
)
class TestIoTHubModuleClientDisableTwinPatchReceive(
IoTHubModuleClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_twin_desired_properties_patch_receive
return client.stop_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):

Просмотреть файл

@ -1616,52 +1616,52 @@ class TestIoTHubDeviceClientNotifyBlobUploadStatus(
assert e_info.value.__cause__ is my_pipeline_error
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .enable_message_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .start_message_receive()")
class TestIoTHubDeviceClientEnableMessageReceive(
IoTHubDeviceClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_message_receive
return client.start_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.C2D_MSG
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .disable_message_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .stop_message_receive()")
class TestIoTHubDeviceClientDisableMessageReceive(
IoTHubDeviceClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_message_receive
return client.stop_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.C2D_MSG
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .enable_method_request_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .start_method_request_receive()")
class TestIoTHubDeviceClientEnableMethodRequestReceive(
IoTHubDeviceClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_method_request_receive
return client.start_method_request_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.METHODS
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .disable_method_request_receive()")
@pytest.mark.describe("IoTHubDeviceClient (Synchronous) - .stop_method_request_receive()")
class TestIoTHubDeviceClientDisableMethodRequestReceive(
IoTHubDeviceClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_method_request_receive
return client.stop_method_request_receive
@pytest.fixture
def feature_name(self):
@ -1669,14 +1669,14 @@ class TestIoTHubDeviceClientDisableMethodRequestReceive(
@pytest.mark.describe(
"IoTHubDeviceClient (Synchronous) - .enable_twin_desired_properties_patch_receive()"
"IoTHubDeviceClient (Synchronous) - .start_twin_desired_properties_patch_receive()"
)
class TestIoTHubDeviceClientEnableTwinPatchtReceive(
IoTHubDeviceClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_twin_desired_properties_patch_receive
return client.start_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):
@ -1684,14 +1684,14 @@ class TestIoTHubDeviceClientEnableTwinPatchtReceive(
@pytest.mark.describe(
"IoTHubDeviceClient (Synchronous) - .disable_twin_desired_properties_patch_receive()"
"IoTHubDeviceClient (Synchronous) - .stop_twin_desired_properties_patch_receive()"
)
class TestIoTHubDeviceClientDisableTwinPatchtReceive(
IoTHubDeviceClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_twin_desired_properties_patch_receive
return client.stop_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):
@ -2230,52 +2230,52 @@ class TestIoTHubModuleClientInvokeMethod(WaitsForEventCompletion, IoTHubModuleCl
assert e_info.value.__cause__ is my_pipeline_error
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .enable_message_receive()")
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .start_message_receive()")
class TestIoTHubModuleClientEnableMessageReceive(
IoTHubModuleClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_message_receive
return client.start_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.INPUT_MSG
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .disable_message_receive()")
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .stop_message_receive()")
class TestIoTHubModuleClientDisableMessageReceive(
IoTHubModuleClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_message_receive
return client.stop_message_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.INPUT_MSG
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .enable_method_request_receive()")
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .start_method_request_receive()")
class TestIoTHubModuleClientEnableMethodRequestReceive(
IoTHubModuleClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_method_request_receive
return client.start_method_request_receive
@pytest.fixture
def feature_name(self):
return pipeline_constant.METHODS
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .disable_method_request_receive()")
@pytest.mark.describe("IoTHubModuleClient (Synchronous) - .stop_method_request_receive()")
class TestIoTHubModuleClientDisableMethodRequestReceive(
IoTHubModuleClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_method_request_receive
return client.stop_method_request_receive
@pytest.fixture
def feature_name(self):
@ -2283,14 +2283,14 @@ class TestIoTHubModuleClientDisableMethodRequestReceive(
@pytest.mark.describe(
"IoTHubModuleClient (Synchronous) - .enable_twin_desired_properties_patch_receive()"
"IoTHubModuleClient (Synchronous) - .start_twin_desired_properties_patch_receive()"
)
class TestIoTHubModuleClientEnableTwinPatchReceive(
IoTHubModuleClientTestsConfig, SharedClientEnableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.enable_twin_desired_properties_patch_receive
return client.start_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):
@ -2298,14 +2298,14 @@ class TestIoTHubModuleClientEnableTwinPatchReceive(
@pytest.mark.describe(
"IoTHubModuleClient (Synchronous) - .disable_twin_desired_properties_patch_receive()"
"IoTHubModuleClient (Synchronous) - .stop_twin_desired_properties_patch_receive()"
)
class TestIoTHubModuleClientDisableTwinPatchReceive(
IoTHubModuleClientTestsConfig, SharedClientDisableReceiveTests
):
@pytest.fixture
def client_method(self, client):
return client.disable_twin_desired_properties_patch_receive
return client.stop_twin_desired_properties_patch_receive
@pytest.fixture
def feature_name(self):