def _obtain_new_token(self): """ Obtain new access token using the login-password credentials pair. If failed to obtain token, raise an exception. """ response_data = self._request('post', 'getToken', json={ 'login': settings.RECEIPTS_ATOL_LOGIN, 'pass': settings.RECEIPTS_ATOL_PASSWORD, }) error = response_data.get('error') if error: logger.error('fail obtained auth token due to %s', error['text'], extra={'data': error}) raise exceptions.AtolAuthTokenException() auth_token = response_data['token'] logger.info( 'successfully obtained fresh auth token "%s" for login "%s"', auth_token, settings.RECEIPTS_ATOL_LOGIN) return auth_token
def _request(self, method, endpoint, params=None, headers=None, json=None): params = params or {} headers = headers or {} headers.setdefault('Content-Type', 'application/json') url = '{base_url}/{endpoint}'.format(base_url=self.base_url.rstrip('/'), endpoint=endpoint) logger.info('about to %s %s with headers=%s, params=%s json=%s', method, url, headers, params, json) try: response = requests.request(method, url, params=params, json=json, headers=headers, timeout=self.request_timeout) except Exception as exc: logger.warning('failed to request %s %s with headers=%s, params=%s json=%s due to %s', method, url, headers, params, json, exc, exc_info=True, extra={'data': {'json': json, 'params': params}}) raise exceptions.AtolRequestException() # error codes other than 2xx, 400, 401 are considered unexpected and yield an exception if response.status_code not in (200, 201, 400, 401): try: json_response = response.json() except Exception: json_response = None logger.warning('request %s %s with headers=%s, params=%s json=%s failed with status code %s: %s', method, url, headers, params, json, response.status_code, json_response, extra={'data': {'json_request': json, 'response': response.content[:1000], 'json_response': json_response}}) raise exceptions.AtolRequestException() # 401 should be handled separately by the calling code if response.status_code == 401: logger.info('authentication failed for request %s %s with headers=%s, params=%s json=%s: %s', method, url, headers, params, json, response.content[:1000]) raise exceptions.AtolAuthTokenException() try: response_data = response.json() except Exception as exc: logger.warning('unable to parse json response %s due to %s', response.content[:1000], exc, exc_info=True, extra={'data': {'content': response.content}}) raise exceptions.AtolRequestException() if response_data.get('error'): logger.warning('received error response from atol url %s: %s', url, response_data['error'], extra={'data': {'json': json, 'params': params}}) raise exceptions.AtolClientRequestException(response=response, response_data=response_data, error_data=response_data['error']) return response_data
def _obtain_new_token(self): """ Obtain new access token using the login-password credentials pair. If failed to obtain token, raise an exception. """ response_data = self._request('post', 'getToken', json={ 'login': settings.RECEIPTS_ATOL_LOGIN, 'pass': settings.RECEIPTS_ATOL_PASSWORD, }) # all codes other than 0 (new token) and 1 (existing token) are considered errors if response_data.get('code') not in (0, 1): raise exceptions.AtolAuthTokenException() auth_token = response_data['token'] logger.info('successfully obtained fresh auth token "%s" for login "%s"', auth_token, settings.RECEIPTS_ATOL_LOGIN) return auth_token