def fetch_json2xx(url, content='', method='GET', credentials=None, headers=None, multipart=False, ua='', timeout=50, caching=None): """Like `fetch2xx()` but JSON-decodes the returned content and returns only that.""" status, rheaders, rcontent = fetch2xx(url, content, method, credentials, headers, multipart, ua, timeout, caching) if not rheaders.get('content-type', '').startswith('application/json'): raise TypeError(u"Ungueltiger Content-Type %r: %r" % (rheaders.get('content-type', ''), rcontent)) return hujson2.loads(rcontent)
def fetch_json2xx(url, content='', method='GET', credentials=None, headers=None, multipart=False, ua='', timeout=50, caching=None): """Like `fetch2xx()` but JSON-decodes the returned content and returns only that.""" status, rheaders, rcontent = fetch2xx(url, content, method, credentials, headers, multipart, ua, timeout, caching) if not rheaders.get('content-type', '').startswith('application/json'): raise TypeError(u"Ungueltiger Content-Type %r: %r %r" % (rheaders.get('content-type', ''), rcontent, url)) return hujson2.loads(rcontent)
def parse_json(content): """Parse JSON response >>> parse_json('{"messages": [{"url": "http://example.com/q/45054/"}]}') [u'http://example.com/q/45054/'] """ result = json.loads(content) return [msg.get('url') for msg in result.get('messages', [])]
def decodingreturnhandler(status, rheaders, rcontent): """Closure to do the json decoding and then call the provided returnhandler""" if (status < 200) or (status >= 300): raise exceptions.WrongStatusCode(u"%s: Fehler: %r" % (status, rcontent)) # Warnig! httplib2 ist case sensitive for header field names. if not rheaders.get('Content-Type', '').startswith('application/json'): logging.debug("no valid content type: %r", rheaders) # There seems to be an interesting issue with the AppEngine Frontend Cache Servers: # When serving from the cache to an AppEngine client (date being rquested from an other # AppEngine Application) the content type header seems to get dropped completely. # So wo only check for a missmatched header but ignore empty ones. # So far this has been only observed with async requests. if rheaders.get('Content-Type', None) is not None: raise TypeError(u"%s: Ungueltiger Content-Type %r: %r" % (url, rheaders.get('Content-Type', ''), rcontent)) return returnhandler(hujson2.loads(rcontent))
def _get_json(self, url): """helper: requests the url as json""" response = self.app.get(url) return loads(response.body)