Example #1
0
    def new(cls,
            host_name,
            port,
            bootstrap,
            socket_options=None,
            tls_connection_options=None,
            proxy_options=None):
        """
        Initiates a new connection to host_name and port using socket_options and tls_connection_options if supplied.
        if tls_connection_options is None, then the connection will be attempted over plain-text.

        Returns a future where the result is a new instance to HttpClientConnection, once the connection has completed
        and is ready for use.
        """
        assert isinstance(bootstrap, ClientBootstrap) or bootstrap is None
        assert isinstance_str(host_name)
        assert isinstance(port, int)
        assert isinstance(
            tls_connection_options,
            TlsConnectionOptions) or tls_connection_options is None
        assert isinstance(socket_options,
                          SocketOptions) or socket_options is None
        assert isinstance(proxy_options,
                          HttpProxyOptions) or proxy_options is None

        future = Future()
        try:
            if not socket_options:
                socket_options = SocketOptions()

            if not bootstrap:
                event_loop_group = EventLoopGroup(1)
                host_resolver = DefaultHostResolver(event_loop_group)
                bootstrap = ClientBootstrap(event_loop_group, host_resolver)

            connection = cls()
            connection._host_name = host_name
            connection._port = port

            def on_connection_setup(binding, error_code):
                if error_code == 0:
                    connection._binding = binding
                    future.set_result(connection)
                else:
                    future.set_exception(
                        awscrt.exceptions.from_code(error_code))

            # on_shutdown MUST NOT reference the connection itself, just the shutdown_future within it.
            # Otherwise we create a circular reference that prevents the connection from getting GC'd.
            shutdown_future = connection.shutdown_future

            def on_shutdown(error_code):
                if error_code:
                    shutdown_future.set_exception(
                        awscrt.exceptions.from_code(error_code))
                else:
                    shutdown_future.set_result(None)

            _awscrt.http_client_connection_new(bootstrap, on_connection_setup,
                                               on_shutdown, host_name, port,
                                               socket_options,
                                               tls_connection_options,
                                               proxy_options)

        except Exception as e:
            future.set_exception(e)

        return future
Example #2
0
    def new(cls,
            host_name,
            port,
            bootstrap,
            socket_options=None,
            tls_connection_options=None,
            proxy_options=None):
        """
        Asynchronously establish a new HttpClientConnection.

        Args:
            host_name (str): Connect to host.

            port (int): Connect to port.

            bootstrap (ClientBootstrap): Client bootstrap to use when initiating socket connection.

            socket_options (Optional[SocketOptions]): Optional socket options.
                If None is provided, then default options are used.

            tls_connection_options (Optional[TlsConnectionOptions]): Optional TLS
                connection options. If None is provided, then the connection will
                be attempted over plain-text.

            proxy_options (Optional[HttpProxyOptions]): Optional proxy options.
                If None is provided then a proxy is not used.

        Returns:
            concurrent.futures.Future: A Future which completes when connection succeeds or fails.
            If successful, the Future will contain a new :class:`HttpClientConnection`.
            Otherwise, it will contain an exception.
        """
        assert isinstance(bootstrap, ClientBootstrap) or bootstrap is None
        assert isinstance(host_name, str)
        assert isinstance(port, int)
        assert isinstance(
            tls_connection_options,
            TlsConnectionOptions) or tls_connection_options is None
        assert isinstance(socket_options,
                          SocketOptions) or socket_options is None
        assert isinstance(proxy_options,
                          HttpProxyOptions) or proxy_options is None

        future = Future()
        try:
            if not socket_options:
                socket_options = SocketOptions()

            if not bootstrap:
                event_loop_group = EventLoopGroup(1)
                host_resolver = DefaultHostResolver(event_loop_group)
                bootstrap = ClientBootstrap(event_loop_group, host_resolver)

            connection = cls()
            connection._host_name = host_name
            connection._port = port

            def on_connection_setup(binding, error_code, http_version):
                if error_code == 0:
                    connection._binding = binding
                    connection._version = HttpVersion(http_version)
                    future.set_result(connection)
                else:
                    future.set_exception(
                        awscrt.exceptions.from_code(error_code))

            # on_shutdown MUST NOT reference the connection itself, just the shutdown_future within it.
            # Otherwise we create a circular reference that prevents the connection from getting GC'd.
            shutdown_future = connection.shutdown_future

            def on_shutdown(error_code):
                if error_code:
                    shutdown_future.set_exception(
                        awscrt.exceptions.from_code(error_code))
                else:
                    shutdown_future.set_result(None)

            _awscrt.http_client_connection_new(bootstrap, on_connection_setup,
                                               on_shutdown, host_name, port,
                                               socket_options,
                                               tls_connection_options,
                                               proxy_options)

        except Exception as e:
            future.set_exception(e)

        return future