From 4cfc9912c00436ae8978033f8c8d48a6e448b548 Mon Sep 17 00:00:00 2001 From: Omar Brikaa Date: Tue, 10 Nov 2020 05:52:58 +0200 Subject: [PATCH] Added a method for getting a user by their id (#1691) * Added a method for getting a user by their id * Added get_user_by_id to MainClass stubs Fixes #1615 --- github/MainClass.py | 14 ++++++++++++++ github/MainClass.pyi | 1 + tests/Github_.py | 3 +++ tests/ReplayData/Github.testGetUserById.txt | 11 +++++++++++ 4 files changed, 29 insertions(+) create mode 100644 tests/ReplayData/Github.testGetUserById.txt diff --git a/github/MainClass.py b/github/MainClass.py index 2fe39ec7..d321ce6c 100644 --- a/github/MainClass.py +++ b/github/MainClass.py @@ -275,6 +275,20 @@ class Github(object): self.__requester, headers, data, completed=True ) + def get_user_by_id(self, user_id): + """ + :calls: `GET /user/:id `_ + :param user_id: int + :rtype: :class:`github.NamedUser.NamedUser` + """ + assert isinstance(user_id, int), user_id + headers, data = self.__requester.requestJsonAndCheck( + "GET", "/user/" + str(user_id) + ) + return github.NamedUser.NamedUser( + self.__requester, headers, data, completed=True + ) + def get_users(self, since=github.GithubObject.NotSet): """ :calls: `GET /users `_ diff --git a/github/MainClass.pyi b/github/MainClass.pyi index 9cbc7d3b..b25935c9 100644 --- a/github/MainClass.pyi +++ b/github/MainClass.pyi @@ -83,6 +83,7 @@ class Github: def get_user(self, login: _NotSetType = ...) -> AuthenticatedUser: ... @overload def get_user(self, login: str) -> NamedUser: ... + def get_user_by_id(self, user_id: int) -> NamedUser: ... def get_users( self, since: Union[int, _NotSetType] = ... ) -> PaginatedList[NamedUser]: ... diff --git a/tests/Github_.py b/tests/Github_.py index d4d05c39..8b4a6de4 100644 --- a/tests/Github_.py +++ b/tests/Github_.py @@ -320,6 +320,9 @@ class Github(Framework.TestCase): def testStringOfNotSet(self): self.assertEqual(str(github.GithubObject.NotSet), "NotSet") + def testGetUserById(self): + self.assertEqual(self.g.get_user_by_id(583231).login, "octocat") + def testGetUsers(self): self.assertListKeyBegin( self.g.get_users(), diff --git a/tests/ReplayData/Github.testGetUserById.txt b/tests/ReplayData/Github.testGetUserById.txt new file mode 100644 index 00000000..6a39b41a --- /dev/null +++ b/tests/ReplayData/Github.testGetUserById.txt @@ -0,0 +1,11 @@ +https +GET +api.github.com +None +/user/583231 +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +None +200 +[('Date', 'Sat, 12 Sep 2020 12:26:09 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Server', 'GitHub.com'), ('Status', '200 OK'), ('Cache-Control', 'public, max-age=60, s-maxage=60'), ('Vary', 'Accept, Accept-Encoding, Accept, X-Requested-With, Accept-Encoding'), ('ETag', 'W/"2409e6c518ebe03ecde9243734f63f93"'), ('Last-Modified', 'Sun, 23 Aug 2020 14:28:27 GMT'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('X-RateLimit-Limit', '60'), ('X-RateLimit-Remaining', '59'), ('X-RateLimit-Reset', '1599917169'), ('X-RateLimit-Used', '1'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '1; mode=block'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'C442:11459:3E06E4D:4A2F023:5F5CBE61')] +{"login":"octocat","id":583231,"node_id":"MDQ6VXNlcjU4MzIzMQ==","avatar_url":"https://avatars3.githubusercontent.com/u/583231?v=4","gravatar_id":"","url":"https://api.github.com/users/octocat","html_url":"https://github.com/octocat","followers_url":"https://api.github.com/users/octocat/followers","following_url":"https://api.github.com/users/octocat/following{/other_user}","gists_url":"https://api.github.com/users/octocat/gists{/gist_id}","starred_url":"https://api.github.com/users/octocat/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/octocat/subscriptions","organizations_url":"https://api.github.com/users/octocat/orgs","repos_url":"https://api.github.com/users/octocat/repos","events_url":"https://api.github.com/users/octocat/events{/privacy}","received_events_url":"https://api.github.com/users/octocat/received_events","type":"User","site_admin":false,"name":"The Octocat","company":"@github","blog":"https://github.blog","location":"San Francisco","email":null,"hireable":null,"bio":null,"twitter_username":null,"public_repos":8,"public_gists":8,"followers":3222,"following":9,"created_at":"2011-01-25T18:44:36Z","updated_at":"2020-08-23T14:28:27Z"} +