Skip to content

Connection builder simplification #276

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 5 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 11 additions & 3 deletions awsiot/mqtt_connection_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -186,6 +188,11 @@ def _builder(
username += _get_metrics_str()

client_bootstrap = _get(kwargs, 'client_bootstrap')
connection_using_static_defaults = _get(kwargs, 'using_static_defaults')
if client_bootstrap is None:
client_bootstrap = awscrt.io.ClientBootstrap.get_or_create_static_default()
connection_using_static_defaults = True

tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options)
mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx)

Expand All @@ -210,6 +217,7 @@ def _builder(
use_websockets=use_websockets,
websocket_handshake_transform=websocket_handshake_transform,
proxy_options=proxy_options,
using_static_defaults=connection_using_static_defaults,
)


Expand Down
7 changes: 1 addition & 6 deletions samples/basic_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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,
Expand Down
9 changes: 1 addition & 8 deletions samples/fleetprovisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 1 addition & 8 deletions samples/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 0 additions & 6 deletions samples/pkcs11_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 1 addition & 8 deletions samples/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 1 addition & 8 deletions samples/shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _load_version():
"Operating System :: OS Independent",
],
install_requires=[
'awscrt==0.13.3',
'awscrt==0.13.4'
],
python_requires='>=3.6',
)