コード例 #1
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
コード例 #2
0
 def create_helper_builder(secret_key_store):
     """
     Creates a WebhooksHelperBuilder that will use the given SecretKeyStore.
     """
     return WebhooksHelperBuilder().with_marshaller(
         DefaultMarshaller.INSTANCE()).with_secret_key_store(
         secret_key_store)
コード例 #3
0
    def __init__(self,
                 integrator,
                 shopping_cart_extension=None,
                 additional_request_headers=()):
        MetaDataProvider.__validate_additional_request_headers(
            additional_request_headers)

        for i in additional_request_headers:
            i.name = re.sub(r'\r?\n(?:(?![\r\n])\s)*', " ", i.name)
            i.name = i.name.strip()
            i.value = re.sub(r'\r?\n(?:(?![\r\n])\s)*', " ", i.value)
            i.value = i.value.strip()

        server_meta_info = self.ServerMetaInfo()
        server_meta_info.platform_identifier = self._platform_identifier
        server_meta_info.sdk_identifier = self._sdk_identifier
        server_meta_info.sdk_creator = "OnlinePayments"
        server_meta_info.integrator = integrator
        server_meta_info.shopping_cart_extension = shopping_cart_extension

        server_meta_info_string = DefaultMarshaller.INSTANCE().marshal(
            server_meta_info)
        server_meta_info_header = RequestHeader(
            self.__SERVER_META_INFO_HEADER,
            b64encode(server_meta_info_string.encode('utf-8')))
        if not additional_request_headers:
            self.__meta_data_headers = tuple([server_meta_info_header])
        else:
            request_headers = [server_meta_info_header]
            request_headers.extend(additional_request_headers)
            self.__meta_data_headers = tuple(request_headers)
コード例 #4
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)
コード例 #5
0
    def test_create_communicator(self):
        """Tests that the factory is correctly able to create a communicator"""
        communicator = Factory.create_communicator_from_file(
            PROPERTIES_URI, API_KEY_ID, SECRET_API_KEY)

        self.assertIs(communicator.marshaller, DefaultMarshaller.INSTANCE())

        connection = communicator.connection
        self.assertIsInstance(connection, DefaultConnection)
        DefaultConnectionTest.assertConnection(self, connection, None, None,
                                               100, None)

        authenticator = communicator.authenticator
        self.assertIsInstance(authenticator, DefaultAuthenticator)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            self.assertEqual(
                AuthorizationType.V1HMAC,
                authenticator._DefaultAuthenticator__authorization_type)
            self.assertEqual(API_KEY_ID,
                             authenticator._DefaultAuthenticator__api_id_key)
            self.assertEqual(
                SECRET_API_KEY,
                authenticator._DefaultAuthenticator__secret_api_key)

        meta_data_provider = communicator.meta_data_provider
        self.assertIsInstance(meta_data_provider, MetaDataProvider)
        request_headers = meta_data_provider.meta_data_headers
        self.assertEqual(1, len(request_headers))
        self.assertEqual("X-GCS-ServerMetaInfo", request_headers[0].name)
コード例 #6
0
    def assertServerMetaInfo(self,
                             meta_data_provider,
                             integrator,
                             shopping_cart_extension=None,
                             request_header=None):
        """Assert that checks that the request_header is the default header "X-GCS-ServerMetaInfo",
        that the server_meta_data_info of the meta_data_provider is correct
        and that the shopping cart extension is consistent with the extension stored in meta_data_provider
        """
        self.assertEqual("X-GCS-ServerMetaInfo", request_header.name)
        self.assertIsNotNone(request_header.value)

        # server_meta_info is stored in json format and encoded using utf-8 and base64 encoding, decode it
        server_meta_info_json = base64.b64decode(
            request_header.value).decode('utf-8')
        server_meta_info = DefaultMarshaller.INSTANCE().unmarshal(
            server_meta_info_json, MetaDataProvider.ServerMetaInfo)
        self.assertEqual(meta_data_provider._platform_identifier,
                         server_meta_info.platform_identifier)
        self.assertEqual(meta_data_provider._sdk_identifier,
                         server_meta_info.sdk_identifier)
        self.assertEqual("OnlinePayments", server_meta_info.sdk_creator)
        self.assertEqual(integrator, server_meta_info.integrator)
        if shopping_cart_extension is None:
            self.assertIsNone(server_meta_info.shopping_cart_extension)
        else:
            self.assertEqual(shopping_cart_extension.creator,
                             server_meta_info.shopping_cart_extension.creator)
            self.assertEqual(shopping_cart_extension.name,
                             server_meta_info.shopping_cart_extension.name)
            self.assertEqual(shopping_cart_extension.version,
                             server_meta_info.shopping_cart_extension.version)
            self.assertEqual(
                shopping_cart_extension.extension_id,
                server_meta_info.shopping_cart_extension.extension_id)
