def discover(self, thing_name): discovery = dict(future=Future(), response_body=bytearray()) def on_incoming_body(http_stream, chunk, **kwargs): discovery['response_body'].extend(chunk) def on_request_complete(completion_future): try: response_code = completion_future.result() if response_code == 200: payload_str = discovery['response_body'].decode('utf-8') discover_res = DiscoverResponse.from_payload( json.loads(payload_str)) discovery['future'].set_result(discover_res) else: discovery['future'].set_exception( DiscoveryException( 'Error during discover call: response_code={}'. format(response_code), response_code)) except Exception as e: discovery['future'].set_exception(e) def on_connection_completed(conn_future): try: connection = conn_future.result() headers = HttpHeaders() headers.add('host', self._gg_server_name) request = HttpRequest( method='GET', path='/greengrass/discover/thing/{}'.format(thing_name), headers=headers) http_stream = connection.request(request=request, on_body=on_incoming_body) http_stream.activate() http_stream.completion_future.add_done_callback( on_request_complete) except Exception as e: discovery['future'].set_exception(e) connect_future = HttpClientConnection.new( host_name=self._gg_server_name, port=self.port, socket_options=self._socket_options, tls_connection_options=self._tls_connection_options, bootstrap=self._bootstrap) connect_future.add_done_callback(on_connection_completed) return discovery['future']
def _establish_http_connection(self, test_type, uri, proxy_options): event_loop_group = EventLoopGroup() host_resolver = DefaultHostResolver(event_loop_group) bootstrap = ClientBootstrap(event_loop_group, host_resolver) connection_future = HttpClientConnection.new( host_name=uri, port=ProxyTestConfiguration.get_port_from_test_type(test_type), bootstrap=bootstrap, tls_connection_options=ProxyTestConfiguration. get_tls_connection_options_for_test(test_type, uri), proxy_options=proxy_options) return connection_future.result(TIMEOUT)
def _new_h2_client_connection(self, url): event_loop_group = EventLoopGroup() host_resolver = DefaultHostResolver(event_loop_group) bootstrap = ClientBootstrap(event_loop_group, host_resolver) port = 443 scheme = 'https' tls_ctx_options = TlsContextOptions() tls_ctx = ClientTlsContext(tls_ctx_options) tls_conn_opt = tls_ctx.new_connection_options() tls_conn_opt.set_server_name(url.hostname) tls_conn_opt.set_alpn_list(["h2"]) connection_future = HttpClientConnection.new(host_name=url.hostname, port=port, bootstrap=bootstrap, tls_connection_options=tls_conn_opt) return connection_future.result(self.timeout)
def _new_client_connection(self, secure, proxy_options=None): if secure: tls_ctx_opt = TlsContextOptions() tls_ctx_opt.override_default_trust_store_from_path(None, 'test/resources/ca.crt') tls_ctx = ClientTlsContext(tls_ctx_opt) tls_conn_opt = tls_ctx.new_connection_options() tls_conn_opt.set_server_name(self.hostname) else: tls_conn_opt = None event_loop_group = EventLoopGroup() host_resolver = DefaultHostResolver(event_loop_group) bootstrap = ClientBootstrap(event_loop_group, host_resolver) connection_future = HttpClientConnection.new(host_name=self.hostname, port=self.port, bootstrap=bootstrap, tls_connection_options=tls_conn_opt, proxy_options=proxy_options) return connection_future.result(self.timeout)
def _new_client_connection(self, secure, proxy_options=None): if secure: tls_ctx_opt = TlsContextOptions() tls_ctx_opt.verify_peer = False tls_ctx = ClientTlsContext(tls_ctx_opt) tls_conn_opt = tls_ctx.new_connection_options() tls_conn_opt.set_server_name(self.hostname) else: tls_conn_opt = None event_loop_group = EventLoopGroup() host_resolver = DefaultHostResolver(event_loop_group) bootstrap = ClientBootstrap(event_loop_group, host_resolver) connection_future = HttpClientConnection.new( host_name=self.hostname, port=self.port, bootstrap=bootstrap, tls_connection_options=tls_conn_opt, proxy_options=proxy_options) return connection_future.result(self.timeout)