Skip to content

Commit 9968fd2

Browse files
Connection builder simplification (#276)
Adjusts the samples to use the MQTT builder simplification for easier MQTT connection creation. Commit log: * First pass at builder simplification for Python V2 SDK * Bumped Python CRT version to 0.13.4 * Removed use_static_defaults from mqtt builder since it is no longer needed Co-authored-by: Vera Xia <[email protected]>
1 parent ba59a74 commit 9968fd2

File tree

7 files changed

+13
-47
lines changed

7 files changed

+13
-47
lines changed

awsiot/mqtt_connection_builder.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
77
**endpoint** (`str`): Host name of AWS IoT server.
88
9-
**client_bootstrap** (:class:`awscrt.io.ClientBootstrap`): Client bootstrap used to establish connection.
10-
119
**client_id** (`str`): ID to place in CONNECT packet. Must be unique across all devices/clients.
1210
If an ID is already in use, the other client will be disconnected.
1311
1412
Optional Keyword Arguments (omit, or set `None` to get default value):
1513
14+
**client_bootstrap** (:class:`awscrt.io.ClientBootstrap`): Client bootstrap used to establish connection.
15+
The ClientBootstrap will default to the static default (Io.ClientBootstrap.get_or_create_static_default)
16+
if the argument is omitted or set to 'None'.
17+
1618
**on_connection_interrupted** (`Callable`): Callback invoked whenever the MQTT connection is lost.
1719
The MQTT client will automatically attempt to reconnect.
1820
The function should take the following arguments return nothing:
@@ -103,7 +105,7 @@
103105

104106

105107
def _check_required_kwargs(**kwargs):
106-
for required in ['client_bootstrap', 'endpoint', 'client_id']:
108+
for required in ['endpoint', 'client_id']:
107109
if not kwargs.get(required):
108110
raise TypeError("Builder needs keyword-only argument '{}'".format(required))
109111

@@ -186,6 +188,9 @@ def _builder(
186188
username += _get_metrics_str()
187189

188190
client_bootstrap = _get(kwargs, 'client_bootstrap')
191+
if client_bootstrap is None:
192+
client_bootstrap = awscrt.io.ClientBootstrap.get_or_create_static_default()
193+
189194
tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options)
190195
mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx)
191196

samples/basic_discovery.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434

3535
io.init_logging(getattr(LogLevel, args.verbosity), 'stderr')
3636

37-
event_loop_group = io.EventLoopGroup(1)
38-
host_resolver = io.DefaultHostResolver(event_loop_group)
39-
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
40-
4137
tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(args.certificate_path, args.private_key_path)
4238
if args.root_ca_path:
4339
tls_options.override_default_trust_store_from_path(None, args.root_ca_path)
@@ -46,7 +42,7 @@
4642
socket_options = io.SocketOptions()
4743

4844
print('Performing greengrass discovery...')
49-
discovery_client = DiscoveryClient(client_bootstrap, socket_options, tls_context, args.region)
45+
discovery_client = DiscoveryClient(io.ClientBootstrap.get_or_create_static_default(), socket_options, tls_context, args.region)
5046
resp_future = discovery_client.discover(args.thing_name)
5147
discover_response = resp_future.result()
5248

@@ -75,7 +71,6 @@ def try_iot_endpoints():
7571
port=connectivity_info.port,
7672
cert_filepath=args.certificate_path,
7773
pri_key_filepath=args.private_key_path,
78-
client_bootstrap=client_bootstrap,
7974
ca_bytes=gg_group.certificate_authorities[0].encode('utf-8'),
8075
on_connection_interrupted=on_connection_interupted,
8176
on_connection_resumed=on_connection_resumed,

samples/fleetprovisioning.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,14 @@ def waitForRegisterThingResponse():
227227
time.sleep(1)
228228

229229
if __name__ == '__main__':
230-
# Spin up resources
231-
event_loop_group = io.EventLoopGroup(1)
232-
host_resolver = io.DefaultHostResolver(event_loop_group)
233-
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
234-
235230
proxy_options = None
236231
if (args.proxy_host):
237232
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
238233

239234
if args.use_websocket == True:
240-
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
235+
credentials_provider = auth.AwsCredentialsProvider.new_default_chain()
241236
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
242237
endpoint=args.endpoint,
243-
client_bootstrap=client_bootstrap,
244238
region=args.signing_region,
245239
credentials_provider=credentials_provider,
246240
http_proxy_options=proxy_options,
@@ -256,7 +250,6 @@ def waitForRegisterThingResponse():
256250
endpoint=args.endpoint,
257251
cert_filepath=args.cert,
258252
pri_key_filepath=args.key,
259-
client_bootstrap=client_bootstrap,
260253
ca_filepath=args.root_ca,
261254
client_id=args.client_id,
262255
on_connection_interrupted=on_connection_interrupted,

