3
3
import json
4
4
import re
5
5
from http import HTTPStatus
6
- from typing import cast
6
+ from typing import Callable , cast
7
7
8
8
from qgis .PyQt .QtCore import QByteArray , QObject , QUrl , pyqtSignal
9
9
from qgis .PyQt .QtNetwork import QNetworkAccessManager , QNetworkProxy , QNetworkReply , QNetworkRequest
@@ -58,13 +58,28 @@ def _is_api_gateway_request(self) -> bool:
58
58
match = re .match (r"^https://.*execute-api.*amazonaws\.com.*$" , self .lambda_url )
59
59
return bool (match )
60
60
61
+ def _get_response_handler (self , action : str ) -> Callable [[dict ], None ]:
62
+ handlers = {
63
+ self .ACTION_GET_PLANS : self ._process_json_reply ,
64
+ self .ACTION_VALIDATE_PLANS : self ._process_validation_reply ,
65
+ }
66
+ return handlers [action ]
67
+
68
+ def _get_error_handler (self , action : str ) -> Callable [[], None ]:
69
+ handlers = {
70
+ self .ACTION_GET_PLANS : lambda : None ,
71
+ self .ACTION_VALIDATE_PLANS : self ._handle_validation_error ,
72
+ }
73
+ return handlers [action ]
74
+
61
75
def _handle_reply (self , reply : QNetworkReply ):
62
76
action = reply .request ().attribute (LambdaService .ActionAttribute )
77
+ response_handler = self ._get_response_handler (action )
78
+ error_handler = self ._get_error_handler (action )
63
79
if reply .error () != QNetworkReply .NoError :
64
80
error = reply .errorString ()
65
81
QMessageBox .critical (None , "API Virhe" , f"Lambda kutsu epäonnistui: { error } " )
66
- if action == self .ACTION_VALIDATE_PLANS :
67
- self .validation_failed .emit ()
82
+ error_handler ()
68
83
reply .deleteLater ()
69
84
return
70
85
@@ -77,8 +92,7 @@ def _handle_reply(self, reply: QNetworkReply):
77
92
if int (response_json .get ("statusCode" , 0 )) != HTTPStatus .OK :
78
93
error = response_json ["body" ] if "body" in response_json else response_json ["errorMessage" ]
79
94
QMessageBox .critical (None , "API Virhe" , f"Lambda kutsu epäonnistui: { error } " )
80
- if action == self .ACTION_VALIDATE_PLANS :
81
- self .validation_failed .emit ()
95
+ error_handler ()
82
96
reply .deleteLater ()
83
97
return
84
98
body = response_json ["body" ]
@@ -87,16 +101,14 @@ def _handle_reply(self, reply: QNetworkReply):
87
101
88
102
except (json .JSONDecodeError , KeyError ) as e :
89
103
QMessageBox .critical (None , "JSON Virhe" , f"Vastauksen JSON-tiedoston jäsennys epäonnistui: { e } " )
90
- if action == self .ACTION_VALIDATE_PLANS :
91
- self .validation_failed .emit ()
104
+ error_handler ()
92
105
return
93
106
finally :
94
107
reply .deleteLater ()
108
+ response_handler (body )
95
109
96
- if action == self .ACTION_GET_PLANS :
97
- self ._process_json_reply (body )
98
- elif action == self .ACTION_VALIDATE_PLANS :
99
- self ._process_validation_reply (body )
110
+ def _handle_validation_error (self ):
111
+ self .validation_failed .emit ()
100
112
101
113
def _process_validation_reply (self , response_json : dict ):
102
114
"""Processes the validation reply from the lambda and emits a signal."""
0 commit comments