这几个简单的API应用,主要是通过TOKEN来获取GIT内空的例子。
但在获取GIT的文件列表时,要注意区分目录和文件的MODE差别(
100644 普通文件
040000 普通目录
)。
import requests class GitHub: def __init__(self): self.__url = 'http://1。2.3.4/api/v3/' self.__private_token = 'sdtrwr324sdfsdf' def get_project_id(self, project_name): project_name = str(project_name).lower() url = self.__url + 'projects/search/' + project_name + '?private_token=' + self.__private_token req = requests.get(url) data = req.json() return str(data[0]['id']) def get_project_files(self, project_name, version): project_name = str(project_name).lower() project_id = self.get_project_id(project_name) url = self.__url + 'projects/' + project_id + '/repository/tree?private_token=' + self.__private_token + '&path=' + version req = requests.get(url) data = req.json() return data def get_file_content(self, project_name, version, file_name): project_name = str(project_name).lower() project_id = self.get_project_id(project_name) url = self.__url + 'projects/' + project_id + '/repository/files?private_token=' + self.__private_token + '&ref=master&file_path=' + version + '/' + file_name req = requests.get(url) data = req.json() return data def get_file_url(self, project_name, version, file_name): project_name = str(project_name).lower() project_id = self.get_project_id(project_name) url = 'http://1.2.3.4/prj_sql/' + project_name + '/blob/master/' + version + '/' + file_name return url
获取文件列表示例(内含子目录):
[{"id":"9cefa7fcd94d23ce6a9afa63c185ae8491580f9d","name":"25.1","type":"tree","mode":"040000"},{"id":"bd34591bcd6d8983ea5c1ab35fd5fb9d226f3f5f","name":"25.2","type":"tree","mode":"040000"},{"id":"f89a96f1aaafcbd16f693a9007466cfd5735636e","name":"101_u01_create_t_model_dd.sql","type":"blob","mode":"100644"},{"id":"5f848d696dd890e4cdd3063b2b596a8921cbf873","name":"102_u01_create_syn_t_model_aa.sql","type":"blob","mode":"100644"},{"id":"9671cef3770c0abb04065240c8edb245c78585eb","name":"103_u01_grant_t_model_cc.sql","type":"blob","mode":"100644"}]
时间: 2024-11-05 23:22:07