def log(target): if hasattr(target, 'im_class'): logger.debug('Calling `%s#%s` in a new thread', target.im_class.__name__, target.__name__) else: logger.debug('Initializing `%s` in a new thread', target.__name__)
def get_with_etag_cache(self, url): headers = {} cached_response = self.cache.get(url, None) if cached_response is not None: etag = self.get_etag(cached_response) headers['If-None-Match'] = etag logger.debug('Headers: %s', headers) response = requests.get(url, headers=headers, auth=NullAuth()) self.log_header(response, 'x-ratelimit-limit') self.log_header(response, 'x-ratelimit-remaining') if response.status_code == 304: logger.info('Using cached response for {}'.format(self.url)) response = cached_response response.raise_for_status() return response
def log_header(self, response, key): if key in response.headers: logger.debug('%s: %s', key, response.headers[key])
def get(self): logger.debug('Requesting {}'.format(self.url)) return self.cache_response(self.url, self.get_with_etag_cache(self.url))