From 2215010b0e8bc26e00baca9f6e0241273d6a29dc Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Wed, 6 Feb 2013 00:04:29 +0100 Subject: [PATCH] Retrieve scopes for current oauth token from response headers (Issue #134) --- ReadMe.md | 1 + doc/ReferenceOfClasses.md | 1 + github/Github.py | 4 ++++ github/Requester.py | 5 +++++ github/tests/Issue134.py | 6 ++++++ .../ReplayData/Issue134.testGetOAuthScopesFromHeader.txt | 5 +++++ 6 files changed, 22 insertions(+) create mode 100644 github/tests/ReplayData/Issue134.testGetOAuthScopesFromHeader.txt diff --git a/ReadMe.md b/ReadMe.md index 917f546f..141a92b2 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -19,6 +19,7 @@ What's new? * Fix bug in PaginatedList without url parameters. Thank you [llimllib](https://github.com/llimllib) for the [contribution](https://github.com/jacquev6/PyGithub/pull/133) * [Implement](https://github.com/jacquev6/PyGithub/issues/130) `NamedUser.get_keys` * [Support PubSubHub](https://github.com/jacquev6/PyGithub/issues/129): `Repository.subscribe_to_hub` and `Repository.unsubscribe_from_hub` +* [Publish the oauth scopes](https://github.com/jacquev6/PyGithub/issues/134) in Github.oauth_scopes, thank you [ bilderbuchi](https://github.com/ bilderbuchi) for asking Previous versions ----------------- diff --git a/doc/ReferenceOfClasses.md b/doc/ReferenceOfClasses.md index a7e3471a..4546c633 100644 --- a/doc/ReferenceOfClasses.md +++ b/doc/ReferenceOfClasses.md @@ -20,6 +20,7 @@ Another argument, that can be passed is `timeout` which has default value `10`. Attributes ---------- * `rate_limiting`: tuple of two integers: remaining and limit, as explained in [Rate Limiting](http://developer.github.com/v3/#rate-limiting) +* `oauth_scopes`: None or list of string: when using oauth, after the first request, the list of scopes of the token (see http://developer.github.com/v3/oauth/#scopes) Methods ------- diff --git a/github/Github.py b/github/Github.py index 1ec0f043..66e0cb88 100644 --- a/github/Github.py +++ b/github/Github.py @@ -48,6 +48,10 @@ class Github(object): def rate_limiting(self): return self.__requester.rate_limiting + @property + def oauth_scopes(self): + return self.__requester.oauth_scopes + def get_user(self, login=github.GithubObject.NotSet): assert login is github.GithubObject.NotSet or isinstance(login, (str, unicode)), login if login is github.GithubObject.NotSet: diff --git a/github/Requester.py b/github/Requester.py index b8293dd2..ddb0fbb2 100644 --- a/github/Requester.py +++ b/github/Requester.py @@ -69,6 +69,8 @@ class Requester: self.rate_limiting = (5000, 5000) self.FIX_REPO_GET_GIT_REF = True + self.oauth_scopes = None + self.__clientId = client_id self.__clientSecret = client_secret self.__userAgent = user_agent @@ -135,6 +137,9 @@ class Requester: if "x-ratelimit-remaining" in responseHeaders and "x-ratelimit-limit" in responseHeaders: self.rate_limiting = (int(responseHeaders["x-ratelimit-remaining"]), int(responseHeaders["x-ratelimit-limit"])) + if "x-oauth-scopes" in responseHeaders: + self.oauth_scopes = responseHeaders["x-oauth-scopes"].split(", ") + return status, responseHeaders, output def __requestRaw(self, verb, url, requestHeaders, input): diff --git a/github/tests/Issue134.py b/github/tests/Issue134.py index 38c52631..51be2106 100644 --- a/github/tests/Issue134.py +++ b/github/tests/Issue134.py @@ -31,3 +31,9 @@ class Issue134(Framework.BasicTestCase): # https://github.com/jacquev6/PyGithub def testGetAuthorizationsSucceedsWhenAutenticatedThroughLoginPassword(self): g = github.Github(self.login, self.password) self.assertListKeyEqual(g.get_user().get_authorizations(), lambda a: a.note, [None, None, 'cligh', None, None, 'GitHub Android App']) + + def testGetOAuthScopesFromHeader(self): + g = github.Github(self.oauth_token) + self.assertEqual(g.oauth_scopes, None) + g.get_user().name + self.assertEqual(g.oauth_scopes, ['repo', 'user', 'gist']) diff --git a/github/tests/ReplayData/Issue134.testGetOAuthScopesFromHeader.txt b/github/tests/ReplayData/Issue134.testGetOAuthScopesFromHeader.txt new file mode 100644 index 00000000..b4d0fb98 --- /dev/null +++ b/github/tests/ReplayData/Issue134.testGetOAuthScopesFromHeader.txt @@ -0,0 +1,5 @@ +https GET api.github.com None /user {'Authorization': 'token private_token_removed'} null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4992'), ('x-github-media-type', 'github.beta; format=json'), ('x-content-type-options', 'nosniff'), ('vary', 'Accept, Authorization, Cookie'), ('content-length', '1464'), ('server', 'GitHub.com'), ('last-modified', 'Tue, 05 Feb 2013 18:44:48 GMT'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"9257360a3470aac247f722352581abb1"'), ('cache-control', 'private, s-maxage=60, max-age=60'), ('date', 'Tue, 05 Feb 2013 22:57:03 GMT'), ('x-oauth-scopes', 'repo, user, gist'), ('content-type', 'application/json; charset=utf-8'), ('x-accepted-oauth-scopes', 'user:follow, user:email, user')] +{"public_repos":21,"type":"User","repos_url":"https://api.github.com/users/jacquev6/repos","followers_url":"https://api.github.com/users/jacquev6/followers","url":"https://api.github.com/users/jacquev6","subscriptions_url":"https://api.github.com/users/jacquev6/subscriptions","following_url":"https://api.github.com/users/jacquev6/following","events_url":"https://api.github.com/users/jacquev6/events{/privacy}","total_private_repos":4,"following":37,"html_url":"https://github.com/jacquev6","created_at":"2010-07-09T06:10:06Z","collaborators":1,"location":"Paris, France","received_events_url":"https://api.github.com/users/jacquev6/received_events","owned_private_repos":4,"followers":22,"organizations_url":"https://api.github.com/users/jacquev6/orgs","gravatar_id":"b68de5ae38616c296fa345d2b9df2225","email":"vincent@vincent-jacques.net","gists_url":"https://api.github.com/users/jacquev6/gists{/gist_id}","avatar_url":"https://secure.gravatar.com/avatar/b68de5ae38616c296fa345d2b9df2225?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-user-420.png","disk_usage":13360,"public_gists":2,"name":"Vincent Jacques","blog":"http://vincent-jacques.net","hireable":false,"plan":{"collaborators":1,"space":614400,"name":"micro","private_repos":5},"starred_url":"https://api.github.com/users/jacquev6/starred{/owner}{/repo}","id":327146,"login":"jacquev6","private_gists":5,"updated_at":"2013-02-05T18:44:48Z","company":"Criteo","bio":""} +