예제 #1
0
 def test_paged_get(self):
     self.register_response('get', API_ROOT + '/paged', 200,
                            {'link': '<{0}/paged?page=2>; rel="next"'.format(API_ROOT)},
                            '[{"item": 1}, {"item": 2}]')
     self.register_response('get', API_ROOT + '/paged?page=2', 200, {}, '[{"item": 3}]')
     client = GithubAPIClient(nickname='test')
     res = client.paged_get('/paged')
     assert_equal(res.next(), {'item': 1})
     assert_equal(res.next(), {'item': 2})
     assert_equal(res.next(), {'item': 3})
     with assert_raises(StopIteration):
         res.next()
예제 #2
0
def browse(args):
    """
    Print the GitHub API response at the given URL
    """
    with cli.catch_api_errors():
        client = GithubAPIClient(nickname=args.github)
        res = client.request(args.method, args.url, _raise=False)

        print('HTTP/1.1 {0} {1}'.format(res.status_code, res.reason))

        for k, v in res.headers.items():
            print("{0}: {1}".format(k, v))

        print()
        if res.json() is not None:
            print(json.dumps(res.json(), indent=2))
        else:
            print(res.content)
예제 #3
0
파일: browse.py 프로젝트: alphagov/ghtools
def browse(args):
    """
    Print the GitHub API response at the given URL
    """
    with cli.catch_api_errors():
        client = GithubAPIClient(nickname=args.github)
        res = client.request(args.method, args.url, _raise=False)

        print('HTTP/1.1 {0} {1}'.format(res.status_code, res.reason))

        for k, v in res.headers.items():
            print("{0}: {1}".format(k, v))

        print()
        if res.json() is not None:
            print(json.dumps(res.json(), indent=2))
        else:
            print(res.content)
예제 #4
0
def login(args):
    """
    Log into a GitHub instance, and print the resulting OAuth token.
    """
    with cli.catch_api_errors():
        client = GithubAPIClient(nickname=args.github)
        login_if_needed(client, args.scope, comment=args.comment)

        oauth_token_key = envkey(client.nickname, 'oauth_token')
        print("export {0}='{1}'".format(oauth_token_key, client.token))
예제 #5
0
def make_client(identifier):
    """
    Make a GithubAPIClient suitable for interacting with the passed identifier
    """

    return GithubAPIClient(nickname=identifier.github)
예제 #6
0
 def test_post(self):
     self.register_response('post', API_ROOT + '/bar', 201, {}, 'bar')
     c = GithubAPIClient(nickname='test')
     assert_equal(c.post('/bar').status_code, 201)
예제 #7
0
 def test_put(self):
     self.register_response('put', API_ROOT + '/baz', 200, {}, '{"message": "success"}')
     c = GithubAPIClient(nickname='test')
     assert_equal(c.put('/baz').json(), {'message': 'success'})
예제 #8
0
 def test_delete(self):
     self.register_response('delete', API_ROOT + '/bat', 204, {}, '')
     c = GithubAPIClient(nickname='test')
     assert_equal(c.delete('/bat').status_code, 204)
예제 #9
0
 def test_patch(self):
     self.register_response('patch', API_ROOT + '/foo', 200, {}, 'foo')
     c = GithubAPIClient(nickname='test')
     assert_equal(c.patch('/foo').content, 'foo')
예제 #10
0
 def test_login_should_set_token(self):
     c = GithubAPIClient(nickname='test')
     assert_equal(c.token, None)
     c.login('foo', 'bar')
     assert_equal(c.token, 'abc123')
     assert_true(c.logged_in)
예제 #11
0
 def test_request(self):
     self.register_response('options', API_ROOT + '/foo', 200, {}, 'foo')
     c = GithubAPIClient(nickname='test')
     assert_equal(c.request('options', '/foo').content, 'foo')
예제 #12
0
 def test_use_specified_ca_bundle(self):
     os.environ['GITHUB_TEST_CA_BUNDLE'] = '/usr/share/openssl/bundle.pem'
     c = GithubAPIClient(nickname='test')
     assert_equal(c._session.verify, '/usr/share/openssl/bundle.pem')
     del os.environ['GITHUB_TEST_CA_BUNDLE']
예제 #13
0
 def test_raise_if_unknown_github(self):
     with assert_raises(GithubError) as e:
         GithubAPIClient(nickname='unknown')
         assert_regexp_matches(r'No known API root', e.message)
예제 #14
0
 def test_explicitly_set_root(self):
     c = GithubAPIClient('https://github.mycorp/api/v3', nickname='mycorp')
     assert_equal(c.nickname, 'mycorp')
     assert_equal(c.root, 'https://github.mycorp/api/v3')
예제 #15
0
 def test_default_to_default_github(self):
     c = GithubAPIClient()
     assert_equal(c.nickname, DEFAULT_GITHUB)
예제 #16
0
def get_client(nickname):
    """Try to create a GithubAPIClient for the given nickname"""
    try:
        return GithubAPIClient(nickname=nickname)
    except GithubError as e:
        fail(str(e))