Ejemplo n.º 1
0
 def create_communicator_from_configuration(
     communicator_configuration,
     meta_data_provider=None,
     connection=None,
     authenticator=None,
     marshaller=DefaultMarshaller.INSTANCE()):
     """
     Creates a Communicator based on the configuration stored in the CommunicatorConfiguration argument
     """
     # If an authenticator is not given, api_key_id and secret_api_key are
     # used to create a DefaultAuthenticator used to create the communicator.
     if connection is None:
         connection = DefaultConnection(
             connect_timeout=communicator_configuration.connect_timeout,
             socket_timeout=communicator_configuration.socket_timeout,
             max_connections=communicator_configuration.max_connections,
             proxy_configuration=communicator_configuration.
             proxy_configuration)
     if authenticator is None:
         authenticator = DefaultAuthenticator(
             api_key_id=communicator_configuration.api_key_id,
             secret_api_key=communicator_configuration.secret_api_key,
             authorization_type=communicator_configuration.
             authorization_type)
     if meta_data_provider is None:
         meta_data_provider = MetaDataProvider(
             integrator=communicator_configuration.integrator,
             shopping_cart_extension=communicator_configuration.
             shopping_cart_extension)
     return Communicator(
         api_endpoint=communicator_configuration.api_endpoint,
         authenticator=authenticator,
         connection=connection,
         meta_data_provider=meta_data_provider,
         marshaller=marshaller)
Ejemplo n.º 2
0
 def __init__(self,
              api_endpoint,
              authenticator,
              meta_data_provider,
              connection=DefaultConnection(),
              marshaller=DefaultMarshaller.INSTANCE()):
     if api_endpoint is None:
         raise ValueError("api_endpoint is required")
     if isinstance(api_endpoint, str):
         api_endpoint = urlparse(api_endpoint)
     if not api_endpoint.scheme.lower() in ["http", "https"
                                            ] or not api_endpoint.netloc:
         raise ValueError("invalid api_endpoint: " + api_endpoint)
     if api_endpoint.path:
         raise ValueError("api_endpoint should not contain a path")
     if api_endpoint.username is not None or api_endpoint.query or api_endpoint.fragment:
         raise ValueError(
             "api_endpoint should not contain user info, query or fragment")
     if authenticator is None:
         raise ValueError("authenticator is required")
     if meta_data_provider is None:
         raise ValueError("meta_data_provider is required")
     if connection is None:
         raise ValueError("connection is required")
     if marshaller is None:
         raise ValueError("marshaller is required")
     self.__api_endpoint = api_endpoint
     self.__authenticator = authenticator
     self.__connection = connection
     self.__marshaller = marshaller
     self.__meta_data_provider = meta_data_provider
Ejemplo n.º 3
0
    def test_construct_with_max_connections_without_proxy(self):
        """Tests construction of a DefaultConnection with a different amount of max connections and no proxy"""
        connection = DefaultConnection(CONNECT_TIMEOUT, SOCKET_TIMEOUT, MAX_CONNECTIONS)

        self.assertTimeouts(self, connection, CONNECT_TIMEOUT, SOCKET_TIMEOUT)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.assertMaxConnections(self, connection, MAX_CONNECTIONS, None)
            self.assertNoProxy(self, connection)
Ejemplo n.º 4
0
    def test_construct_without_proxy(self):
        """Tests construction of a DefaultConnection without using a proxy"""
        connection = DefaultConnection(CONNECT_TIMEOUT, SOCKET_TIMEOUT)

        self.assertTimeouts(self, connection, CONNECT_TIMEOUT, SOCKET_TIMEOUT)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.assertMaxConnections(self, connection, CommunicatorConfiguration.DEFAULT_MAX_CONNECTIONS, None)
            self.assertNoProxy(self, connection)
Ejemplo n.º 5
0
    def test_construct_with_proxy_with_authentication(self):
        """Tests construction of a DefaultConnection with an authenticated proxy"""
        proxy_config = ProxyConfiguration.from_uri("http://test-proxy", "test-username", "test-password")

        connection = DefaultConnection(CONNECT_TIMEOUT, SOCKET_TIMEOUT, proxy_configuration=proxy_config)

        self.assertTimeouts(self, connection, CONNECT_TIMEOUT, SOCKET_TIMEOUT)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.assertMaxConnections(self, connection,
                                      CommunicatorConfiguration.DEFAULT_MAX_CONNECTIONS, proxy_config)
            self.assertProxy(self, connection, proxy_config)
Ejemplo n.º 6
0
def create_client(
        http_host,
        connect_timeout=0.500,
        socket_timeout=0.500,
        max_connections=EndpointConfiguration.DEFAULT_MAX_CONNECTIONS):
    connection = DefaultConnection(connect_timeout, socket_timeout,
                                   max_connections)
    authenticator = DefaultAuthenticator("apiKey", "secret")
    meta_data_provider = MetaDataProvider("the Online Payments")
    communicator = Communicator(api_endpoint=http_host,
                                authenticator=authenticator,
                                meta_data_provider=meta_data_provider,
                                connection=connection)
    return Factory.create_client_from_communicator(communicator)
Ejemplo n.º 7
0
    def test_construct_with_max_connections_with_proxy(self):
        """Tests construction of a DefaultConnection
        with a different amount of max connections and an unauthenticated proxy
        """
        proxy_config = ProxyConfiguration.from_uri("http://test-proxy")

        connection = DefaultConnection(CONNECT_TIMEOUT, SOCKET_TIMEOUT,
                                       MAX_CONNECTIONS, proxy_configuration=proxy_config)

        self.assertTimeouts(self, connection, CONNECT_TIMEOUT, SOCKET_TIMEOUT)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.assertMaxConnections(self, connection, MAX_CONNECTIONS, proxy_config)
            self.assertProxy(self, connection, proxy_config)