11
11
12
12
class LambdaService (QObject ):
13
13
jsons_received = pyqtSignal (dict , dict )
14
+ validation_received = pyqtSignal (dict )
15
+ ActionAttribute = QNetworkRequest .User + 1
16
+ ACTION_VALIDATE_PLANS = "validate_plans"
17
+ ACTION_GET_PLANS = "get_plans"
14
18
15
19
def __init__ (self ):
16
- super ().__init__ () # Ensure QObject initialization
17
- # Init network manager
20
+ super ().__init__ ()
18
21
self .network_manager = QNetworkAccessManager ()
22
+ self .network_manager .finished .connect (self ._handle_reply )
19
23
20
- def send_request (self , action : str , plan_id : str ):
21
- """Sends a request to the lambda function."""
24
+ def serialize_plan (self , plan_id : str ):
25
+ self ._send_request (action = self .ACTION_GET_PLANS , plan_id = plan_id )
26
+
27
+ def validate_plan (self , plan_id : str ):
28
+ self ._send_request (action = self .ACTION_VALIDATE_PLANS , plan_id = plan_id )
22
29
30
+ def _send_request (self , action : str , plan_id : str ):
31
+ """Sends a request to the lambda function."""
23
32
proxy_host , proxy_port , self .lambda_url = get_settings ()
24
33
25
34
# Initialize or reset proxy each time a request is sent. Incase settings have changed.
@@ -35,16 +44,48 @@ def send_request(self, action: str, plan_id: str):
35
44
36
45
payload = {"action" : action , "plan_uuid" : plan_id }
37
46
payload_bytes = QByteArray (json .dumps (payload ).encode ("utf-8" ))
38
-
39
47
request = QNetworkRequest (QUrl (self .lambda_url ))
48
+ request .setAttribute (LambdaService .ActionAttribute , action )
40
49
request .setHeader (QNetworkRequest .ContentTypeHeader , "application/json" )
50
+ self .network_manager .post (request , payload_bytes )
51
+
52
+ def _handle_reply (self , reply : QNetworkReply ):
53
+ action = reply .request ().attribute (LambdaService .ActionAttribute )
54
+ if action == self .ACTION_GET_PLANS :
55
+ self ._process_json_reply (reply )
56
+ elif action == self .ACTION_VALIDATE_PLANS :
57
+ self ._process_validation_reply (reply )
41
58
42
- reply = self .network_manager .post (request , payload_bytes )
59
+ def _process_validation_reply (self , reply : QNetworkReply ):
60
+ """Processes the validation reply from the lambda and emits a signal."""
61
+ if reply .error () != QNetworkReply .NoError :
62
+ error_string = reply .errorString ()
63
+ QMessageBox .critical (None , "API Error" , f"Lambda call failed: { error_string } " )
64
+ reply .deleteLater ()
65
+ return
43
66
44
- # Connect reply signal to handle the response
45
- reply .finished .connect (lambda : self ._process_reply (reply ))
67
+ try :
68
+ response_data = reply .readAll ().data ().decode ("utf-8" )
69
+ response_json = json .loads (response_data )
70
+
71
+ # Determine if the proxy is set up.
72
+ if hasattr (self , "network_manager" ) and self .network_manager .proxy ().type () == QNetworkProxy .Socks5Proxy :
73
+ # If proxy has been set up, retrieve 'ryhti_responses' directly
74
+ validation_errors = response_json .get ("ryhti_responses" , {})
75
+ else :
76
+ # If proxy has not been set up (using local docker lambda), the response includes 'body'.
77
+ # In this case we need to retrieve 'ryhti_responses' from 'body' first.
78
+ body = response_json .get ("body" , {})
79
+ validation_errors = body .get ("ryhti_responses" , {})
80
+
81
+ self .validation_received .emit (validation_errors )
82
+
83
+ except json .JSONDecodeError as e :
84
+ QMessageBox .critical (None , "JSON Error" , f"Failed to parse response JSON: { e } " )
85
+ finally :
86
+ reply .deleteLater ()
46
87
47
- def _process_reply (self , reply : QNetworkReply ):
88
+ def _process_json_reply (self , reply : QNetworkReply ):
48
89
"""Processes the reply from the lambda and emits signal."""
49
90
plan_id = get_active_plan_id ()
50
91
0 commit comments