Skip to content

Commit b3855ed

Browse files
authored
Merge pull request #1648 from maykinmedia/more-klanten-service-typing-fixes
Improve klanten service typing
2 parents aa306e5 + 0a3b56d commit b3855ed

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

src/open_inwoner/accounts/views/contactmoments.py

+4
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,15 @@ def get_anchors(self) -> list:
196196
def get_context_data(self, **kwargs):
197197
ctx = super().get_context_data(**kwargs)
198198

199+
service: VragenService | None = None
199200
if KlantenServiceType.ESUITE.value in self.request.path:
200201
service = self.get_service(service_type=KlantenServiceType.ESUITE)
201202
elif KlantenServiceType.OPENKLANT2.value in self.request.path:
202203
service = self.get_service(service_type=KlantenServiceType.OPENKLANT2)
203204

205+
if not service:
206+
raise ImproperlyConfigured("Unknown KlantenServiceType")
207+
204208
origin = self.request.headers.get("Referer")
205209
question, zaak_with_api_group = service.retrieve_question(
206210
self.get_fetch_params(service),

src/open_inwoner/cms/cases/views/status.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ def _register_via_esuite(self, form, config: ESuiteKlantConfig):
10751075

10761076
try:
10771077
service = eSuiteVragenService(config=config)
1078-
except RuntimeError:
1078+
except ImproperlyConfigured:
10791079
logger.error("Failed to build eSuiteVragenService")
10801080
return
10811081

src/open_inwoner/openklant/services.py

+34-18
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@
33
import uuid
44
from dataclasses import dataclass
55
from datetime import timedelta
6-
from typing import ClassVar, Iterable, Literal, NotRequired, Protocol, Self, Sequence
6+
from typing import (
7+
ClassVar,
8+
Iterable,
9+
Literal,
10+
NotRequired,
11+
Protocol,
12+
Self,
13+
Sequence,
14+
cast,
15+
)
716

817
from django.conf import settings
918
from django.core.exceptions import ImproperlyConfigured
19+
from django.http import HttpRequest
1020
from django.urls import reverse
1121
from django.utils import timezone
1222
from django.utils.translation import gettext_lazy as _
@@ -102,10 +112,11 @@ class Question(TypedDict):
102112

103113
class KlantenService(Protocol):
104114
service_config: ServiceConfig
115+
supports_anonymous_questions: ClassVar[bool]
105116

106117
def get_fetch_parameters(
107118
self,
108-
request=None,
119+
request: HttpRequest | None = None,
109120
user: User | None = None,
110121
use_vestigingsnummer: bool = False,
111122
) -> FetchParameters | None:
@@ -125,10 +136,10 @@ def get_fetch_parameters(
125136
kvk_or_rsin = user.rsin
126137

127138
if use_vestigingsnummer:
128-
if not getattr(request, "session", None):
139+
if not (session := getattr(request, "session", None)):
129140
raise ValueError("`request` does not contain a session")
130141

131-
vestigingsnummer = get_kvk_branch_number(request.session)
142+
vestigingsnummer = get_kvk_branch_number(session)
132143
if vestigingsnummer:
133144
return {
134145
"user_kvk_or_rsin": kvk_or_rsin,
@@ -139,25 +150,25 @@ def get_fetch_parameters(
139150

140151
return None
141152

142-
supports_anonymous_questions: ClassVar[bool]
143153

144-
145-
class eSuiteKlantenService(KlantenService):
154+
class eSuiteKlantenService(
155+
KlantenService,
156+
):
146157
config: ESuiteKlantConfig
147158
client: APIClient
148159
service_config: ServiceConfig
149160
supports_anonymous_questions = True
150161

151162
def __init__(self, config: ESuiteKlantConfig | None = None):
152-
self.config = config or ESuiteKlantConfig.get_solo()
163+
self.config = cast(ESuiteKlantConfig, config or ESuiteKlantConfig.get_solo())
153164
if not self.config:
154165
raise ImproperlyConfigured(
155166
"eSuiteKlantenService instance needs a configuration"
156167
)
157168

158169
if not self.config.klanten_service:
159170
raise ImproperlyConfigured(
160-
"eSuiteKlantenService instance needs a servivce configuration"
171+
"eSuiteKlantenService instance needs a service configuration"
161172
)
162173

163174
self.service_config = self.config.klanten_service
@@ -407,17 +418,19 @@ class eSuiteVragenService(KlantenService):
407418
def __init__(self, config: ESuiteKlantConfig | None = None):
408419
self.config = config or ESuiteKlantConfig.get_solo()
409420
if not self.config:
410-
raise RuntimeError("eSuiteVragenService instance needs a configuration")
421+
raise ImproperlyConfigured(
422+
"eSuiteVragenService instance needs a configuration"
423+
)
411424

412425
self.service_config = self.config.contactmomenten_service
413426
if not self.service_config:
414-
raise RuntimeError(
427+
raise ImproperlyConfigured(
415428
"eSuiteVragenService instance needs a servivce configuration"
416429
)
417430

418431
self.client = build_zgw_client(service=self.service_config)
419432
if not self.client:
420-
raise RuntimeError("eSuiteVragenService instance needs a client")
433+
raise ImproperlyConfigured("eSuiteVragenService instance needs a client")
421434

422435
#
423436
# contactmomenten
@@ -765,9 +778,9 @@ def retrieve_question(
765778
def list_questions_for_zaak(self, zaak: Zaak, user: User) -> list[Question]:
766779
objectcontactmomenten = self.retrieve_objectcontactmomenten_for_zaak(zaak)
767780

768-
contactmomenten = []
781+
contactmomenten: list[ContactMoment] = []
769782
for ocm in objectcontactmomenten:
770-
question = getattr(ocm, "contactmoment", None)
783+
question: ContactMoment | None = getattr(ocm, "contactmoment", None)
771784
if question:
772785
contactmomenten.append(question)
773786
contactmomenten.sort(key=lambda q: q.registratiedatum, reverse=True)
@@ -780,13 +793,13 @@ def list_questions_for_zaak(self, zaak: Zaak, user: User) -> list[Question]:
780793
]
781794

782795
kcm_answer_mapping = get_kcm_answer_mapping(contactmomenten, user)
783-
questions = []
796+
questions: list[Question] = []
784797
for contactmoment in contactmomenten:
785798
new_answer_available = contactmoment_has_new_answer(
786799
contactmoment, kcm_answer_mapping
787800
)
788801
questions.append(
789-
Question(
802+
dict(
790803
identification=contactmoment.identificatie,
791804
api_source_url=contactmoment.url,
792805
api_source_uuid=contactmoment.uuid,
@@ -870,7 +883,10 @@ def from_klantcontact_and_answer(
870883
)
871884

872885

873-
class OpenKlant2Service(LogMixin, KlantenService):
886+
class OpenKlant2Service(
887+
LogMixin,
888+
KlantenService,
889+
):
874890
config: OpenKlant2Config
875891
client: OpenKlant2Client
876892
supports_anonymous_questions = False
@@ -1439,7 +1455,7 @@ def retrieve_question(
14391455
fetch_params: FetchParameters,
14401456
question_uuid: str,
14411457
user: User,
1442-
) -> tuple[Question | None, Zaak | None]: # noqa: E704
1458+
) -> tuple[Question | None, ZaakWithApiGroup | None]: # noqa: E704
14431459
if bsn := fetch_params.get("user_bsn"):
14441460
partij = self.find_persoon_for_bsn(bsn)
14451461
elif kvk_or_rsin := fetch_params.get("user_kvk_or_rsin"):

0 commit comments

Comments
 (0)