forked from shabane/gitlynx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.py
44 lines (38 loc) · 1.5 KB
/
github.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import requests
import json
def upload(token: str, file_name: str, data: str, repo: str, owner: str, path: str = None):
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28',
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
"message": f"push file: {file_name}",
"content": data,
}
data = json.dumps(data)
if path:
response = requests.put(f'https://api.github.com/repos/{owner}/{repo}/contents/{path}/{file_name}', headers=headers, data=data)
else:
response = requests.put(f'https://api.github.com/repos/{owner}/{repo}/contents/{file_name}', headers=headers, data=data)
if response.status_code != 201:
return response
return response
def getContents(token: str, repo: str, owner: str) -> list:
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28',
'Content-Type': 'application/x-www-form-urlencoded',
}
response = requests.get(f'https://api.github.com/repos/{owner}/{repo}/contents/', headers=headers)
if response.status_code == 200:
_ = []
if response.content:
data = json.loads(response.content)
for i in data:
_.append(i['name'])
return json.dumps(_)
return _
raise Exception(f"there where an error to github: {response.status_code}")