11
11
**client_id** (`str`): ID to place in CONNECT packet. Must be unique across all devices/clients.
12
12
If an ID is already in use, the other client will be disconnected.
13
13
14
- Optional Keyword Arguments:
14
+ Optional Keyword Arguments (omit, or set `None` to get default value) :
15
15
16
16
**on_connection_interrupted** (`Callable`): Callback invoked whenever the MQTT connection is lost.
17
17
The MQTT client will automatically attempt to reconnect.
@@ -106,6 +106,20 @@ def _check_required_kwargs(**kwargs):
106
106
raise TypeError ("Builder needs keyword-only argument '{}'" .format (required ))
107
107
108
108
109
+ def _get (kwargs , name , default = None ):
110
+ """
111
+ Returns kwargs['name'] if it exists and is not None.
112
+ Otherwise returns default.
113
+
114
+ This function exists so users can pass some_arg=None to get its default
115
+ value, instead of literally passing None.
116
+ """
117
+ val = kwargs .get (name )
118
+ if val is None :
119
+ val = default
120
+ return val
121
+
122
+
109
123
_metrics_str = None
110
124
111
125
@@ -132,64 +146,64 @@ def _builder(
132
146
websocket_proxy_options = None ,
133
147
** kwargs ):
134
148
135
- ca_bytes = kwargs . get ( 'ca_bytes' )
136
- ca_filepath = kwargs . get ( 'ca_filepath' )
137
- ca_dirpath = kwargs . get ( 'ca_dirpath' )
149
+ ca_bytes = _get ( kwargs , 'ca_bytes' )
150
+ ca_filepath = _get ( kwargs , 'ca_filepath' )
151
+ ca_dirpath = _get ( kwargs , 'ca_dirpath' )
138
152
if ca_bytes :
139
153
tls_ctx_options .override_default_trust_store (ca_bytes )
140
154
elif ca_filepath or ca_dirpath :
141
155
tls_ctx_options .override_default_trust_store_from_path (ca_dirpath , ca_filepath )
142
156
143
- if use_websockets :
144
- port = 443
145
- if awscrt .io .is_alpn_available ():
146
- tls_ctx_options .alpn_list = ['http/1.1' ]
147
- else :
148
- port = 8883
149
- if awscrt .io .is_alpn_available ():
157
+ port = _get (kwargs , 'port' )
158
+ if port is None :
159
+ # prefer 443, even for direct MQTT connections, since it's less likely to be blocked by firewalls
160
+ if use_websockets or awscrt .io .is_alpn_available ():
150
161
port = 443
151
- tls_ctx_options .alpn_list = ['x-amzn-mqtt-ca' ]
162
+ else :
163
+ port = 8883
152
164
153
- port = kwargs .get ('port' , port )
165
+ if port == 443 and awscrt .io .is_alpn_available ():
166
+ tls_ctx_options .alpn_list = ['http/1.1' ] if use_websockets else ['x-amzn-mqtt-ca' ]
154
167
155
168
socket_options = awscrt .io .SocketOptions ()
156
- socket_options .connect_timeout_ms = kwargs . get ( 'tcp_connect_timeout_ms' , 5000 )
169
+ socket_options .connect_timeout_ms = _get ( kwargs , 'tcp_connect_timeout_ms' , 5000 )
157
170
# These have been inconsistent between keepalive/keep_alive. Resolve both for now to ease transition.
158
- socket_options .keep_alive = kwargs .get ('tcp_keep_alive' , kwargs .get ('tcp_keepalive' , False ))
159
- socket_options .keep_alive_timeout_secs = kwargs .get (
160
- 'tcp_keep_alive_timeout_secs' , kwargs .get (
161
- 'tcp_keepalive_timeout_secs' , 0 ))
162
- socket_options .keep_alive_interval_secs = kwargs .get (
163
- 'tcp_keep_alive_interval_secs' , kwargs .get (
164
- 'tcp_keepalive_interval_secs' , 0 ))
165
- socket_options .keep_alive_max_probes = kwargs .get (
166
- 'tcp_keep_alive_max_probes' , kwargs .get (
167
- 'tcp_keepalive_max_probes' , 0 ))
168
-
169
- username = kwargs .get ('username' , '' )
170
- if kwargs .get ('enable_metrics_collection' , True ):
171
+ socket_options .keep_alive = \
172
+ _get (kwargs , 'tcp_keep_alive' , _get (kwargs , 'tcp_keepalive' , False ))
173
+
174
+ socket_options .keep_alive_timeout_secs = \
175
+ _get (kwargs , 'tcp_keep_alive_timeout_secs' , _get (kwargs , 'tcp_keepalive_timeout_secs' , 0 ))
176
+
177
+ socket_options .keep_alive_interval_secs = \
178
+ _get (kwargs , 'tcp_keep_alive_interval_secs' , _get (kwargs , 'tcp_keepalive_interval_secs' , 0 ))
179
+
180
+ socket_options .keep_alive_max_probes = \
181
+ _get (kwargs , 'tcp_keep_alive_max_probes' , _get (kwargs , 'tcp_keepalive_max_probes' , 0 ))
182
+
183
+ username = _get (kwargs , 'username' , '' )
184
+ if _get (kwargs , 'enable_metrics_collection' , True ):
171
185
username += _get_metrics_str ()
172
186
173
- client_bootstrap = kwargs . get ( 'client_bootstrap' )
187
+ client_bootstrap = _get ( kwargs , 'client_bootstrap' )
174
188
tls_ctx = awscrt .io .ClientTlsContext (tls_ctx_options )
175
189
mqtt_client = awscrt .mqtt .Client (client_bootstrap , tls_ctx )
176
190
177
191
return awscrt .mqtt .Connection (
178
192
client = mqtt_client ,
179
- on_connection_interrupted = kwargs . get ( 'on_connection_interrupted' ),
180
- on_connection_resumed = kwargs . get ( 'on_connection_resumed' ),
181
- client_id = kwargs . get ( 'client_id' ),
182
- host_name = kwargs . get ( 'endpoint' ),
193
+ on_connection_interrupted = _get ( kwargs , 'on_connection_interrupted' ),
194
+ on_connection_resumed = _get ( kwargs , 'on_connection_resumed' ),
195
+ client_id = _get ( kwargs , 'client_id' ),
196
+ host_name = _get ( kwargs , 'endpoint' ),
183
197
port = port ,
184
- clean_session = kwargs . get ( 'clean_session' , False ),
185
- reconnect_min_timeout_secs = kwargs . get ( 'reconnect_min_timeout_secs' , 5 ),
186
- reconnect_max_timeout_secs = kwargs . get ( 'reconnect_max_timeout_secs' , 60 ),
187
- keep_alive_secs = kwargs . get ( 'keep_alive_secs' , 1200 ),
188
- ping_timeout_ms = kwargs . get ( 'ping_timeout_ms' , 3000 ),
189
- protocol_operation_timeout_ms = kwargs . get ( 'protocol_operation_timeout_ms' , 0 ),
190
- will = kwargs . get ( 'will' ),
198
+ clean_session = _get ( kwargs , 'clean_session' , False ),
199
+ reconnect_min_timeout_secs = _get ( kwargs , 'reconnect_min_timeout_secs' , 5 ),
200
+ reconnect_max_timeout_secs = _get ( kwargs , 'reconnect_max_timeout_secs' , 60 ),
201
+ keep_alive_secs = _get ( kwargs , 'keep_alive_secs' , 1200 ),
202
+ ping_timeout_ms = _get ( kwargs , 'ping_timeout_ms' , 3000 ),
203
+ protocol_operation_timeout_ms = _get ( kwargs , 'protocol_operation_timeout_ms' , 0 ),
204
+ will = _get ( kwargs , 'will' ),
191
205
username = username ,
192
- password = kwargs . get ( 'password' ),
206
+ password = _get ( kwargs , 'password' ),
193
207
socket_options = socket_options ,
194
208
use_websockets = use_websockets ,
195
209
websocket_handshake_transform = websocket_handshake_transform ,
0 commit comments