コード例 #7
0
 def create_client_from_file(configuration_file_name,
                             api_key_id,
                             secret_api_key,
                             meta_data_provider=None,
                             connection=None,
                             authenticator=None,
                             marshaller=DefaultMarshaller.INSTANCE()):
     """
     Creates a Client based on the configuration values in configuration_file_name, api_key_id and secret_api_key.
     """
     communicator = Factory.create_communicator_from_file(
         configuration_file_name, api_key_id, secret_api_key,
         meta_data_provider, connection, authenticator, marshaller)
     return Client(communicator)
コード例 #8
0
    def test_with_client_meta_info(self):
        """Tests if the function withClientMetaInfo alters a client when it needs to and does nothing if not required"""

        client1 = Factory.create_client_from_file(PROPERTIES_URI, API_KEY_ID,
                                                  SECRET_API_KEY)
        client2 = client1.with_client_meta_info(None)
        client_meta_info = DefaultMarshaller.INSTANCE().marshal(
            {"test": "test"})
        client3 = client1.with_client_meta_info(client_meta_info)
        client4 = client3.with_client_meta_info(client_meta_info)
        client5 = client3.with_client_meta_info(None)

        self.assertIsNone(client1._client_headers)
        self.assertIs(client1, client2)
        self.assertIsNot(client1, client3)
        self.assertClientHeaders(client3, client_meta_info)
        self.assertIs(client3, client4)
        self.assertIsNot(client3, client5)
        self.assertIsNone(client5._client_headers)
コード例 #9
0
    def test_unmarshal_extended_token_response(self):
        """
        Tests if the marshaller is able to marshal an object that inherits from token_response
        in such a way that it can be interpreted as a token_response
        """
        card = TokenCard()
        card.alias = "12345"
        card.data = TokenCardData()

        token_response = TokenResponseWithExtraField()
        token_response.card = card
        token_response.extraField = "a random string"

        marshaller = DefaultMarshaller.INSTANCE()
        # Marshal the extended token response and unmarshal as a regular token response
        json = marshaller.marshal(token_response)
        unmarshalled = marshaller.unmarshal(json, TokenResponse)

        self.assertIsNotNone(unmarshalled.card)
        self.assertEqual("12345", unmarshalled.card.alias)
        self.assertIsNotNone(unmarshalled.card.data)
コード例 #10
0
    def test_unmarshal_with_extra_fields(self):
        """Tests if the marshaller is able to marshal an object and unmarshal it as an instance of a parent class"""
        dummy_object = JsonDummyExtended()
        mini_dummy = JsonMiniDummy()
        mini_mini_dummy = JsonMiniMiniDummy()
        mini_mini_dummy.foo = "hiddenfoo"
        mini_dummy.foo = mini_mini_dummy
        dummy_object.foo = mini_dummy
        dummy_object.bar = True
        dummy_object.boo = 0o1
        dummy_object.far = "close"
        dummy_object.extra_field = "something else"
        marshaller = DefaultMarshaller.INSTANCE()

        json = marshaller.marshal(dummy_object)
        unmarshalled = marshaller.unmarshal(json, JsonDummy)

        self.assertEqual(True, unmarshalled.bar)
        self.assertEqual(0o1, unmarshalled.boo)
        self.assertEqual("close", unmarshalled.far)
        self.assertEqual("hiddenfoo", unmarshalled.foo.foo.foo)
コード例 #11
0
 def __create_helper(self, marshaller=DefaultMarshaller.INSTANCE()):
     return WebhooksHelper(marshaller, InMemorySecretKeyStore.INSTANCE())
コード例 #12
0
 def test_create_helper(self):
     helper = Webhooks.create_helper(InMemorySecretKeyStore.INSTANCE())
     self.assertIs(DefaultMarshaller.INSTANCE(), helper.marshaller)
     self.assertIs(InMemorySecretKeyStore.INSTANCE(),
                   helper.secret_key_store)