Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Commit

Permalink
Add some error checking to help diagnose when response fails json par…
Browse files Browse the repository at this point in the history
…sing. Send a browser like User-Agent to prevent getting blocked by CloudFlare.
  • Loading branch information
omwah committed Mar 1, 2014
1 parent 26dae28 commit 6204401
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion meetup_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ def http_response(self, request, response):
try:
return HTTPErrorProcessor.http_response(self, request, response)
except HTTPError, e:
error_json = parse_json(e.read())
data = e.read()

try:
error_json = parse_json(data)
except ValueError:
logging.debug('Value error when trying to parse JSON from response data:\n%s' % response)
raise

if e.code == 401:
raise UnauthorizedError(error_json)
elif e.code in ( 400, 500 ):
Expand All @@ -73,6 +80,10 @@ def http_response(self, request, response):

class Meetup(object):
opener = build_opener(MeetupHTTPErrorProcessor)
# Act like a real browser or else CloudFlare protection
# blocks the request as banned
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]

def __init__(self, api_key):
"""Initializes a new session with an api key that will be added
to subsequent api calls"""
Expand Down

0 comments on commit 6204401

Please sign in to comment.