diff --git a/awsiot/mqtt_connection_builder.py b/awsiot/mqtt_connection_builder.py index 7612e5ef..cd4bc63d 100644 --- a/awsiot/mqtt_connection_builder.py +++ b/awsiot/mqtt_connection_builder.py @@ -6,13 +6,15 @@ **endpoint** (`str`): Host name of AWS IoT server. - **client_bootstrap** (:class:`awscrt.io.ClientBootstrap`): Client bootstrap used to establish connection. - **client_id** (`str`): ID to place in CONNECT packet. Must be unique across all devices/clients. If an ID is already in use, the other client will be disconnected. Optional Keyword Arguments (omit, or set `None` to get default value): + **client_bootstrap** (:class:`awscrt.io.ClientBootstrap`): Client bootstrap used to establish connection. + The ClientBootstrap will default to the static default (Io.ClientBootstrap.get_or_create_static_default) + if the argument is omitted or set to 'None'. + **on_connection_interrupted** (`Callable`): Callback invoked whenever the MQTT connection is lost. The MQTT client will automatically attempt to reconnect. The function should take the following arguments return nothing: @@ -103,7 +105,7 @@ def _check_required_kwargs(**kwargs): - for required in ['client_bootstrap', 'endpoint', 'client_id']: + for required in ['endpoint', 'client_id']: if not kwargs.get(required): raise TypeError("Builder needs keyword-only argument '{}'".format(required)) @@ -186,6 +188,9 @@ def _builder( username += _get_metrics_str() client_bootstrap = _get(kwargs, 'client_bootstrap') + if client_bootstrap is None: + client_bootstrap = awscrt.io.ClientBootstrap.get_or_create_static_default() + tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options) mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx) diff --git a/samples/basic_discovery.py b/samples/basic_discovery.py index 001e7cbb..ac40ebaa 100644 --- a/samples/basic_discovery.py +++ b/samples/basic_discovery.py @@ -34,10 +34,6 @@ io.init_logging(getattr(LogLevel, args.verbosity), 'stderr') -event_loop_group = io.EventLoopGroup(1) -host_resolver = io.DefaultHostResolver(event_loop_group) -client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) - tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(args.certificate_path, args.private_key_path) if args.root_ca_path: tls_options.override_default_trust_store_from_path(None, args.root_ca_path) @@ -46,7 +42,7 @@ socket_options = io.SocketOptions() print('Performing greengrass discovery...') -discovery_client = DiscoveryClient(client_bootstrap, socket_options, tls_context, args.region) +discovery_client = DiscoveryClient(io.ClientBootstrap.get_or_create_static_default(), socket_options, tls_context, args.region) resp_future = discovery_client.discover(args.thing_name) discover_response = resp_future.result() @@ -75,7 +71,6 @@ def try_iot_endpoints(): port=connectivity_info.port, cert_filepath=args.certificate_path, pri_key_filepath=args.private_key_path, - client_bootstrap=client_bootstrap, ca_bytes=gg_group.certificate_authorities[0].encode('utf-8'), on_connection_interrupted=on_connection_interupted, on_connection_resumed=on_connection_resumed, diff --git a/samples/fleetprovisioning.py b/samples/fleetprovisioning.py index 1384f4bd..b284c9b0 100644 --- a/samples/fleetprovisioning.py +++ b/samples/fleetprovisioning.py @@ -227,20 +227,14 @@ def waitForRegisterThingResponse(): time.sleep(1) if __name__ == '__main__': - # Spin up resources - event_loop_group = io.EventLoopGroup(1) - host_resolver = io.DefaultHostResolver(event_loop_group) - client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) - proxy_options = None if (args.proxy_host): proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port) if args.use_websocket == True: - credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap) + credentials_provider = auth.AwsCredentialsProvider.new_default_chain() mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing( endpoint=args.endpoint, - client_bootstrap=client_bootstrap, region=args.signing_region, credentials_provider=credentials_provider, http_proxy_options=proxy_options, @@ -256,7 +250,6 @@ def waitForRegisterThingResponse(): endpoint=args.endpoint, cert_filepath=args.cert, pri_key_filepath=args.key, - client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, client_id=args.client_id, on_connection_interrupted=on_connection_interrupted, diff --git a/samples/jobs.py b/samples/jobs.py index 8639cd1e..d4d6571f 100644 --- a/samples/jobs.py +++ b/samples/jobs.py @@ -224,20 +224,14 @@ def on_update_job_execution_rejected(rejected): thing_name = args.thing_name io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr') - # Spin up resources - event_loop_group = io.EventLoopGroup(1) - host_resolver = io.DefaultHostResolver(event_loop_group) - client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) - proxy_options = None if (args.proxy_host): proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port) if args.use_websocket == True: - credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap) + credentials_provider = auth.AwsCredentialsProvider.new_default_chain() mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing( endpoint=args.endpoint, - client_bootstrap=client_bootstrap, region=args.signing_region, credentials_provider=credentials_provider, http_proxy_options=proxy_options, @@ -251,7 +245,6 @@ def on_update_job_execution_rejected(rejected): endpoint=args.endpoint, cert_filepath=args.cert, pri_key_filepath=args.key, - client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, client_id=args.client_id, clean_session=False, diff --git a/samples/pkcs11_pubsub.py b/samples/pkcs11_pubsub.py index 7caeb7cf..029d32d2 100644 --- a/samples/pkcs11_pubsub.py +++ b/samples/pkcs11_pubsub.py @@ -70,11 +70,6 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): if __name__ == '__main__': - # Spin up resources - event_loop_group = io.EventLoopGroup(1) - host_resolver = io.DefaultHostResolver(event_loop_group) - client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) - print(f"Loading PKCS#11 library '{args.pkcs11_lib}' ...") pkcs11_lib = io.Pkcs11Lib( file=args.pkcs11_lib, @@ -91,7 +86,6 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): cert_filepath=args.cert, endpoint=args.endpoint, port=args.port, - client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, on_connection_interrupted=on_connection_interrupted, on_connection_resumed=on_connection_resumed, diff --git a/samples/pubsub.py b/samples/pubsub.py index 4c712435..fc34beb3 100644 --- a/samples/pubsub.py +++ b/samples/pubsub.py @@ -85,20 +85,14 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): received_all_event.set() if __name__ == '__main__': - # Spin up resources - event_loop_group = io.EventLoopGroup(1) - host_resolver = io.DefaultHostResolver(event_loop_group) - client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) - proxy_options = None if (args.proxy_host): proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port) if args.use_websocket == True: - credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap) + credentials_provider = auth.AwsCredentialsProvider.new_default_chain() mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing( endpoint=args.endpoint, - client_bootstrap=client_bootstrap, region=args.signing_region, credentials_provider=credentials_provider, http_proxy_options=proxy_options, @@ -115,7 +109,6 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs): port=args.port, cert_filepath=args.cert, pri_key_filepath=args.key, - client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, on_connection_interrupted=on_connection_interrupted, on_connection_resumed=on_connection_resumed, diff --git a/samples/shadow.py b/samples/shadow.py index 4df52bcb..38931fd7 100644 --- a/samples/shadow.py +++ b/samples/shadow.py @@ -302,20 +302,14 @@ def user_input_thread_fn(): shadow_property = args.shadow_property io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr') - # Spin up resources - event_loop_group = io.EventLoopGroup(1) - host_resolver = io.DefaultHostResolver(event_loop_group) - client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver) - proxy_options = None if (args.proxy_host): proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port) if args.use_websocket == True: - credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap) + credentials_provider = auth.AwsCredentialsProvider.new_default_chain() mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing( endpoint=args.endpoint, - client_bootstrap=client_bootstrap, region=args.signing_region, credentials_provider=credentials_provider, http_proxy_options=proxy_options, @@ -329,7 +323,6 @@ def user_input_thread_fn(): endpoint=args.endpoint, cert_filepath=args.cert, pri_key_filepath=args.key, - client_bootstrap=client_bootstrap, ca_filepath=args.root_ca, client_id=args.client_id, clean_session=True,