-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
226 lines (184 loc) · 7.76 KB
/
Copy pathmain.py
File metadata and controls
226 lines (184 loc) · 7.76 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import dearpygui.dearpygui as dpg
import os, platform
from http import HTTPStatus
import urllib3, webbrowser
import json
import time
def update_history():
try:
with open("data.json", "r", encoding="UTF-8") as file:
data = json.load(file)
except:
data = {}
for i in range(5):
url = data.get(f"url{i+1}", "")
text_id, btn_id = history_items[i]
dpg.set_value(text_id, url)
dpg.set_item_user_data(btn_id, url)
def copy(sender, app_data, user_data):
dpg.set_clipboard_text(user_data)
def add_query_params(sender, app_data):
key = dpg.get_value(qp_key)
value = dpg.get_value(qp_value)
if key != "" and value != "":
if "?" in dpg.get_value(url_input):
dpg.set_value(url_input, dpg.get_value(url_input) + "&" + key + "=" + value)
else:
dpg.set_value(url_input, dpg.get_value(url_input) + "?" + key + "=" + value)
dpg.set_value(qp_key, "")
dpg.set_value(qp_value, "")
else:
pass
def get_font_path():
system = platform.system()
if system == "Windows":
possible_paths = [
"C:/Windows/Fonts/arial.ttf",
"C:/Windows/Fonts/times.ttf",
"C:/Windows/Fonts/calibri.ttf",
"C:/Windows/Fonts/verdana.ttf",
"C:/Windows/Fonts/consola.ttf",
"C:/Windows/Fonts/tahoma.ttf"
]
for path in possible_paths:
if os.path.exists(path):
return path
elif system == "Linux":
possible_paths = [
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
"/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf",
"/usr/share/fonts/truetype/freefont/FreeSans.ttf",
"/usr/share/fonts/truetype/freefont/FreeSerif.ttf",
"/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
"/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf"
]
for path in possible_paths:
if os.path.exists(path):
return path
elif system == "Darwin":
possible_paths = [
"/System/Library/Fonts/SFNS.ttf",
"/System/Library/Fonts/Helvetica.ttc",
"/System/Library/Fonts/Times.ttc",
"/System/Library/Fonts/Verdana.ttf"
]
for path in possible_paths:
if os.path.exists(path):
return path
raise FileNotFoundError("Could not find font in standard directory.")
def save_callback(sender, app_data, user_data):
response, exec_time, type = user_data
html = f"""
Request time: {exec_time} seconds
Status:
{str(response.status)} {str(HTTPStatus(response.status).phrase)}
Output:
{json.dumps(json.loads(response.data.decode("UTF-8")), indent=4)}
"""
if type == "txt":
path = "output.txt"
elif type == "html":
path = "output.html"
with open(path, "w", encoding="UTF-8") as f:
f.write(html)
webbrowser.open('file://' + os.path.realpath(path))
def callback(sender, app_data):
start = time.perf_counter()
http = urllib3.PoolManager()
method = dpg.get_value(method_combo)
scheme = dpg.get_value(scheme_combo)
url_value = dpg.get_value(url_input)
json_value = "{" + dpg.get_value(json_input) + "}"
token_value = dpg.get_value(token_input)
bodyjs = json.dumps(json_value).encode("UTF-8")
headers = {
"Authorization": f"Bearer {token_value}",
"Content-Type": "application/json"
}
response = http.request(method, url_value, body=bodyjs, headers=headers)
end = time.perf_counter()
exec_time = f"{end - start:.6f}"
parsed_json = json.dumps(json.loads(response.data.decode("UTF-8")), indent=4)
with dpg.window(label="Output", width=700, height=600):
with dpg.group(horizontal=True):
dpg.add_button(label="Save request as txt", width=200, height=40, callback=save_callback, user_data=(response, exec_time, "txt"))
dpg.add_button(label="Save request as html", width=200, height=40, callback=save_callback, user_data=(response, exec_time, "html"))
dpg.add_text(f"Request time: {exec_time} seconds")
dpg.add_text("")
dpg.add_text("Status:")
dpg.add_text(str(response.status) + " " + str(HTTPStatus(response.status).phrase))
dpg.add_text("")
with dpg.group(horizontal=True):
dpg.add_text("Output:")
dpg.add_button(label="Copy", width=100, height=30, callback=copy, user_data=(parsed_json))
dpg.add_text(parsed_json, wrap=690)
def url_history(new_url):
try:
with open("data.json", "r", encoding="UTF-8") as file:
data = json.load(file)
except:
data = {"url1": "", "url2": "", "url3": "", "url4": "", "url5": ""}
data["url5"] = data.get("url4", "")
data["url4"] = data.get("url3", "")
data["url3"] = data.get("url2", "")
data["url2"] = data.get("url1", "")
data["url1"] = new_url
with open("data.json", "w", encoding="UTF-8") as file:
json.dump(data, file, ensure_ascii=False, indent=4)
if scheme == "None":
url_history(f"{str(url_value)}")
else:
url_history(f"{scheme + str(url_value)}")
update_history()
history_items = []
dpg.create_context()
dpg.create_viewport(title='LazyReq', width=700, height=600, resizable=False)
dpg.setup_dearpygui()
with dpg.font_registry():
with dpg.font(get_font_path(), 20) as font:
dpg.add_font_range_hint(dpg.mvFontRangeHint_Default)
dpg.add_font_range_hint(dpg.mvFontRangeHint_Cyrillic)
with dpg.window(label="LazyReq", tag="Primary Window"):
dpg.add_button(label="Make a request", width=685, height=30, callback=callback)
with dpg.tab_bar():
with dpg.tab(label="Main"):
with dpg.group(horizontal=True):
method_combo = dpg.add_combo(
items=["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"],
default_value="GET",
width=100
)
scheme_combo = dpg.add_combo(
items=[None, "http://", "https://"],
default_value=None,
width=100
)
url_input = dpg.add_input_text(width=400, label="API url")
dpg.add_spacer(height=200)
dpg.add_text("History")
for i in range(5):
with dpg.group(horizontal=True):
btn_id = dpg.add_button(label="Copy", width=100, height=30, callback=copy, user_data=(""))
text_id = dpg.add_text("")
history_items.append((text_id, btn_id))
with dpg.tab(label="Data"):
with dpg.tab_bar():
with dpg.tab(label="Query params"):
with dpg.group(horizontal=True):
dpg.add_text("Key")
dpg.add_spacer(width=260)
dpg.add_text("Value")
with dpg.group(horizontal=True):
qp_key = dpg.add_input_text(width=300, height=50,)
qp_value = dpg.add_input_text(width=300, height=50)
dpg.add_button(label="Add query params", width=200, height=50, callback=add_query_params)
with dpg.tab(label="JSON"):
json_input = dpg.add_input_text(width=600, height=480, multiline=True)
with dpg.tab(label="Authorization"):
token_input = dpg.add_input_text(width=300, label="Authorization bearer token")
update_history()
dpg.bind_font(font)
dpg.set_primary_window("Primary Window", True)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()