def test_urlencoded_post_querystring_multivalued(self): r = requests.post(httpbin('post'), params=dict(test=['foo','baz'])) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, httpbin('post?test=foo&test=baz')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), {}) # No form supplied self.assertEquals(rbody.get('data'), '')
def test_nonurlencoded_post_querystring(self): r = requests.post(httpbin('post'), params='fooaowpeuf') self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, httpbin('post?fooaowpeuf')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), {}) # No form supplied self.assertEquals(rbody.get('data'), '')
def load(text, format='json'): data = None if format == 'json': data = jsonload.loads(text) if format == 'yaml': data = yaml.load(text, Loader=Loader) assert data, format return bunchify(data)
def test_urlencoded_post_data(self): r = requests.post(httpbin('post'), data=dict(test='fooaowpeuf')) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, httpbin('post')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), dict(test='fooaowpeuf')) self.assertEquals(rbody.get('data'), '')
def test_urlencoded_post_query_multivalued_and_data(self): r = requests.post(httpbin('post'), params=dict(test=['foo','baz']), data=dict(test2="foobar",test3=['foo','baz'])) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, httpbin('post?test=foo&test=baz')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), dict(test2='foobar',test3='foo')) self.assertEquals(rbody.get('data'), '')
def user(username): user = None response = github_api('/users/%(username)s' % {'username': username}) if response.ok: user = json.loads(response.content) return user
def pull_requests(user, repository, state='closed'): pulls = [] response = github_api('/repos/%(user)s/%(repository)s/pulls' % {'user': user, 'repository': repository}, data={'state':state}) if response.ok: pulls = json.loads(response.content) return pulls
def organisation(org): organisation = None response = github_api('/orgs/%(organisation)s' % {'organisation': org}) if response.ok: organisation = json.loads(response.content) return organisation
def server_info(server_href, nickname=None): """ Detailed server information :param server_href: URL representing the server to query :param nickname: (optional) String representing the nickname of the server """ response = _request("%s.js" % _lookup_server(server_href, nickname), prepend_api_base=False) return json.loads(response.content)
def server_settings(server_href, nickname=None): """ Current server settings :param server_href: URL representing the server to query settngs from :param nickname: (optional) String representing the nickname of the server """ response = _request("%s/settings.js" % _lookup_server(server_href, nickname), prepend_api_base=False) return json.loads(response.content)
def test_nonurlencoded_post_query_and_data(self): r = requests.post(httpbin('post'), params='fooaowpeuf', data="foobar") self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, httpbin('post?fooaowpeuf')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), None) self.assertEquals(rbody.get('data'), 'foobar')
def organisation_repositories(organisation): repositories = [] response = github_api('/orgs/%(organisation)s/repos' % {'organisation': organisation}) if response.ok: repositories = [repository for repository in json.loads(response.content) if not repository['fork']] return repositories
def test_nonurlencoded_post_data(self): r = requests.post(httpbin('post'), data='fooaowpeuf') self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, httpbin('post')) rbody = json.loads(r.content) # Body wasn't valid url encoded data, so the server returns None as # "form" and the raw body as "data". self.assertEquals(rbody.get('form'), None) self.assertEquals(rbody.get('data'), 'fooaowpeuf')
def find_server(nickname): """ Finds a server based on nickname :param nickname: (optional) String representing the nickname of the server to lookup """ response = _request("/servers.js?filter=nickname=%s" % nickname) servers = json.loads(response.content) return servers[0] if len(servers) else None
def test_cookies_on_redirects(self): """Test interaction between cookie handling and redirection.""" # get a cookie for tinyurl.com ONLY s = requests.session() s.get(url='http://tinyurl.com/preview.php?disable=1') # we should have set a cookie for tinyurl: preview=0 self.assertTrue('preview' in s.cookies) self.assertEqual(s.cookies['preview'], '0') self.assertEqual(list(s.cookies)[0].name, 'preview') self.assertEqual(list(s.cookies)[0].domain, 'tinyurl.com') # get cookies on another domain r2 = s.get(url='http://httpbin.org/cookies') # the cookie is not there self.assertTrue('preview' not in json.loads(r2.text)['cookies']) # this redirects to another domain, httpbin.org # cookies of the first domain should NOT be sent to the next one r3 = s.get(url='http://tinyurl.com/7zp3jnr') assert r3.url == 'http://httpbin.org/cookies' self.assertTrue('preview' not in json.loads(r3.text)['cookies'])
def test_urlencoded_post_querystring_multivalued(self): for service in SERVICES: r = requests.post(service('post'), params=dict(test=['foo','baz'])) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, service('post?test=foo&test=baz')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), {}) # No form supplied self.assertEquals(rbody.get('data'), '')
def test_urlencoded_post_querystring_multivalued(self): for service in SERVICES: r = requests.post(service("post"), params=dict(test=["foo", "baz"])) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers["content-type"], "application/json") self.assertEquals(r.url, service("post?test=foo&test=baz")) rbody = json.loads(r.content) self.assertEquals(rbody.get("form"), {}) # No form supplied self.assertEquals(rbody.get("data"), "")
def test_urlencoded_post_query_and_data(self): for service in SERVICES: r = requests.post(service("post"), params=dict(test="fooaowpeuf"), data=dict(test2="foobar")) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers["content-type"], "application/json") self.assertEquals(r.url, service("post?test=fooaowpeuf")) rbody = json.loads(r.content) self.assertEquals(rbody.get("form"), dict(test2="foobar")) self.assertEquals(rbody.get("data"), "")
def test_nonurlencoded_post_querystring(self): for service in SERVICES: r = requests.post(service('post'), params='fooaowpeuf') self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, service('post?fooaowpeuf')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), {}) # No form supplied self.assertEquals(rbody.get('data'), '')
def organisation_repositories(organisation): repositories = [] response = github_api('/orgs/%(organisation)s/repos' % {'organisation': organisation}) if response.ok: repositories = [ repository for repository in json.loads(response.content) if not repository['fork'] ] return repositories
def pull_requests_with_comments(user, repository, state='closed'): pulls = [] comments = [] pulls = pull_requests(user, repository, state=state) if pulls: for pull_request in pulls: response = github_api('/repos/%(user)s/%(repository)s/pulls/%(pull_request_number)s/comments' % dict(user=user, repository=repository, pull_request_number=pull_request['number'])) if response.ok: comments += json.loads(response.content) return pulls, comments
def pull_requests(user, repository, state='closed'): pulls = [] response = github_api('/repos/%(user)s/%(repository)s/pulls' % { 'user': user, 'repository': repository }, data={'state': state}) if response.ok: pulls = json.loads(response.content) return pulls
def test_nonurlencoded_post_querystring(self): for service in SERVICES: r = requests.post(service("post"), params="fooaowpeuf") self.assertEquals(r.status_code, 200) self.assertEquals(r.headers["content-type"], "application/json") self.assertEquals(r.url, service("post?fooaowpeuf")) rbody = json.loads(r.content) self.assertEquals(rbody.get("form"), {}) # No form supplied self.assertEquals(rbody.get("data"), "")
def test_urlencoded_post_querystring(self): for service in SERVICES: r = requests.post(service('post'), params=dict(test='fooaowpeuf')) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, service('post?test=fooaowpeuf')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), {}) # No form supplied self.assertEquals(rbody.get('data'), '')
def test_nonurlencoded_postdata(self): for service in SERVICES: r = requests.post(service('post'), data="foobar") self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), {}) self.assertEquals(rbody.get('data'), 'foobar')
def test_nonurlencoded_postdata(self): for service in SERVICES: r = requests.post(service("post"), data="foobar") self.assertEquals(r.status_code, 200) self.assertEquals(r.headers["content-type"], "application/json") rbody = json.loads(r.content) self.assertEquals(rbody.get("form"), {}) self.assertEquals(rbody.get("data"), "foobar")
def test_urlencoded_post_data(self): for service in SERVICES: r = requests.post(service('post'), data=dict(test='fooaowpeuf')) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, service('post')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), dict(test='fooaowpeuf')) self.assertEquals(rbody.get('data'), '')
def test_nonurlencoded_post_query_and_data(self): for service in SERVICES: r = requests.post(service('post'), params='fooaowpeuf', data="foobar") self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, service('post?fooaowpeuf')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), None) self.assertEquals(rbody.get('data'), 'foobar')
def test_nonurlencoded_post_data(self): for service in SERVICES: r = requests.post(service("post"), data="fooaowpeuf") self.assertEquals(r.status_code, 200) self.assertEquals(r.headers["content-type"], "application/json") self.assertEquals(r.url, service("post")) rbody = json.loads(r.content) # Body wasn't valid url encoded data, so the server returns None as # "form" and the raw body as "data". self.assertEquals(rbody.get("form"), {}) self.assertEquals(rbody.get("data"), "fooaowpeuf")
def test_urlencoded_post_query_multivalued_and_data(self): for service in SERVICES: r = requests.post(service('post'), params=dict(test=['foo', 'baz']), data=dict(test2="foobar", test3=['foo', 'baz'])) self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, service('post?test=foo&test=baz')) rbody = json.loads(r.content) self.assertEquals(rbody.get('form'), dict(test2='foobar', test3=['foo', 'baz'])) self.assertEquals(rbody.get('data'), '')
def test_nonurlencoded_post_data(self): for service in SERVICES: r = requests.post(service('post'), data='fooaowpeuf') self.assertEquals(r.status_code, 200) self.assertEquals(r.headers['content-type'], 'application/json') self.assertEquals(r.url, service('post')) rbody = json.loads(r.content) # Body wasn't valid url encoded data, so the server returns None as # "form" and the raw body as "data". self.assertEquals(rbody.get('form'), {}) self.assertEquals(rbody.get('data'), 'fooaowpeuf')
def response_to_objects(response, obj): if not response.content: return [] try: json_content = json.loads(response.content) except json.JSONError: print response.content print "-------------------------------" print "Can not process content as json data" return [] if isinstance(json_content, list): return map(lambda x: obj(x), json_content) return obj(json_content)
def list_servers(deployment_id=None): """ Lists servers in a deployment Returns JSON :param deployment_id: (optional) String representing Deployment to list servers from """ if not deployment_id: deployment_id = config.settings.default_deployment_id if not deployment_id: raise Exception("Deployment id not specified in configuration or as " "an API parameter") response = _request("/deployments/%s.js" % deployment_id) return json.loads(response.content)
def pull_requests_with_comments(user, repository, state='closed'): pulls = [] comments = [] pulls = pull_requests(user, repository, state=state) if pulls: for pull_request in pulls: response = github_api( '/repos/%(user)s/%(repository)s/pulls/%(pull_request_number)s/comments' % dict(user=user, repository=repository, pull_request_number=pull_request['number'])) if response.ok: comments += json.loads(response.content) return pulls, comments
def request(self, api, **pars): req=api+('?'+urllib.urlencode(pars) if len(pars)>0 else "") headers={'Accept' : 'application/json'} if len(self.cookies): cookie="" for key in self.cookies: if cookie!="": cookie+='; ' cookie+=key+'='+self.cookies[key] headers['Cookie']= cookie response=app.request(req, headers=headers) self.assertEqual(response.status, "200 OK", "API call to '"+req+"' failed, response was "+repr(response)) if 'Set-Cookie' in response.headers: cookie=response.headers['Set-Cookie'] key, sep, value=cookie.partition('=') if ';' in value: value=value[:value.find(';')] self.cookies[key]=value data=json.loads(response.data) return (data, response)
def _fill_attrs(self, info_url, attr_name_set): response = _request("%s?format=js" % info_url, prepend_api_base=False) try: tmp = json.loads(response.content) except json.JSONError: print "Cannot process the following response:" print "--------------------------------" print response.content print "--------------------------------" else: if isinstance(tmp, dict): self.__setattr__(attr_name_set, Settings()) for set_name, set_value in tmp.items(): set_name = set_name.replace("-", "_") self.__getattribute__(attr_name_set).__setattr__(set_name, set_value) elif isinstance(tmp, list): self.__setattr__(attr_name_set, []) for tmp_dict in tmp: settings = Settings() for set_name, set_value in tmp_dict.items(): set_name = set_name.replace("-", "_") settings.__setattr__(set_name, set_value) self.__getattribute__(attr_name_set).append(settings)
def test_session_persistent_cookies(self): s = requests.session() # Internally dispatched cookies are sent. _c = {'kenneth': 'reitz', 'bessie': 'monke'} r = s.get(httpbin('cookies'), cookies=_c) r = s.get(httpbin('cookies')) # Those cookies persist transparently. c = json.loads(r.content).get('cookies') assert c == _c # Double check. r = s.get(httpbin('cookies'), cookies={}) c = json.loads(r.content).get('cookies') assert c == _c # Remove a cookie by setting it's value to None. r = s.get(httpbin('cookies'), cookies={'bessie': None}) c = json.loads(r.content).get('cookies') del _c['bessie'] assert c == _c # Test session-level cookies. s = requests.session(cookies=_c) r = s.get(httpbin('cookies')) c = json.loads(r.content).get('cookies') assert c == _c # Have the server set a cookie. r = s.get(httpbin('cookies', 'set', 'k', 'v'), allow_redirects=True) c = json.loads(r.content).get('cookies') assert 'k' in c # And server-set cookie persistience. r = s.get(httpbin('cookies')) c = json.loads(r.content).get('cookies') assert 'k' in c
def test_session_persistent_cookies(self): s = requests.session() # Internally dispatched cookies are sent. _c = {"kenneth": "reitz", "bessie": "monke"} r = s.get(httpbin("cookies"), cookies=_c) r = s.get(httpbin("cookies")) # Those cookies persist transparently. c = json.loads(r.content).get("cookies") assert c == _c # Double check. r = s.get(httpbin("cookies"), cookies={}) c = json.loads(r.content).get("cookies") assert c == _c # Remove a cookie by setting it's value to None. r = s.get(httpbin("cookies"), cookies={"bessie": None}) c = json.loads(r.content).get("cookies") del _c["bessie"] assert c == _c # Test session-level cookies. s = requests.session(cookies=_c) r = s.get(httpbin("cookies")) c = json.loads(r.content).get("cookies") assert c == _c # Have the server set a cookie. r = s.get(httpbin("cookies", "set", "k", "v"), allow_redirects=True) c = json.loads(r.content).get("cookies") assert "k" in c # And server-set cookie persistience. r = s.get(httpbin("cookies")) c = json.loads(r.content).get("cookies") assert "k" in c
#!/usr/bin/env python # -*- coding: utf-8 -*- import omnijson as json a = json.loads('{"yo":"dawg"}') print a print json.dumps(a) json.loads('{"yo": dawg"}')
def jloads(json_string): global cjson if cjson: return cjson.decode(json_string) else: return json.loads(json_string)
def load_data(organisation_name, update=False): data = {} path = '%s/%s.json' % (config.STORE, organisation_name) if os.path.exists(path) and not update: with open(path, 'rb') as f: data = json.loads(f.read()) else: organisation = github.organisation(organisation_name) projects = [ repository['name'] for repository in github.organisation_repositories(organisation_name) ] pull_request_map = {} pull_request_comments_map = {} pull_requests = [] pull_request_comments = [] projects_with_pulls = [] user_data = {} project_data = {} for project in projects: pulls, comments = github.pull_requests_with_comments( organisation_name, project, state='closed') print '[load_data] %s: got %d pull requests with %d comments' % ( project, len(pulls), len(comments)) open_pulls, open_comments = github.pull_requests_with_comments( organisation_name, project, state='open') print '[load_data] %s: got %d open pull requests with %d comments' % ( project, len(open_pulls), len(open_comments)) for user in [ x['user']['login'] for x in pulls + open_pulls if x['user'] ]: if user not in user_data: print '[load_data] %s: caching user %s' % (project, user) user_data[user] = github.user(user) if project not in project_data and (pulls + open_pulls): print '[load_data] caching project %s data' % project project_data[project] = (pulls or open_pulls)[0]['base']['repo'] pulls += open_pulls comments += open_comments pull_request_map[project] = pulls pull_request_comments_map[project] = comments pull_requests += pulls pull_request_comments += comments if pulls: projects_with_pulls.append(project) data = { 'pull_requests': pull_requests, 'pull_requests_per_project': pull_request_map, 'pull_request_comments': pull_request_comments, 'pull_request_comments_per_project': pull_request_comments_map, 'projects': projects, 'projects_with_pulls': projects_with_pulls, 'organisation': organisation, 'user_data': user_data, 'project_data': project_data, } with PersistentDict(path, 'c', format='json') as d: d.update(data) return data
def test_load_json(self): a = json.loads(self._good_json_string) self.assertEqual(a, self._good_json_result)