From ba803f790011395273a3437c8abd3be3a59f656b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Riku=20Kestila=CC=88?= Date: Tue, 26 Nov 2024 13:23:22 +0200 Subject: [PATCH] chore: move requestHandler to own file --- .../management/commands/ahjo_base_command.py | 3 +- .../services/ahjo/request_handler.py | 28 ++++++++++++++++++ .../applications/services/ahjo_integration.py | 29 ++----------------- 3 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 backend/benefit/applications/services/ahjo/request_handler.py diff --git a/backend/benefit/applications/management/commands/ahjo_base_command.py b/backend/benefit/applications/management/commands/ahjo_base_command.py index 3eb9dbaa30..b07d8763c8 100644 --- a/backend/benefit/applications/management/commands/ahjo_base_command.py +++ b/backend/benefit/applications/management/commands/ahjo_base_command.py @@ -4,8 +4,9 @@ from django.core.management.base import BaseCommand from django.utils import timezone +from applications.services.ahjo.request_handler import AhjoRequestHandler from applications.services.ahjo_authentication import AhjoTokenExpiredException -from applications.services.ahjo_integration import AhjoRequestHandler, get_token +from applications.services.ahjo_integration import get_token LOGGER = logging.getLogger(__name__) diff --git a/backend/benefit/applications/services/ahjo/request_handler.py b/backend/benefit/applications/services/ahjo/request_handler.py new file mode 100644 index 0000000000..76e94f6159 --- /dev/null +++ b/backend/benefit/applications/services/ahjo/request_handler.py @@ -0,0 +1,28 @@ +from typing import List, Union + +from applications.enums import AhjoRequestType +from applications.services.ahjo.enums import AhjoSettingName +from applications.services.ahjo.setting_response_handler import AhjoResponseHandler +from applications.services.ahjo_authentication import AhjoToken +from applications.services.ahjo_client import ( + AhjoApiClient, + AhjoDecisionMakerRequest, + AhjoRequest, +) + + +class AhjoRequestHandler: + def __init__(self, ahjo_token: AhjoToken, ahjo_request_type: AhjoRequest): + self.ahjo_token = ahjo_token + self.ahjo_request_type = ahjo_request_type + + def handle_request_without_application(self): + if self.ahjo_request_type == AhjoRequestType.GET_DECISION_MAKER: + self.get_decision_maker_from_ahjo() + + def get_decision_maker_from_ahjo(self) -> Union[List, None]: + ahjo_client = AhjoApiClient(self.ahjo_token, AhjoDecisionMakerRequest()) + _, result = ahjo_client.send_request_to_ahjo() + AhjoResponseHandler.handle_ahjo_query_response( + setting_name=AhjoSettingName.DECISION_MAKER, data=result + ) diff --git a/backend/benefit/applications/services/ahjo_integration.py b/backend/benefit/applications/services/ahjo_integration.py index 4a041511e8..b4df349cc0 100644 --- a/backend/benefit/applications/services/ahjo_integration.py +++ b/backend/benefit/applications/services/ahjo_integration.py @@ -5,15 +5,12 @@ from collections import defaultdict from dataclasses import dataclass from io import BytesIO -from typing import Dict, List, Optional, Tuple, Union +from typing import List, Optional, Tuple, Union import jinja2 import pdfkit from django.conf import settings -from django.core.exceptions import ( - ImproperlyConfigured, - ObjectDoesNotExist, -) +from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist from django.core.files.base import ContentFile from django.db.models import QuerySet from django.urls import reverse @@ -21,7 +18,6 @@ from lxml.etree import XMLSchemaParseError, XMLSyntaxError from applications.enums import ( - AhjoRequestType, AhjoStatus as AhjoStatusEnum, ApplicationStatus, AttachmentType, @@ -33,7 +29,6 @@ ApplicationBatch, Attachment, ) -from applications.services.ahjo.enums import AhjoSettingName from applications.services.ahjo.exceptions import ( DecisionProposalAlreadyAcceptedError, DecisionProposalError, @@ -43,11 +38,9 @@ AhjoAddRecordsRequest, AhjoApiClient, AhjoDecisionDetailsRequest, - AhjoDecisionMakerRequest, AhjoDecisionProposalRequest, AhjoDeleteCaseRequest, AhjoOpenCaseRequest, - AhjoRequest, AhjoSubscribeDecisionRequest, AhjoUpdateRecordsRequest, ) @@ -68,7 +61,6 @@ ) from companies.models import Company -from applications.services.ahjo.setting_response_handler import AhjoResponseHandler @dataclass class ExportFileInfo: @@ -664,20 +656,3 @@ def get_decision_details_from_ahjo( ahjo_request = AhjoDecisionDetailsRequest(application) ahjo_client = AhjoApiClient(ahjo_token, ahjo_request) return ahjo_client.send_request_to_ahjo() - - -class AhjoRequestHandler: - def __init__(self, ahjo_token: AhjoToken, ahjo_request_type: AhjoRequest): - self.ahjo_token = ahjo_token - self.ahjo_request_type = ahjo_request_type - - def handle_request_without_application(self): - if self.ahjo_request_type == AhjoRequestType.GET_DECISION_MAKER: - self.get_decision_maker_from_ahjo() - - def get_decision_maker_from_ahjo(self) -> Union[List, None]: - ahjo_client = AhjoApiClient(self.ahjo_token, AhjoDecisionMakerRequest()) - _, result = ahjo_client.send_request_to_ahjo() - AhjoResponseHandler.handle_ahjo_query_response( - setting_name=AhjoSettingName.DECISION_MAKER, data=result - )