Skip to content

Commit 4db772f

Browse files
committed
Refactor
1 parent 4e55234 commit 4db772f

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

arho_feature_template/core/lambda_service.py

+23-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import re
55
from http import HTTPStatus
6-
from typing import cast
6+
from typing import Callable, cast
77

88
from qgis.PyQt.QtCore import QByteArray, QObject, QUrl, pyqtSignal
99
from qgis.PyQt.QtNetwork import QNetworkAccessManager, QNetworkProxy, QNetworkReply, QNetworkRequest
@@ -58,13 +58,28 @@ def _is_api_gateway_request(self) -> bool:
5858
match = re.match(r"^https://.*execute-api.*amazonaws\.com.*$", self.lambda_url)
5959
return bool(match)
6060

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+
6175
def _handle_reply(self, reply: QNetworkReply):
6276
action = reply.request().attribute(LambdaService.ActionAttribute)
77+
response_handler = self._get_response_handler(action)
78+
error_handler = self._get_error_handler(action)
6379
if reply.error() != QNetworkReply.NoError:
6480
error = reply.errorString()
6581
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()
6883
reply.deleteLater()
6984
return
7085

@@ -77,8 +92,7 @@ def _handle_reply(self, reply: QNetworkReply):
7792
if int(response_json.get("statusCode", 0)) != HTTPStatus.OK:
7893
error = response_json["body"] if "body" in response_json else response_json["errorMessage"]
7994
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()
8296
reply.deleteLater()
8397
return
8498
body = response_json["body"]
@@ -87,16 +101,14 @@ def _handle_reply(self, reply: QNetworkReply):
87101

88102
except (json.JSONDecodeError, KeyError) as e:
89103
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()
92105
return
93106
finally:
94107
reply.deleteLater()
108+
response_handler(body)
95109

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()
100112

101113
def _process_validation_reply(self, response_json: dict):
102114
"""Processes the validation reply from the lambda and emits a signal."""

0 commit comments

Comments
 (0)