Skip to content

Commit

Permalink
Merge pull request #1233 from maykinmedia/issues/2514-concrete-openkl…
Browse files Browse the repository at this point in the history
…ant-client-factories

[#2514] Refactor generic openklant client factories to concrete equivalents
  • Loading branch information
alextreme authored May 31, 2024
2 parents 04e73af + 703c284 commit 95f207f
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 24 deletions.
11 changes: 7 additions & 4 deletions src/open_inwoner/accounts/views/contactmoments.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
from view_breadcrumbs import BaseBreadcrumbMixin

from open_inwoner.openklant.api_models import KlantContactMoment
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.clients import (
build_contactmomenten_client,
build_klanten_client,
)
from open_inwoner.openklant.constants import Status
from open_inwoner.openklant.models import ContactFormSubject, KlantContactMomentAnswer
from open_inwoner.openklant.views.contactform import ContactFormView
Expand Down Expand Up @@ -218,7 +221,7 @@ def get_context_data(self, **kwargs):
local_kcm.is_seen = True
local_kcm.save()

if client := build_client("contactmomenten"):
if client := build_contactmomenten_client():
zaken_client = build_zaken_client()
ocm = client.retrieve_objectcontactmoment(
kcm.contactmoment, "zaak", zaken_client
Expand Down Expand Up @@ -281,8 +284,8 @@ class KlantContactMomentRedirectView(KlantContactMomentAccessMixin, View):
"""

def get(self, request, *args, **kwargs):
klanten_client = build_client("klanten")
contactmoment_client = build_client("contactmomenten")
klanten_client = build_klanten_client()
contactmoment_client = build_contactmomenten_client()

klant = klanten_client.retrieve_klant(**get_fetch_parameters(self.request))
kcms = contactmoment_client.retrieve_klantcontactmomenten_for_klant(klant)
Expand Down
4 changes: 2 additions & 2 deletions src/open_inwoner/accounts/views/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from open_inwoner.haalcentraal.utils import fetch_brp
from open_inwoner.laposta.forms import NewsletterSubscriptionForm
from open_inwoner.laposta.models import LapostaConfig
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.clients import build_klanten_client
from open_inwoner.openklant.wrap import get_fetch_parameters
from open_inwoner.plans.models import Plan
from open_inwoner.qmatic.client import NoServiceConfigured, QmaticClient
Expand Down Expand Up @@ -239,7 +239,7 @@ def update_klant_api(self, user_form_data: dict):
if user_form_data.get(local_name)
}
if update_data:
if client := build_client("klanten"):
if client := build_klanten_client():
klant = client.retrieve_klant(**get_fetch_parameters(self.request))

if klant:
Expand Down
11 changes: 7 additions & 4 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
from zgw_consumers.api_models.constants import RolOmschrijving

from open_inwoner.mail.service import send_contact_confirmation_mail
from open_inwoner.openklant.clients import build_client as build_client_openklant
from open_inwoner.openklant.clients import (
build_contactmomenten_client,
build_klanten_client,
)
from open_inwoner.openklant.models import OpenKlantConfig
from open_inwoner.openklant.wrap import (
contactmoment_has_new_answer,
Expand Down Expand Up @@ -150,7 +153,7 @@ def get_context_data(self, **kwargs):
self.store_resulttype_mapping(self.case.zaaktype.identificatie)

objectcontactmomenten = []
if contactmoment_client := build_client_openklant("contactmomenten"):
if contactmoment_client := build_contactmomenten_client():
objectcontactmomenten = (
contactmoment_client.retrieve_objectcontactmomenten_for_zaak(
self.case
Expand Down Expand Up @@ -911,7 +914,7 @@ def register_by_api(self, form, config: OpenKlantConfig):
except ObjectDoesNotExist:
ztc = None

if klanten_client := build_client_openklant("klanten"):
if klanten_client := build_klanten_client():
klant = klanten_client.retrieve_klant(**get_fetch_parameters(self.request))

if klant:
Expand Down Expand Up @@ -959,7 +962,7 @@ def register_by_api(self, form, config: OpenKlantConfig):
if ztc and ztc.contact_subject_code:
data["onderwerp"] = ztc.contact_subject_code

if contactmoment_client := build_client_openklant("contactmomenten"):
if contactmoment_client := build_contactmomenten_client():
contactmoment = contactmoment_client.create_contactmoment(data, klant=klant)
if contactmoment:
self.log_system_action(
Expand Down
9 changes: 6 additions & 3 deletions src/open_inwoner/configurations/bootstrap/kic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from zgw_consumers.constants import APITypes, AuthTypes
from zgw_consumers.models import Service

from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.clients import (
build_contactmomenten_client,
build_klanten_client,
)
from open_inwoner.openklant.models import OpenKlantConfig
from open_inwoner.utils.api import ClientError

Expand Down Expand Up @@ -200,8 +203,8 @@ def test_configuration(self):
"""
make requests to the APIs and verify that a connection can be made
"""
klanten_client = build_client("klanten")
contactmoment_client = build_client("contactmomenten")
klanten_client = build_klanten_client()
contactmoment_client = build_contactmomenten_client()

try:
response = klanten_client.get(
Expand Down
14 changes: 11 additions & 3 deletions src/open_inwoner/openklant/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ape_pie.client import APIClient
from requests.exceptions import RequestException
from zgw_consumers.api_models.base import factory
from zgw_consumers.client import build_client as _build_client
from zgw_consumers.client import build_client
from zgw_consumers.utils import pagination_helper

from open_inwoner.openzaak.api_models import Zaak
Expand Down Expand Up @@ -311,7 +311,7 @@ def retrieve_objectcontactmoment(
return ocms[0]


def build_client(type_) -> APIClient | None:
def _build_open_klant_client(type_) -> APIClient | None:
config = OpenKlantConfig.get_solo()
services_to_client_mapping = {
"klanten": KlantenClient,
Expand All @@ -320,8 +320,16 @@ def build_client(type_) -> APIClient | None:
if client_class := services_to_client_mapping.get(type_):
service = getattr(config, f"{type_}_service")
if service:
client = _build_client(service, client_factory=client_class)
client = build_client(service, client_factory=client_class)
return client

logger.warning("no service defined for %s", type_)
return None


def build_contactmomenten_client() -> ContactmomentenClient | None:
return _build_open_klant_client("contactmomenten")


def build_klanten_client() -> KlantenClient | None:
return _build_open_klant_client("klanten")
4 changes: 2 additions & 2 deletions src/open_inwoner/openklant/services.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from open_inwoner.accounts.models import User
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.clients import build_klanten_client
from open_inwoner.utils.logentry import system_action

from .wrap import get_fetch_parameters
Expand All @@ -11,7 +11,7 @@ def update_user_from_klant(request):

user: User = request.user

client = build_client("klanten")
client = build_klanten_client()
if not client:
return

Expand Down
9 changes: 6 additions & 3 deletions src/open_inwoner/openklant/views/contactform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
from view_breadcrumbs import BaseBreadcrumbMixin

from open_inwoner.mail.service import send_contact_confirmation_mail
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.clients import (
build_contactmomenten_client,
build_klanten_client,
)
from open_inwoner.openklant.forms import ContactForm
from open_inwoner.openklant.models import OpenKlantConfig
from open_inwoner.openklant.wrap import get_fetch_parameters
Expand Down Expand Up @@ -149,7 +152,7 @@ def register_by_api(self, form, config: OpenKlantConfig) -> tuple[bool, str]:

# fetch/update/create klant
klant = None
if klanten_client := build_client("klanten"):
if klanten_client := build_klanten_client():
if self.request.user.is_authenticated and (
self.request.user.bsn or self.request.user.kvk
):
Expand Down Expand Up @@ -245,7 +248,7 @@ def register_by_api(self, form, config: OpenKlantConfig) -> tuple[bool, str]:
data["medewerkerIdentificatie"] = {"identificatie": employee_id}

contactmoment = None
if contactmoment_client := build_client("contactmomenten"):
if contactmoment_client := build_contactmomenten_client():
contactmoment = contactmoment_client.create_contactmoment(data, klant=klant)

if contactmoment:
Expand Down
9 changes: 6 additions & 3 deletions src/open_inwoner/openklant/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from open_inwoner.accounts.models import User
from open_inwoner.kvk.branches import get_kvk_branch_number
from open_inwoner.openklant.api_models import ContactMoment, KlantContactMoment
from open_inwoner.openklant.clients import build_client
from open_inwoner.openklant.clients import (
build_contactmomenten_client,
build_klanten_client,
)
from open_inwoner.openklant.models import KlantContactMomentAnswer, OpenKlantConfig
from open_inwoner.utils.time import instance_is_new

Expand All @@ -21,7 +24,7 @@ def fetch_klantcontactmomenten(
if not user_bsn and not user_kvk_or_rsin:
return []

client = build_client("klanten")
client = build_klanten_client()
if client is None:
return []

Expand All @@ -35,7 +38,7 @@ def fetch_klantcontactmomenten(
if klanten is None:
return []

client = build_client("contactmomenten")
client = build_contactmomenten_client()
if client is None:
return []

Expand Down

0 comments on commit 95f207f

Please sign in to comment.