def test_jsonfetch_error_passes_through(self): self.expect_urlfetch('http://my/url', 'my error', status=408) self.mox.ReplayAll() try: util.jsonfetch('http://my/url') except exc.HTTPException, e: self.assertEquals(408, e.status_int)
def jsonfetch(url): """Wraps util.jsonfetch() and adds the access_token query param.""" parsed = list(urlparse.urlparse(url)) # query params are in index 4 # TODO: when this is on python 2.7, switch to urlparse.parse_qsl params = cgi.parse_qsl(parsed[4]) + [ ('access_token', appengine_config.GITHUB_ACCESS_TOKEN)] parsed[4] = urllib.urlencode(params) url = urlparse.urlunparse(parsed) return util.jsonfetch(url)
def jsonfetch(url, **kwargs): """Wraps util.jsonfetch(), signing with OAuth. """ auth = tweepy.OAuthHandler(appengine_config.TWITTER_APP_KEY, appengine_config.TWITTER_APP_SECRET) auth.set_access_token(appengine_config.TWITTER_ACCESS_TOKEN, appengine_config.TWITTER_ACCESS_TOKEN_SECRET) method = kwargs.get('method', 'GET') headers = kwargs.setdefault('headers', {}) parsed = urlparse.urlparse(url) url_without_query = urlparse.urlunparse(list(parsed[0:4]) + ['', '']) auth.apply_auth(url_without_query, method, headers, # TODO: switch to urlparse.parse_qsl after python27 runtime dict(cgi.parse_qsl(parsed.query))) logging.info('Signed with OAuth, populated Authorization header: %s', headers.get('Authorization')) return util.jsonfetch(url, **kwargs)
def test_jsonfetch(self): self.expect_urlfetch('http://my/url', '["x", "y"]', foo='bar') self.mox.ReplayAll() self.assertEquals(['x', 'y'], util.jsonfetch('http://my/url', foo='bar'))