-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-example.py
More file actions
186 lines (127 loc) · 4.28 KB
/
Copy pathpython-example.py
File metadata and controls
186 lines (127 loc) · 4.28 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import requests
class RequestBin:
def __init__(self, url: str, token: str | None = None):
self.url = url.rstrip("/")
self.session = requests.session()
self.uid = None
self.token = token
if not self.token:
response = self.api_post("/auth/init")
self.uid = response["uid"]
self.token = self.session.cookies["token"]
else:
self.session.cookies["token"] = self.token
response = self.api_get("/auth/me")
self.uid = response["uid"]
def api_get(self, path: str, **kwargs):
return self.api_request("GET", path, **kwargs)
def api_post(self, path: str, **kwargs):
return self.api_request("POST", path, **kwargs)
def api_delete(self, path: str, **kwargs):
return self.api_request("DELETE", path, **kwargs)
def api_request(self, method, path: str, **kwargs):
path = path.lstrip("/")
res = self.session.request(method, f"{self.url}/api/{path}", **kwargs)
res.raise_for_status()
return res.json()
def create_bin(
self,
method: str,
route: str,
content: str,
headers: dict = None,
status_code: int = None,
) -> tuple[str, bool]:
data = {"method": method, "route": route, "content": content}
if headers is not None:
data["headers"] = headers
if status_code is not None:
data["statusCode"] = status_code
response = self.api_post(
"/bins",
json=data,
)
return response["id"], response["inserted"]
def delete_bin(self, id: str):
self.api_delete(
f"/bins/{id}",
json={
"id": id,
},
)
def get_bin(self, id: str):
return self.api_get(
f"/bins/{id}",
)
def list_bins(self):
return self.api_get("/bins")
def logs(self, limit=100):
return self.api_get("/logs/", params={"limit": limit})
def logs_export(self):
return self.api_get("/logs/export")
def log(self, id: int):
return self.api_get(f"/logs/{id}")
def log_body(self, log_id: str) -> bytes:
res = self.session.get(f"{self.url}/api/logs/{log_id}/body")
res.raise_for_status()
return res.content
def reset_logs(self):
self.api_delete("/logs")
URL = "http://localhost:3000/"
rbin = RequestBin(URL)
print(f"==== Test new content ====")
content_id, inserted = rbin.create_bin("GET", "/test", "<h1>hello world!</h1>")
print(f"{inserted = }")
api_content = rbin.get_bin(content_id)
print(f"{api_content = }")
api_list = rbin.list_bins()
print(f"{api_list = }")
r = requests.get(f"{URL}/{rbin.uid}/test")
print(f"content = {r.text}")
print(f"status_code = {r.status_code}")
print(f"headers = {r.headers}")
print(f"==== Test add content ====")
content_id, _ = rbin.create_bin(
"POST", "/post", "posted!", {"X-additional": "test-header"}, status_code=201
)
api_list = rbin.list_bins()
print(f"{api_list = }")
r = requests.post(f"{URL}/{rbin.uid}/post")
print(f"content = {r.text}")
print(f"status_code = {r.status_code}")
print(f"headers = {r.headers}")
print(f"==== Test edit content ====")
content_id, inserted = rbin.create_bin(
"GET", "/test", "edited", {"Content-Type": "text/plain"}
)
print(f"inserted = {inserted}")
r = requests.get(f"{URL}/{rbin.uid}/test")
print(f"content = {r.text}")
print(f"headers = {r.headers}")
print(f"==== Test logs ====")
r = requests.post(
f"{URL}/{rbin.uid}/post", headers={"custom": "value"}, json={"post": "data"}
)
r = requests.post(
f"{URL}/{rbin.uid}/post",
headers={"custom": "value", "Content-Type": "application/octet-stream"},
data="こんにちは".encode("Shift-JIS"),
)
logs = rbin.logs()
print(f"{logs = }")
last_log = rbin.log(logs[-1]["id"])
print(f"{last_log = }")
body1 = rbin.log_body(logs[0]["id"])
print(f"{body1.decode("Shift-JIS") = }")
body1 = rbin.log_body(logs[1]["id"])
print(f"{body1.decode() = }")
exported = rbin.logs_export()
print(f"{exported = }")
print(f"==== Test reset logs ====")
rbin.reset_logs()
logs = rbin.logs()
print(f"{logs = }")
print(f"==== Test continue ====")
rbin2 = RequestBin(URL, token=rbin.token)
print(f"{rbin.uid = }")
print(f"{rbin2.uid = }")