Skip to content

Commit

Permalink
Impove error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Nov 6, 2016
1 parent 636b61e commit 76f7885
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pyqiwi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
def get_version():
return '.'.join(map(str, VERSION))

VERSION = (0, 1, 0)
VERSION = (0, 1, 1)
__version__ = get_version()
17 changes: 10 additions & 7 deletions pyqiwi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@


class QiwiError(Exception):
def __init__(self, code):
super(QiwiError, self).__init__('Result code: %s' % code)
def __init__(self, code, description=''):
super(QiwiError, self).__init__(
'Result code: %s (%s)' % (code, description))
self.code = code


Expand Down Expand Up @@ -54,6 +55,10 @@ def _urlencode(self, params):
# type: (dict) -> str
return urllib.urlencode({k: v for k, v in params.items() if v})

def _make_auth(self, username, password):
# type: (str, str) -> str
return 'Basic %s' % base64.b64encode('%s:%s' % (username, password))

def _make_signature(self, data):
# type: (dict) -> str
joined = u'|'.join(data[key] for key in sorted(data.keys()))
Expand All @@ -65,8 +70,7 @@ def _request(self, url, data=None, method='GET'):
# type: (str, dict, str) -> dict
request = urllib2.Request(url, headers={
'Accept': 'application/json',
'Authorization': 'Basic %s' % base64.b64encode(
'%s:%s' % (self.api_id, self.api_password))
'Authorization': self._make_auth(self.api_id, self.api_password),
})
if data:
request.add_data(self._urlencode(data))
Expand All @@ -80,7 +84,7 @@ def _request(self, url, data=None, method='GET'):

response = json.load(response)['response']
if response['result_code'] > 0:
raise QiwiError(response['result_code'])
raise QiwiError(response['result_code'], response.get('description', ''))
else:
return response

Expand Down Expand Up @@ -190,8 +194,7 @@ def check_auth(self, auth):
:param auth: HTTP Authorization header value
:return:
"""
return auth.replace('Basic ', '') == base64.b64encode('%s:%s' % (
self.shop_id, self.notification_password))
return self._make_auth(self.shop_id, self.notification_password) == auth

def check_signature(self, signature, data):
# type: (str, dict) -> bool
Expand Down

0 comments on commit 76f7885

Please sign in to comment.