(self)
| 33 | self._get_token() |
| 34 | |
| 35 | def download(self): |
| 36 | headers = self._add_auth_header({'Content-Type': 'application/json'}) |
| 37 | url = 'https://platform.opentable.com/sync/listings' |
| 38 | |
| 39 | with open(self.opentable_filename, 'w') as f: |
| 40 | offset = 0 |
| 41 | while True: |
| 42 | request = urllib2.Request(url + '?offset={}'.format(offset), headers=headers) |
| 43 | logging.debug('Fetching data with headers %s from %s', |
| 44 | str(headers), request.get_full_url()) |
| 45 | resp = urllib2.urlopen(request) |
| 46 | # TODO(mgsergio): Handle exceptions |
| 47 | data = json.loads(resp.read()) |
| 48 | for rest in data['items']: |
| 49 | print(json.dumps(rest), file=f) |
| 50 | |
| 51 | total_items = int(data['total_items']) |
| 52 | offset = int(data['offset']) |
| 53 | items_count = len(data['items']) |
| 54 | |
| 55 | if total_items <= offset + items_count: |
| 56 | break |
| 57 | |
| 58 | offset += items_count |
| 59 | |
| 60 | def _get_token(self): |
| 61 | url = 'https://oauth.opentable.com/api/v2/oauth/token?grant_type=client_credentials' |
no test coverage detected