示例#1
0
    def test_configuration(self):
        Configuration.configure(account_id='test_account', secret_key='test_key', timeout=1000, max_attempts=2)
        configuration = Configuration.instantiate()

        self.assertEqual(configuration.account_id, 'test_account')
        self.assertEqual(configuration.secret_key, 'test_key')
        self.assertEqual(configuration.timeout, 1000)
        self.assertEqual(configuration.max_attempts, 2)
示例#2
0
 def setUp(self):
     Configuration.configure(
         account_id="test_account_id",
         secret_key="test_secret_key",
         timeout=1,
     )
     Configuration.agent_framework = Version(
         "Yandex.Money.Framework", "0.0.1"
     )
     Configuration.agent_cms = Version("Yandex.Money.Cms", "0.0.2")
     Configuration.agent_module = Version("Yandex.Money.Module", "0.0.3")
     self.client = ApiClient()
     self.client.endpoint = TARPIT
示例#3
0
 def __init__(self, shopid, secretkey):
     self.configuration = Configuration.instantiate()
     self.shop_id = shopid
     self.shop_password = secretkey
     self.auth_token = self.configuration.auth_token
     self.timeout = self.configuration.timeout
     self.max_attempts = self.configuration.max_attempts
示例#4
0
    def __init__(self):
        self.configuration = Configuration.instantiate()
        self.shop_id = self.configuration.account_id
        self.shop_password = self.configuration.secret_key
        self.auth_token = self.configuration.auth_token
        self.timeout = self.configuration.timeout
        self.max_attempts = self.configuration.max_attempts

        self.user_agent = UserAgent()
        if self.configuration.agent_framework:
            self.user_agent.framework = self.configuration.agent_framework
        if self.configuration.agent_cms:
            self.user_agent.cms = self.configuration.agent_cms
        if self.configuration.agent_module:
            self.user_agent.module = self.configuration.agent_module
示例#5
0
 def setUp(self):
     Configuration.configure(account_id='test_account_id', secret_key='test_secret_key')
示例#6
0
class ApiClient(object):
    endpoint = Configuration.api_endpoint()

    def __init__(self, shopid, secretkey):
        self.configuration = Configuration.instantiate()
        self.shop_id = shopid
        self.shop_password = secretkey
        self.auth_token = self.configuration.auth_token
        self.timeout = self.configuration.timeout
        self.max_attempts = self.configuration.max_attempts

    def request(self,
                method="",
                path="",
                query_params=None,
                headers=None,
                body=None):
        if isinstance(body, RequestObject):
            body.validate()
            body = dict(body)

        request_headers = self.prepare_request_headers(headers)
        raw_response = self.execute(body, method, path, query_params,
                                    request_headers)

        if raw_response.status_code != 200:
            self.__handle_error(raw_response)

        return raw_response.json()

    def execute(self, body, method, path, query_params, request_headers):
        session = self.get_session()
        raw_response = session.request(method,
                                       self.endpoint + path,
                                       params=query_params,
                                       headers=request_headers,
                                       json=body)
        return raw_response

    def get_session(self):
        session = requests.Session()
        retries = Retry(total=self.max_attempts,
                        backoff_factor=self.timeout / 1000,
                        method_whitelist=['POST'],
                        status_forcelist=[202])
        session.mount('https://', HTTPAdapter(max_retries=retries))
        return session

    def prepare_request_headers(self, headers):
        request_headers = {'Content-type': 'application/json'}
        if self.auth_token is not None:
            auth_headers = {"Authorization": "Bearer " + self.auth_token}
        else:
            auth_headers = {
                "Authorization": _basic_auth_str(self.shop_id,
                                                 self.shop_password)
            }

        request_headers.update(auth_headers)

        if isinstance(headers, dict):
            request_headers.update(headers)
        return request_headers

    def __handle_error(self, raw_response):
        http_code = raw_response.status_code
        if http_code == BadRequestError.HTTP_CODE:
            raise BadRequestError(raw_response.json())
        elif http_code == ForbiddenError.HTTP_CODE:
            raise ForbiddenError(raw_response.json())
        elif http_code == NotFoundError.HTTP_CODE:
            raise NotFoundError(raw_response.json())
        elif http_code == TooManyRequestsError.HTTP_CODE:
            raise TooManyRequestsError(raw_response.json())
        elif http_code == UnauthorizedError.HTTP_CODE:
            raise UnauthorizedError(raw_response.json())
        elif http_code == ResponseProcessingError.HTTP_CODE:
            raise ResponseProcessingError(raw_response.json())
        else:
            raise ApiError(raw_response.text)
示例#7
0
 def test_empty_credentials(self):
     with self.assertRaises(ConfigurationError):
         Configuration.configure(account_id=None, secret_key=None)
         configuration = Configuration.instantiate()
 def __init__(self):
     self.configuration = Configuration.instantiate()
     self.shop_id = self.configuration.account_id
     self.shop_password = self.configuration.secret_key
     self.timeout = self.configuration.timeout
     self.max_attempts = self.configuration.max_attempts
 def setUp(self):
     Configuration.configure(account_id='test_account_id', secret_key='test_secret_key')
     Configuration.agent_framework = Version('Yandex.Money.Framework', '0.0.1')
     Configuration.agent_cms = Version('Yandex.Money.Cms', '0.0.2')
     Configuration.agent_module = Version('Yandex.Money.Module', '0.0.3')