-
Notifications
You must be signed in to change notification settings - Fork 1
/
toolkit_requests.py
134 lines (114 loc) · 4.79 KB
/
toolkit_requests.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import urllib.parse
import requests
import json
import logging
import base64
import config_loader
try:
from urllib.parse import quote_plus
except ImportError:
from urllib import quote_plus
config = config_loader.config
# proxies = {"http": "socks5://127.0.0.1:8888", }
class Gitea():
"""docstring for Gitea."""
def __init__(self, token: str, host='https://gitea.com'):
self.host = host
self.token = token
self.sess = requests.Session()
self.headers = {
"Authorization": "token " + token,
}
def webpage_get(self, url, headers=None, allow_redirects=True):
if headers is None:
headers = self.headers
# print("Get: " + url)
proxies = None
self.resp = self.sess.get(
url, headers=headers, allow_redirects=allow_redirects, verify=False, proxies=proxies, timeout=10)
return self.resp
def webpage_post(self, url, data=None, json=None, headers=None):
if headers is None:
headers = self.headers
proxies = None
self.resp = self.sess.post(
url, headers=headers, data=data, json=json, verify=False, proxies=proxies, timeout=10)
return self.resp
def webpage_put(self, url, data=None, json=None, headers=None):
if headers is None:
headers = self.headers
proxies = None
self.resp = self.sess.put(
url, headers=headers, data=data, json=json, verify=False, proxies=proxies, timeout=10)
return self.resp
def webpage_delete(self, url, headers=None, allow_redirects=True):
if headers is None:
headers = self.headers
proxies = None
self.resp = self.sess.delete(
url, headers=headers, allow_redirects=allow_redirects, verify=False, proxies=proxies, timeout=10)
return self.resp
def request_api(self, method: str, api_endpoint: str, payload={}) -> str:
"""
Request Gitea API.
doc: https://gitea.com/api/swagger
Args:
method (str): Request method, one of ("GET", "POST", "DELETE", "PUT")
api_endpoint (str): api endpoint
payload (dict): payload in post request
Returns:
str: _description_
"""
url = f'{self.host}/api/v1{api_endpoint}'
match method:
case "GET":
self.resp = self.webpage_get(url, headers=self.headers, allow_redirects=True)
case "POST":
self.resp = self.webpage_post(url, headers=self.headers, json=payload)
case "PUT":
self.resp = self.webpage_put(url, headers=self.headers, json=payload)
case "DELETE":
self.resp = self.webpage_delete(url, headers=self.headers)
try:
assert self.resp.status_code in (200, 201)
except AssertionError as e:
logging.error('Error request API:')
logging.error(self.resp.content.decode('unicode-escape'))
self.resp.raise_for_status()
return json.loads(self.resp.text)
def get_file_content(self, owner: str, repo: str, filepath: str) -> dict:
api_endpoint = f"/repos/{owner}/{repo}/contents/{filepath}"
result = self.request_api("GET", api_endpoint)
return result
def create_file_content(self, owner: str, repo: str, filepath: str, content: str) -> dict:
payload = {
"content": base64.b64encode(content.encode()).decode()
}
api_endpoint = f"/repos/{owner}/{repo}/contents/{filepath}"
result = self.request_api("POST", api_endpoint, payload=payload)
return result
def update_file_content(self, owner: str, repo: str, filepath: str, content: str) -> dict:
payload = {
"content": base64.b64encode(content.encode()).decode(),
"sha": self.get_file_content(owner, repo, filepath)["sha"],
}
logging.debug(' '.join((owner, repo, filepath)))
logging.debug(json.dumps(payload))
api_endpoint = f"/repos/{owner}/{repo}/contents/{filepath}"
result = self.request_api("PUT", api_endpoint, payload=payload)
return result
if __name__ == "__main__":
logger = logging.getLogger()
handler = logging.StreamHandler()
LOG_FORMAT = '[%(asctime)s] %(levelname)8s - %(name)s - %(message)s'
formatter = logging.Formatter(LOG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# token = ""
# gitea = Gitea(token=token)
# owner = "duchenpaul"
# repo = "mixed_nodes"
# filepath = "v2board_nodes.txt"
# print(gitea.update_file_content(owner, repo, filepath, 'teddst'))
pass