handle errors
This commit is contained in:
parent
abb9b3dfc3
commit
4336da6224
1 changed files with 15 additions and 4 deletions
|
|
@ -34,6 +34,9 @@ except ImportError:
|
||||||
__all__ = ['MediawikiBrowser']
|
__all__ = ['MediawikiBrowser']
|
||||||
|
|
||||||
|
|
||||||
|
class APIError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
# Browser
|
# Browser
|
||||||
class MediawikiBrowser(BaseBrowser):
|
class MediawikiBrowser(BaseBrowser):
|
||||||
ENCODING = 'utf-8'
|
ENCODING = 'utf-8'
|
||||||
|
|
@ -165,15 +168,23 @@ class MediawikiBrowser(BaseBrowser):
|
||||||
'''We don't need to change location, we're using the JSON API here.'''
|
'''We don't need to change location, we're using the JSON API here.'''
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def check_result(self, result):
|
||||||
|
if 'error' in result:
|
||||||
|
raise APIError('%s' % result['error']['info'])
|
||||||
|
|
||||||
def API_get(self, data):
|
def API_get(self, data):
|
||||||
'''Submit a GET request to the website
|
'''Submit a GET request to the website
|
||||||
The JSON data is parsed and returned as a dictionary'''
|
The JSON data is parsed and returned as a dictionary'''
|
||||||
data['format'] = 'json'
|
data['format'] = 'json'
|
||||||
return simplejson.loads(self.readurl(self.buildurl(self.apiurl, **data)), 'utf-8')
|
result = simplejson.loads(self.readurl(self.buildurl(self.apiurl, **data)), 'utf-8')
|
||||||
|
self.check_result(result)
|
||||||
|
return result
|
||||||
|
|
||||||
def API_post(self, data):
|
def API_post(self, data):
|
||||||
'''Submit a POST request to the website
|
'''Submit a POST request to the website
|
||||||
The JSON data is parsed and returned as a dictionary'''
|
The JSON data is parsed and returned as a dictionary'''
|
||||||
|
|
||||||
data['format'] = 'json'
|
data['format'] = 'json'
|
||||||
return simplejson.loads(self.readurl(self.apiurl, urllib.urlencode(data)), 'utf-8')
|
result = simplejson.loads(self.readurl(self.apiurl, urllib.urlencode(data)), 'utf-8')
|
||||||
|
self.check_result(result)
|
||||||
|
return result
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue