11import json
22
33from PySide6 .QtCore import QTimer
4- from PySide6 .QtWidgets import QWidget , QVBoxLayout , QHBoxLayout , QLabel , QLineEdit , QComboBox , QTabWidget , QFormLayout , \
5- QPushButton , QTextEdit
4+ from PySide6 .QtWidgets import (
5+ QWidget , QVBoxLayout , QHBoxLayout , QLabel , QLineEdit ,
6+ QComboBox , QTabWidget , QFormLayout , QPushButton , QTextEdit
7+ )
68
79from je_api_testka .gui .api_testka_gui_thread import APITestkaGUIThread
810from je_api_testka .gui .language_wrapper .multi_language_wrapper import language_wrapper
911from je_api_testka .gui .message_queue import api_testka_ui_queue
1012
1113
1214class APITestkaWidget (QWidget ):
15+ """
16+ API Testka 主視窗元件
17+ Main widget for API Testka GUI
18+ """
1319
1420 def __init__ (self , parent = None ):
1521 super ().__init__ (parent )
1622
23+ # 建立主版面配置
24+ # Create main layout
1725 main_layout = QVBoxLayout ()
18- # URL row
26+
27+ # -------------------------------
28+ # URL 與 Method 選擇區
29+ # URL and HTTP Method selection
30+ # -------------------------------
1931 url_layout = QHBoxLayout ()
2032 url_layout .addWidget (QLabel (language_wrapper .language_word_dict .get ("url" )))
33+
2134 self .url_input = QLineEdit ()
2235 url_layout .addWidget (self .url_input )
36+
2337 self .method_combobox = QComboBox ()
2438 self .method_combobox .addItems ([
2539 language_wrapper .language_word_dict .get ("get" ),
@@ -33,60 +47,86 @@ def __init__(self, parent=None):
3347 url_layout .addWidget (self .method_combobox )
3448 main_layout .addLayout (url_layout )
3549
50+ # -------------------------------
3651 # Tabs for Param, Auth, Header, Body
52+ # -------------------------------
3753 self .tabs = QTabWidget ()
3854 self .param_tab = QWidget ()
3955 self .auth_tab = QWidget ()
4056 self .header_tab = QWidget ()
4157 self .body_tab = QWidget ()
58+
4259 self .tabs .addTab (self .param_tab , language_wrapper .language_word_dict .get ("param" ))
60+ # 可依需求開啟其他 Tab
61+ # Uncomment if needed
4362 # self.tabs.addTab(self.auth_tab, "Auth")
4463 # self.tabs.addTab(self.header_tab, "Header")
4564 # self.tabs.addTab(self.body_tab, "Body")
4665
47- layout = QFormLayout ()
66+ # Param Tab layout
67+ param_layout = QFormLayout ()
4868 self .param_input = QLineEdit ()
49- layout .addRow (language_wrapper .language_word_dict .get ("param" ), self .param_input )
50- self .param_tab .setLayout (layout )
69+ param_layout .addRow (language_wrapper .language_word_dict .get ("param" ), self .param_input )
70+ self .param_tab .setLayout (param_layout )
5171
5272 main_layout .addWidget (self .tabs )
5373
54- # Send button
74+ # -------------------------------
75+ # Send Button
76+ # -------------------------------
5577 self .send_button = QPushButton (language_wrapper .language_word_dict .get ("send" ))
5678 self .send_button .clicked .connect (self .start_api_request )
5779 main_layout .addWidget (self .send_button )
5880
59- # Log panel
81+ # -------------------------------
82+ # Log Panel
83+ # -------------------------------
6084 self .log_panel = QTextEdit ()
6185 self .log_panel .setReadOnly (True )
6286 main_layout .addWidget (QLabel (language_wrapper .language_word_dict .get ("response_and_log" )))
6387 main_layout .addWidget (self .log_panel )
6488
65- # Param
66- self .test_api_thread = None
89+ # -------------------------------
90+ # Thread + Timer
91+ # -------------------------------
92+ self .test_api_thread : APITestkaGUIThread | None = None
6793 self .pull_log_timer = QTimer ()
68- self .pull_log_timer .setInterval (20 )
94+ self .pull_log_timer .setInterval (20 ) # 每 20ms 檢查一次佇列
6995 self .pull_log_timer .timeout .connect (self .pull_log )
7096
7197 self .setLayout (main_layout )
7298
7399 def start_api_request (self ):
100+ """
101+ 開始 API 測試請求
102+ Start API request in a separate thread
103+ """
74104 self .log_panel .clear ()
75105 self .pull_log_timer .stop ()
76106 self .pull_log_timer .start ()
107+
77108 self .test_api_thread = APITestkaGUIThread ()
78109 self .test_api_thread .url = self .url_input .text ()
79110 self .test_api_thread .test_method = self .method_combobox .currentText ().lower ()
80- param = self .param_input .text ()
81- if param .strip () != "" :
111+
112+ # 嘗試解析 JSON 參數
113+ # Try to parse JSON parameters
114+ param_text = self .param_input .text ().strip ()
115+ if param_text :
82116 try :
83- self .test_api_thread .param = json .loads (param )
84- except json .decoder .JSONDecodeError :
85- pass
86- self .test_api_thread .start ()
117+ self .test_api_thread .param = json .loads (param_text )
118+ except json .JSONDecodeError :
119+ # 若 JSON 格式錯誤,忽略並繼續
120+ # Ignore invalid JSON and continue
121+ self .test_api_thread .param = None
87122
123+ self .test_api_thread .start ()
88124
89125 def pull_log (self ):
90- if not api_testka_ui_queue .empty ():
126+ """
127+ 從佇列中拉取訊息並顯示在 Log Panel
128+ Pull messages from queue and display in log panel
129+ """
130+ while not api_testka_ui_queue .empty ():
91131 message = api_testka_ui_queue .get_nowait ()
92- self .log_panel .append (str (message ))
132+ self .log_panel .append (str (message ))
0 commit comments