def _paginated(self, endpoint, records=None, *, if_none_match=None, pages=None, **kwargs): if records is None: records = collections.OrderedDict() headers = {} if if_none_match is not None: headers['If-None-Match'] = utils.quote(if_none_match) if pages is None: pages = 1 if '_limit' in kwargs else float('inf') record_resp, headers = self.session.request( 'get', endpoint, headers=headers, params=kwargs) # Save the current records collection timestamp etag = headers.get('ETag', '').strip('"') self._records_timestamp[endpoint] = etag if record_resp: records_tuples = [(r['id'], r) for r in record_resp['data']] records.update(collections.OrderedDict(records_tuples)) if pages > 1 and 'next-page' in map(str.lower, headers.keys()): # Paginated wants a relative URL, but the returned one is # absolute. next_page = headers['Next-Page'] return self._paginated(next_page, records, if_none_match=if_none_match, pages=pages - 1) return list(records.values())
def _paginated_generator(self, endpoint, *, if_none_match=None, **kwargs): headers = {} if if_none_match is not None: headers["If-None-Match"] = utils.quote(if_none_match) record_resp, headers = self.session.request("get", endpoint, headers=headers, params=kwargs) if record_resp: yield record_resp if "next-page" in map(str.lower, headers.keys()): next_page = headers["Next-Page"] yield from self._paginated_generator(next_page, if_none_match=if_none_match)
def _paginated(self, endpoint, records=None, if_none_match=None, **kwargs): if records is None: records = collections.OrderedDict() headers = {} if if_none_match is not None: headers['If-None-Match'] = utils.quote(if_none_match) record_resp, headers = self.session.request( 'get', endpoint, headers=headers, params=kwargs) if record_resp: records_tuples = [(r['id'], r) for r in record_resp['data']] records.update(collections.OrderedDict(records_tuples)) if 'next-page' in map(str.lower, headers.keys()): # Paginated wants a relative URL, but the returned one is # absolute. next_page = headers['Next-Page'] return self._paginated(next_page, records, if_none_match=if_none_match) return records.values()
def _get_cache_headers(self, safe, data=None, if_match=None): has_data = data is not None and data.get('last_modified') if (if_match is None and has_data): if_match = data['last_modified'] if safe and if_match is not None: return {'If-Match': utils.quote(if_match)}
def test_quotes_can_take_integers(self): assert utils.quote(1234) == '"1234"'
def test_quote_strips_extra_quotes(self): assert utils.quote('"1234"') == '"1234"'
def _get_cache_headers(self, safe, data=None, if_match=None): has_data = data is not None and data.get("last_modified") if if_match is None and has_data: if_match = data["last_modified"] if safe and if_match is not None: return {"If-Match": utils.quote(if_match)}