samples/jobs.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,14 @@ def on_update_job_execution_rejected(rejected):
224224
thing_name = args.thing_name
225225
io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr')
226226

227-
# Spin up resources
228-
event_loop_group = io.EventLoopGroup(1)
229-
host_resolver = io.DefaultHostResolver(event_loop_group)
230-
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
231-
232227
proxy_options = None
233228
if (args.proxy_host):
234229
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
235230

236231
if args.use_websocket == True:
237-
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
232+
credentials_provider = auth.AwsCredentialsProvider.new_default_chain()
238233
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
239234
endpoint=args.endpoint,
240-
client_bootstrap=client_bootstrap,
241235
region=args.signing_region,
242236
credentials_provider=credentials_provider,
243237
http_proxy_options=proxy_options,
@@ -251,7 +245,6 @@ def on_update_job_execution_rejected(rejected):
251245
endpoint=args.endpoint,
252246
cert_filepath=args.cert,
253247
pri_key_filepath=args.key,
254-
client_bootstrap=client_bootstrap,
255248
ca_filepath=args.root_ca,
256249
client_id=args.client_id,
257250
clean_session=False,

samples/pkcs11_pubsub.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
7070

7171

7272
if __name__ == '__main__':
73-
# Spin up resources
74-
event_loop_group = io.EventLoopGroup(1)
75-
host_resolver = io.DefaultHostResolver(event_loop_group)
76-
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
77-
7873
print(f"Loading PKCS#11 library '{args.pkcs11_lib}' ...")
7974
pkcs11_lib = io.Pkcs11Lib(
8075
file=args.pkcs11_lib,
@@ -91,7 +86,6 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
9186
cert_filepath=args.cert,
9287
endpoint=args.endpoint,
9388
port=args.port,
94-
client_bootstrap=client_bootstrap,
9589
ca_filepath=args.root_ca,
9690
on_connection_interrupted=on_connection_interrupted,
9791
on_connection_resumed=on_connection_resumed,

samples/pubsub.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,14 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
8585
received_all_event.set()
8686

8787
if __name__ == '__main__':
88-
# Spin up resources
89-
event_loop_group = io.EventLoopGroup(1)
90-
host_resolver = io.DefaultHostResolver(event_loop_group)
91-
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
92-
9388
proxy_options = None
9489
if (args.proxy_host):
9590
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
9691

9792
if args.use_websocket == True:
98-
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
93+
credentials_provider = auth.AwsCredentialsProvider.new_default_chain()
9994
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
10095
endpoint=args.endpoint,
101-
client_bootstrap=client_bootstrap,
10296
region=args.signing_region,
10397
credentials_provider=credentials_provider,
10498
http_proxy_options=proxy_options,
@@ -115,7 +109,6 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
115109
port=args.port,
116110
cert_filepath=args.cert,
117111
pri_key_filepath=args.key,
118-
client_bootstrap=client_bootstrap,
119112
ca_filepath=args.root_ca,
120113
on_connection_interrupted=on_connection_interrupted,
121114
on_connection_resumed=on_connection_resumed,

samples/shadow.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,20 +302,14 @@ def user_input_thread_fn():
302302
shadow_property = args.shadow_property
303303
io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr')
304304

305-
# Spin up resources
306-
event_loop_group = io.EventLoopGroup(1)
307-
host_resolver = io.DefaultHostResolver(event_loop_group)
308-
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
309-
310305
proxy_options = None
311306
if (args.proxy_host):
312307
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
313308

314309
if args.use_websocket == True:
315-
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
310+
credentials_provider = auth.AwsCredentialsProvider.new_default_chain()
316311
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
317312
endpoint=args.endpoint,
318-
client_bootstrap=client_bootstrap,
319313
region=args.signing_region,
320314
credentials_provider=credentials_provider,
321315
http_proxy_options=proxy_options,
@@ -329,7 +323,6 @@ def user_input_thread_fn():
329323
endpoint=args.endpoint,
330324
cert_filepath=args.cert,
331325
pri_key_filepath=args.key,
332-
client_bootstrap=client_bootstrap,
333326
ca_filepath=args.root_ca,
334327
client_id=args.client_id,
335328
clean_session=True,

0 commit comments

Comments
 (0)