From 8c4a9dc7988a259319bfb82e587291f8de86863e Mon Sep 17 00:00:00 2001 From: zhangting85 <89507288@qq.com> Date: Sun, 14 Apr 2019 19:46:20 +0800 Subject: [PATCH 1/2] Teach 005 (#5) * init commit * add the login of github by username:password or by token * make the 1st api packed * format the code * add more methods * remove the token * fix the format * teach branch 005 * remove token * remove import json --- api/__init__.py | 0 api/repositories/__init__.py | 0 api/repositories/repos.py | 56 ++++++++++++++++++++++++++++++++++ core/__init__.py | 0 core/rest_client.py | 58 ++++++++++++++++++++++++++++++++++++ github.py | 54 +++++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 api/__init__.py create mode 100644 api/repositories/__init__.py create mode 100644 api/repositories/repos.py create mode 100644 core/__init__.py create mode 100644 core/rest_client.py create mode 100644 github.py diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/repositories/__init__.py b/api/repositories/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/repositories/repos.py b/api/repositories/repos.py new file mode 100644 index 0000000..68c1dc0 --- /dev/null +++ b/api/repositories/repos.py @@ -0,0 +1,56 @@ +from core.rest_client import RestClient + + +class Repos(RestClient): + def __init__(self, api_root_url, **kwargs): + super(Repos, self).__init__(api_root_url, **kwargs) + + def list_your_repos(self, **kwargs): + """ + https://developer.github.com/v3/repos/#list-your-repositories + """ + return self.get("/user/repos", **kwargs) + + def list_user_repos(self, username, **kwargs): + """ + https://developer.github.com/v3/repos/#list-user-repositories + :param username: username + """ + return self.get("/users/{}/repos".format(username), **kwargs) + + def list_organization_repos(self, org, **kwargs): + """ + https://developer.github.com/v3/repos/#list-organization-repositories + :param org: orgnization name + """ + return self.get("/orgs/{}/repos".format(org), **kwargs) + + def list_all_public_repos(self, **kwargs): + """ + https://developer.github.com/v3/repos/#list-all-public-repositories + """ + return self.get("/repositories", **kwargs) + + def create_user_repo(self, **kwargs): + """ + https://developer.github.com/v3/repos/#create + """ + return self.post("/user/repos", **kwargs) + + def create_organization_repo(self, org, **kwargs): + """ + https://developer.github.com/v3/repos/#create + """ + return self.post("/orgs/{}/repos".format(org), **kwargs) + + def get_repo(self, owner, repo, **kwargs): + """ + https://developer.github.com/v3/repos/#get + """ + return self.get("/repos/{}/{}".format(owner, repo), **kwargs) + + def edit_repo(self, owner, repo, **kwargs): + """ + https://developer.github.com/v3/repos/#edit + """ + return self.patch("/repos/{}/{}".format(owner, repo), **kwargs) diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/rest_client.py b/core/rest_client.py new file mode 100644 index 0000000..bf07fbc --- /dev/null +++ b/core/rest_client.py @@ -0,0 +1,58 @@ +import requests +import json as json_parser + + +class RestClient(): + def __init__(self, api_root_url, username=None, password=None, token=None): + self.api_root_url = api_root_url + self.session = requests.session() + if username and password: + self.session.auth = (username, password) + elif token: + self.session.headers["Authorization"] = "token {}".format(token) + + def get(self, url, **kwargs): + return self.request(url, "get", **kwargs) + + def post(self, url, data=None, json=None, **kwargs): + return self.request(url, "post", data, json, **kwargs) + + def options(self, url, **kwargs): + return self.request(url, "potions", **kwargs) + + def head(self, url, **kwargs): + return self.request(url, "head", **kwargs) + + def put(self, url, data=None, **kwargs): + return self.request(url, "put", data, **kwargs) + + def patch(self, url, data=None, **kwargs): + return self.request(url, "patch", data, **kwargs) + + def delete(self, url, **kwargs): + return self.request(url, "delete", **kwargs) + + def request(self, url, method_name, data=None, json=None, **kwargs): + url = self.api_root_url + url + if method_name == "get": + return self.session.get(url, **kwargs) + if method_name == "post": + return self.session.post(url, data, json, **kwargs) + if method_name == "options": + return self.session.options(url, **kwargs) + if method_name == "head": + return self.session.head(url, **kwargs) + if method_name == "put": + return self.session.put(url, data, **kwargs) + if method_name == "patch": + if json: + data = json_parser.dumps(json) + return self.session.patch(url, data, **kwargs) + if method_name == "delete": + return self.session.delete(url, **kwargs) + + +if __name__ == '__main__': + r = RestClient("http://httpbin.org") + x = r.post("/post", json={"a": "b"}) + print(x.text) diff --git a/github.py b/github.py new file mode 100644 index 0000000..3a3da8e --- /dev/null +++ b/github.py @@ -0,0 +1,54 @@ +from api.repositories.repos import Repos + + +class Github(): + def __init__(self, **kwargs): + self.api_root_url = "https://api.github.com" + self.repos = Repos(self.api_root_url, **kwargs) + + +if __name__ == '__main__': + r = Github(token="xxxxx") + username = "zhangting85" + orgnname = "xxxx" + + # case 1 + data = { + "name": "Hello-WorldXXX", + "description": "This is your first repository", + "homepage": "https://github.com", + "private": False, + "has_issues": True, + "has_projects": True, + "has_wiki": True + } + x = r.repos.create_user_repo(json=data) + print(x.status_code) + assert x.status_code == 201 + + # case 2 + x = r.repos.create_organization_repo(org=orgnname, json=data) + print(x.status_code) + assert x.status_code == 201 + + # case 3 + x = r.repos.get_repo(username, "Hello-World") + print(x.status_code) + assert x.status_code == 200 + print(x.text) + + # case 4 + data = { + "name": "Hello-World", + "description": "YYYY:This is your first repository ", + "homepage": "https://github.com", + "private": False, + "has_issues": True, + "has_projects": True, + "has_wiki": True + } + + x = r.repos.edit_repo(username, "Hello-World", json=data) + print(x.status_code) + print(x.text) + assert x.status_code == 200 From 5ded9c1a072831ced650928acf713d2d125b0130 Mon Sep 17 00:00:00 2001 From: zhangting <89507288@qq.com> Date: Wed, 17 Apr 2019 21:00:29 +0800 Subject: [PATCH 2/2] create the folders --- api/activity/__init__.py | 0 api/checks/__init__.py | 0 api/gists/__init__.py | 0 api/git_data/__init__.py | 0 api/github_apps/__init__.py | 0 api/github_marketplace/__init__.py | 0 api/interactions/__init__.py | 0 api/issues/__init__.py | 0 api/issues/issues.py | 12 ++++++++ api/migrations/__init__.py | 0 api/miscellaneous/__init__.py | 0 api/organizations/__init__.py | 0 api/projects/__init__.py | 0 api/pull_requests/__init__.py | 0 api/reactions/__init__.py | 0 api/scim/__init__.py | 0 api/search/__init__.py | 0 api/teams/__init__.py | 0 api/users/__init__.py | 0 core/rest_client.py | 5 --- github.py | 49 ++---------------------------- 21 files changed, 14 insertions(+), 52 deletions(-) create mode 100644 api/activity/__init__.py create mode 100644 api/checks/__init__.py create mode 100644 api/gists/__init__.py create mode 100644 api/git_data/__init__.py create mode 100644 api/github_apps/__init__.py create mode 100644 api/github_marketplace/__init__.py create mode 100644 api/interactions/__init__.py create mode 100644 api/issues/__init__.py create mode 100644 api/issues/issues.py create mode 100644 api/migrations/__init__.py create mode 100644 api/miscellaneous/__init__.py create mode 100644 api/organizations/__init__.py create mode 100644 api/projects/__init__.py create mode 100644 api/pull_requests/__init__.py create mode 100644 api/reactions/__init__.py create mode 100644 api/scim/__init__.py create mode 100644 api/search/__init__.py create mode 100644 api/teams/__init__.py create mode 100644 api/users/__init__.py diff --git a/api/activity/__init__.py b/api/activity/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/checks/__init__.py b/api/checks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/gists/__init__.py b/api/gists/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/git_data/__init__.py b/api/git_data/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/github_apps/__init__.py b/api/github_apps/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/github_marketplace/__init__.py b/api/github_marketplace/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/interactions/__init__.py b/api/interactions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/issues/__init__.py b/api/issues/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/issues/issues.py b/api/issues/issues.py new file mode 100644 index 0000000..ec24bf0 --- /dev/null +++ b/api/issues/issues.py @@ -0,0 +1,12 @@ +from core.rest_client import RestClient + + +class Issues(RestClient): + def __init__(self, api_root_url, **kwargs): + super(Issues, self).__init__(api_root_url, **kwargs) + + def create_issue(self, owner, repo, **kwargs): + """ + https://developer.github.com/v3/issues/#create-an-issue + """ + return self.post("/repos/{}/{}/issues".format(owner, repo), **kwargs) diff --git a/api/migrations/__init__.py b/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/miscellaneous/__init__.py b/api/miscellaneous/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/organizations/__init__.py b/api/organizations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/projects/__init__.py b/api/projects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/pull_requests/__init__.py b/api/pull_requests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/reactions/__init__.py b/api/reactions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/scim/__init__.py b/api/scim/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/search/__init__.py b/api/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/teams/__init__.py b/api/teams/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/users/__init__.py b/api/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/rest_client.py b/core/rest_client.py index bf07fbc..dada251 100644 --- a/core/rest_client.py +++ b/core/rest_client.py @@ -51,8 +51,3 @@ def request(self, url, method_name, data=None, json=None, **kwargs): if method_name == "delete": return self.session.delete(url, **kwargs) - -if __name__ == '__main__': - r = RestClient("http://httpbin.org") - x = r.post("/post", json={"a": "b"}) - print(x.text) diff --git a/github.py b/github.py index 3a3da8e..1454899 100644 --- a/github.py +++ b/github.py @@ -1,54 +1,9 @@ from api.repositories.repos import Repos +from api.issues.issues import Issues class Github(): def __init__(self, **kwargs): self.api_root_url = "https://api.github.com" self.repos = Repos(self.api_root_url, **kwargs) - - -if __name__ == '__main__': - r = Github(token="xxxxx") - username = "zhangting85" - orgnname = "xxxx" - - # case 1 - data = { - "name": "Hello-WorldXXX", - "description": "This is your first repository", - "homepage": "https://github.com", - "private": False, - "has_issues": True, - "has_projects": True, - "has_wiki": True - } - x = r.repos.create_user_repo(json=data) - print(x.status_code) - assert x.status_code == 201 - - # case 2 - x = r.repos.create_organization_repo(org=orgnname, json=data) - print(x.status_code) - assert x.status_code == 201 - - # case 3 - x = r.repos.get_repo(username, "Hello-World") - print(x.status_code) - assert x.status_code == 200 - print(x.text) - - # case 4 - data = { - "name": "Hello-World", - "description": "YYYY:This is your first repository ", - "homepage": "https://github.com", - "private": False, - "has_issues": True, - "has_projects": True, - "has_wiki": True - } - - x = r.repos.edit_repo(username, "Hello-World", json=data) - print(x.status_code) - print(x.text) - assert x.status_code == 200 + self.issues = Issues(self.api_root_url, **kwargs)