def _fetch(self, api_path, query_params={}, post_params={}, authenticated=True): url = 'https://www.google.com/reader/api/0/%s' % api_path if self._cache: cache_key = base.paths.url_to_file_name(url, query_params, post_params) cache_value = self._cache.get(cache_key) if cache_value: return cache_value def urlencode(params): def encode(s): return isinstance(s, unicode) and s.encode('utf-8') or s encoded_params = {} for key, value in params.items(): if isinstance(value, list): value = [encode(v) for v in value] else: value = encode(value) encoded_params[encode(key)] = value return urllib.urlencode(encoded_params, doseq=True) request_url = '%s?%s' % (url, urlencode(query_params)) url_fetcher = self._authenticated_url_fetcher if authenticated \ else self._direct_url_fetcher response_text = url_fetcher.fetch( request_url, post_data=urlencode(post_params) if post_params else None) if self._cache: self._cache.set(cache_key, response_text) return response_text
def _fetch(self, api_path, query_params={}, post_params={}, authenticated=True): url = 'https://www.google.com/reader/api/0/%s' % api_path if self._cache: cache_key = base.paths.url_to_file_name(url, query_params, post_params) cache_value = self._cache.get(cache_key) if cache_value: return cache_value def urlencode(params): def encode(s): return isinstance(s, unicode) and s.encode('utf-8') or s encoded_params = {} for key, value in params.items(): if isinstance(value, list): value = [encode(v) for v in value] else: value = encode(value) encoded_params[encode(key)] = value return urllib.urlencode(encoded_params, doseq=True) request_url = '%s?%s' % (url, urlencode(query_params)) url_fetcher = self._authenticated_url_fetcher if authenticated \ else self._direct_url_fetcher try: response_text = url_fetcher.fetch( request_url, post_data=urlencode(post_params) if post_params else None) except urllib2.HTTPError, e: if e.code >= 400 and e.code < 500: # Log 400s, since they're usually programmer error, and the response # indicates how to fix it. logging.error( 'HTTP status %d when requesting %s. Error response body:\n%s', e.code, request_url, e.read()) raise