diff --git a/pro/.idea/.gitignore b/pro/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/pro/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/pro/.idea/inspectionProfiles/profiles_settings.xml b/pro/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/pro/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/pro/.idea/misc.xml b/pro/.idea/misc.xml new file mode 100644 index 0000000..a2e120d --- /dev/null +++ b/pro/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/pro/.idea/modules.xml b/pro/.idea/modules.xml new file mode 100644 index 0000000..71c9e66 --- /dev/null +++ b/pro/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/pro/.idea/pro.iml b/pro/.idea/pro.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/pro/.idea/pro.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/pro/TUGithubAPI/api/__init__.py b/pro/TUGithubAPI/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pro/TUGithubAPI/api/repositories/__init__.py b/pro/TUGithubAPI/api/repositories/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pro/TUGithubAPI/api/repositories/repos.py b/pro/TUGithubAPI/api/repositories/repos.py new file mode 100644 index 0000000..bf10e01 --- /dev/null +++ b/pro/TUGithubAPI/api/repositories/repos.py @@ -0,0 +1,76 @@ +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, username, **kwargs): + # def list_your_repos(self,**kwargs): + # params={ + # "visibility":visibility, + # "affiliation":affiliation, + # "type":type, + # "sort":sort, + # "direction":direction + # + # + # } + # return self.get("/user/repos",params = params) + # return self.get("/user/repos", **kwargs) + return self.get("/users/{}/repos".format(username), **kwargs) + + def list_organizition(self, org, **kwargs): + return self.get("/orgs/{}/repos".format(org), **kwargs) + + def list_all_repositories(self, **kwargs): + return self.get("/repositories", **kwargs) + + def create_user_repositories(self, **kwargs): + return self.post("/user/repos", **kwargs) + + def create_org_repositories(self, org, **kwargs): + return self.post("/orgs/{}/repos".format(org), **kwargs) + + def get_a_repositories(self, owner, repos, **kwargs ): + return self.get("/repos/{}/{}".format(owner, repos), **kwargs) + + def edit_repositories(self, owner, repos, **kwargs): + return self.patch("/repos/{}/{}".format(owner, repos), **kwargs) + """ + 以下函数为项目实战 + """ + + def list_pull_requests(self, owner, repos, **kwargs): + return self.get("/repos/{}/{}/pulls".format(owner, repos), **kwargs) + + def get_a_pull_request(self, owner, repos, pull_num, **kwargs): + return self.get("/repos/{}/{}/pulls/{}".format(owner, repos, pull_num), **kwargs) + + def create_a_pull_request(self, owner, repos, **kwargs): + return self.post("/repos/{}/{}/pulls".format(owner, repos), **kwargs) + + def update_a_pull_request_branch(self, owner, repos, pull_num, **kwargs): + return self.put("/repos/{}/{}/pulls/{}/update-branch".format(owner, repos, pull_num), **kwargs) + + def update_a_pull_request(self, owner, repos, pull_num, **kwargs ): + return self.patch("/repos/{}/{}/pulls/{}".format(owner, repos, pull_num), **kwargs) + + def list_commits(self, owner, repos, pull_num, **kwargs): + return self.get("/repos/{}/{}/pulls/{}/commits".format(owner, repos, pull_num), **kwargs) + + def list_pull_requests_files(self, owner, repos, pull_num, **kwargs): + return self.get("/repos/{}/{}/pulls/{}/files".format(owner, repos, pull_num), **kwargs) + + def check_merged(self, owner, repos, pull_num, **kwargs): + return self.get("/repos/{}/{}/pulls/{}/merge".format(owner, repos, pull_num), **kwargs) + + def merge_pull_request(self, owner, repos, pull_num, **kwargs): + return self.put("/repos/{}/{}/pulls/{}/merge".format(owner, repos, pull_num), **kwargs) + + + + + + + + diff --git a/pro/TUGithubAPI/content.txt b/pro/TUGithubAPI/content.txt new file mode 100644 index 0000000..a0063c5 --- /dev/null +++ b/pro/TUGithubAPI/content.txt @@ -0,0 +1,11 @@ +MSH|^~\&|LIS||HIS||201804080952||OUL^R21^OUL_R21|694C8642E587C93FE050A8C06D0C6FDA|P|2.6||||||utf-8 + +PID||2501333371|||ϼ|||2 +PV1||1||R|||101761^||||||||||||4||||||||||||||||||||H0001 +ORC|SN||||||||||||||||||||||||||||C|||ֳҽѧ^^110304 +OBR|1|01C201803-1310088||13201^״ϸѧ|||20180408092021|||||||20180408094822|39&|||||||20180408095214|||F|||||||||&|&ϲ +OBX|1|TX|6636^|1|II||||||F||||||N +OBX|2|TX|6637^|1|6||||||F||||||N +OBX|3|TX|6638^ϸ(BV)|1|-||||||F||||||N +OBX|4|TX|6639^ϸ|1|-||||||F||||||N +OBX|5|TX|6640^γ|1|-||||||F||||||N \ No newline at end of file diff --git a/pro/TUGithubAPI/core/__init__.py b/pro/TUGithubAPI/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pro/TUGithubAPI/core/rest_client.py b/pro/TUGithubAPI/core/rest_client.py new file mode 100644 index 0000000..f3ff9d3 --- /dev/null +++ b/pro/TUGithubAPI/core/rest_client.py @@ -0,0 +1,61 @@ +import requests, json +import json as json_parser + +#传入一个固定的url +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) + + if 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) + return self.session.post(url, data, json, **kwargs) + if method_name == "delete": + return self.session.delete(url, **kwargs) + + +# if __name__ == '__main__': +# r = RestClient("https://github.com/") +# x = r.get("/get", json={"a": "b"}) +# print(x.text) \ No newline at end of file diff --git a/pro/TUGithubAPI/get_token.py b/pro/TUGithubAPI/get_token.py new file mode 100644 index 0000000..4817363 --- /dev/null +++ b/pro/TUGithubAPI/get_token.py @@ -0,0 +1,27 @@ +import requests,json + +url="https://github.com/login" +headers={'Content-Type':'application/json;charset=UTF-8'} +request_param={ + "username":"cassie01", + "password":"xd19941225" +} +response=requests.post(url,data=json.dumps(request_param), headers=headers) +print(response.text) +print(response.json()['data']['token']) + + +import requests +def login(): + url = "http://test.xxxxxxx.com/api/common/login/login" + data = { + "username": "cassie01", + "password": "xd19941225" + } + h = { + "User-Agent": "Android/H60-L01/8.1.0/" + + } + res = requests.post(url, data=data, headers=h) + print(res.json()) + login() \ No newline at end of file diff --git a/pro/TUGithubAPI/github.py b/pro/TUGithubAPI/github.py new file mode 100644 index 0000000..609ab0c --- /dev/null +++ b/pro/TUGithubAPI/github.py @@ -0,0 +1,74 @@ +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="67c64dcc041f10947fabdb067d821fac1a1aeef7") + # username = "cassie01" + # orgname = "hello0811world" + # + # # case 1 + # """注意GitHub类中的网址需要是https""" + # data = { + # "name" : "Test01", + # "description":"this is your first repository in org", + # "homepage": "https://github.com", + # "private" : False, + # "has_issues": True, + # "has_projects": True, + # "has_wiki": True + # } + # x = r.repos.create_user_repositories(json=data) + # print(x.status_code) + # assert x.status_code == 201 + + + + #case 2 + # """需要先在GitHub中建立organization""" + # x = r.repos.create_org_repositories(org= orgname, json = data) + # print(x.status_code) + # assert x.status_code == 201 + + #case 3 + # x = r.repos.get_a_repositories(username, "Test01") + # print(x.status_code) + # assert x.status_code == 200 + + #case 4 + # data = { + # "name": "Test01", + # "description": "this is not your big repository in org", + # "homepage": "https://github.com", + # "private": False, + # "has_issues": True, + # "has_projects": True, + # "has_wiki": True + # } + # x = r.repos.edit_repositories(username, 'Test01', json = data) + # print(x.status_code) + # assert x.status_code == 200 + + """ + 项目实战: + API文档:https://developer.github.com/v3/pulls/#custom-media-types + """ + r = Github(token="67c64dcc041f10947fabdb067d821fac1a1aeef7") + username = "cassie01" + orgname = "teach_005" + #case 1 + # data = { + # "state": all, + # "direction": "desc" + # } + # x = r.repos.list_pull_requests(username, orgname, json = data) + # print(x.text) + # assert x.status_code == 200 + + #case 2 + # x = r.repos.get_a_pull_request(username, orgname, 3) + # print(x) + # assert x.status_code == 200 \ No newline at end of file