|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Provides the PythonKC Meetup.com API client implementation. |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | + |
| 8 | +from pythonkc_meetups.exceptions import PythonKCMeetupsBadJson |
| 9 | +from pythonkc_meetups.exceptions import PythonKCMeetupsBadResponse |
| 10 | +from pythonkc_meetups.exceptions import PythonKCMeetupsMeetupDown |
| 11 | +from pythonkc_meetups.exceptions import PythonKCMeetupsNotJson |
| 12 | +from pythonkc_meetups.exceptions import PythonKCMeetupsRateLimitExceeded |
| 13 | +from pythonkc_meetups.parsers import parse_event |
| 14 | +import httplib2 |
| 15 | +import json |
| 16 | +import mimeparse |
| 17 | +import urllib |
| 18 | + |
| 19 | + |
| 20 | +EVENTS_URL = 'https://api.meetup.com/2/events.json' |
| 21 | +GROUP_URLNAME = 'pythonkc' |
| 22 | + |
| 23 | + |
| 24 | +class PythonKCMeetups(object): |
| 25 | + |
| 26 | + """ |
| 27 | + Retrieves information about PythonKC meetups. |
| 28 | +
|
| 29 | + """ |
| 30 | + |
| 31 | + def __init__(self, api_key, http_timeout=1, http_retries=2): |
| 32 | + """ |
| 33 | + Create a new instance. |
| 34 | +
|
| 35 | + Parameters |
| 36 | + ---------- |
| 37 | + api_key |
| 38 | + The Meetup.com API consumer key to make requests with. |
| 39 | + http_timeout |
| 40 | + Time, in seconds, to give HTTP requests to complete. |
| 41 | + http_retries |
| 42 | + The number of times to retry requests when it is appropriate to do |
| 43 | + so. |
| 44 | +
|
| 45 | + """ |
| 46 | + self._api_key = api_key |
| 47 | + self._http_timeout = http_timeout |
| 48 | + self._http_retries = http_retries |
| 49 | + |
| 50 | + def get_upcoming_events(self): |
| 51 | + """ |
| 52 | + Get the upcoming PythonKC meetup events. |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + List of ``pythonkc_meetups.types.MeetupEvent``, ordered by event time, |
| 57 | + ascending. |
| 58 | +
|
| 59 | + """ |
| 60 | + |
| 61 | + query = urllib.urlencode({'key': self._api_key, |
| 62 | + 'group_urlname': GROUP_URLNAME}) |
| 63 | + url = '{0}?{1}'.format(EVENTS_URL, query) |
| 64 | + data = self._http_get_json(url) |
| 65 | + events = data['results'] |
| 66 | + return [parse_event(event) for event in events] |
| 67 | + |
| 68 | + def _http_get_json(self, url): |
| 69 | + """ |
| 70 | + Make an HTTP GET request to the specified URL, check that it returned a |
| 71 | + JSON response, and returned the data parsed from that response. |
| 72 | +
|
| 73 | + Parameters |
| 74 | + ---------- |
| 75 | + url |
| 76 | + The URL to GET. |
| 77 | +
|
| 78 | + Returns |
| 79 | + ------- |
| 80 | + Dictionary of data parsed from a JSON HTTP response. |
| 81 | +
|
| 82 | + Exceptions |
| 83 | + ---------- |
| 84 | + * PythonKCMeetupsBadJson |
| 85 | + * PythonKCMeetupsNotJson |
| 86 | + * PythonKCMeetupsMeetupDown |
| 87 | + * PythonKCMeetupsRateLimitExceeded |
| 88 | + * PythonKCMeetupsBadResponse |
| 89 | +
|
| 90 | + """ |
| 91 | + response, content = self._http_get(url) |
| 92 | + |
| 93 | + content_type = response['content-type'] |
| 94 | + parsed_mimetype = mimeparse.parse_mime_type(content_type) |
| 95 | + if parsed_mimetype[1] not in ('json', 'javascript'): |
| 96 | + raise PythonKCMeetupsNotJson(content_type) |
| 97 | + |
| 98 | + try: |
| 99 | + return json.loads(content) |
| 100 | + except ValueError as e: |
| 101 | + raise PythonKCMeetupsBadJson(e) |
| 102 | + |
| 103 | + def _http_get(self, url): |
| 104 | + """ |
| 105 | + Make an HTTP GET request to the specified URL and return the response. |
| 106 | +
|
| 107 | + Retries |
| 108 | + ------- |
| 109 | + The constructor of this class takes an argument specifying the number |
| 110 | + of times to retry a GET. The statuses which are retried on are: 408, |
| 111 | + 500, 502, 503, and 504. |
| 112 | +
|
| 113 | + Returns |
| 114 | + ------- |
| 115 | + A tuple of response, content, where response is a dictionary of |
| 116 | + response metadata (headers), and content is the entity body returned. |
| 117 | +
|
| 118 | + Exceptions |
| 119 | + ---------- |
| 120 | + * PythonKCMeetupsMeetupDown |
| 121 | + * PythonKCMeetupsRateLimitExceeded |
| 122 | + * PythonKCMeetupsBadResponse |
| 123 | +
|
| 124 | + """ |
| 125 | + for try_number in range(self._http_retries + 1): |
| 126 | + client = httplib2.Http(timeout=self._http_timeout) |
| 127 | + response, content = client.request(url, 'GET') |
| 128 | + if response.status == 200: |
| 129 | + return response, content |
| 130 | + |
| 131 | + if (try_number >= self._http_retries or |
| 132 | + response.status not in (408, 500, 502, 503, 504)): |
| 133 | + |
| 134 | + if response.status >= 500: |
| 135 | + raise PythonKCMeetupsMeetupDown(response, content) |
| 136 | + if response.status == 400: |
| 137 | + try: |
| 138 | + data = json.loads(content) |
| 139 | + if data.get('code', None) == 'limit': |
| 140 | + raise PythonKCMeetupsRateLimitExceeded |
| 141 | + except: # Don't lose original error when JSON is bad |
| 142 | + pass |
| 143 | + raise PythonKCMeetupsBadResponse(response, content) |
0 commit comments