Skip to content

Commit

Permalink
Merge pull request #3503 from open-formulieren/refactor/3489-api-clie…
Browse files Browse the repository at this point in the history
…nt-implementations-tris

Refactor Haal Centraal BRP clients
  • Loading branch information
sergei-maertens authored Sep 25, 2023
2 parents d74fdf2 + 43fb4a2 commit 220fb42
Show file tree
Hide file tree
Showing 64 changed files with 1,317 additions and 3,117 deletions.
7 changes: 4 additions & 3 deletions src/openforms/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,11 @@
"openforms.ui",
"openforms.submissions",
"openforms.logging.apps.LoggingAppConfig",
"openforms.contrib.bag.apps.BAGConfig", # TODO: remove once 2.4.0 is released
"openforms.contrib.brp",
"openforms.contrib.haal_centraal",
"openforms.contrib.kadaster",
"openforms.contrib.kvk",
"openforms.contrib.bag.apps.BAGConfig", # TODO: remove once 2.4.0 is released
"openforms.contrib.microsoft.apps.MicrosoftApp",
"openforms.dmn",
"openforms.dmn.contrib.camunda",
Expand All @@ -227,7 +228,7 @@
"openforms.prefill.contrib.demo.apps.DemoApp",
"openforms.prefill.contrib.kvk.apps.KVKPrefillApp",
"openforms.prefill.contrib.stufbg.apps.StufBgApp",
"openforms.prefill.contrib.haalcentraal.apps.HaalCentraalApp",
"openforms.prefill.contrib.haalcentraal_brp.apps.HaalCentraalBRPApp",
"openforms.authentication",
"openforms.authentication.contrib.demo.apps.DemoApp",
"openforms.authentication.contrib.outage.apps.DemoOutageApp",
Expand Down Expand Up @@ -929,7 +930,7 @@
os.path.join(
BASE_DIR, "src/openforms/registrations/contrib/objects_api/tests/files"
),
os.path.join(BASE_DIR, "src/openforms/prefill/contrib/haalcentraal/tests/files"),
os.path.join(BASE_DIR, "src/openforms/contrib/haal_centraal/tests/files"),
]

#
Expand Down
4 changes: 4 additions & 0 deletions src/openforms/contrib/brp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
This Django app is obsolete. The code is left here so that migrations can run,
but after Open Forms 2.4.0 this package will be removed completely.
"""
10 changes: 0 additions & 10 deletions src/openforms/contrib/brp/admin.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Generated by Django 3.2.21 on 2023-09-23 21:02

from django.db import migrations

from openforms.contrib.haal_centraal.constants import BRPVersions


def copy_config_to_hc(apps, _):
"""
If there was no HC prefill used, but BRPConfig did have a configuration, we can
use that configuration to set up HC.
"""
HaalCentraalConfig = apps.get_model("haalcentraal", "HaalCentraalConfig")
BRPConfig = apps.get_model("brp", "BRPConfig")
hc_config, _ = HaalCentraalConfig.objects.get_or_create(pk=1)

# already configured, nothing to do
if hc_config.brp_personen_service:
return

brp_config, _ = BRPConfig.objects.get_or_create(pk=1)
if not brp_config.brp_service:
return

# only v1 was functional/supported, so we can make this explicit
hc_config.brp_personen_service = brp_config.brp_service
hc_config.brp_personen_version = BRPVersions.v13
hc_config.config()


def copy_config_from_hc(apps, _):
"""
Copy the config back from HC but only if it's V1 - v2 was not supported at the
time these migrations were added.
"""
HaalCentraalConfig = apps.get_model("haalcentraal", "HaalCentraalConfig")
BRPConfig = apps.get_model("brp", "BRPConfig")
hc_config = HaalCentraalConfig.objects.first()
if not hc_config:
return

if (
not hc_config.brp_personen_service
or hc_config.brp_personen_version != BRPVersions.v13
):
return

brp_config, _ = BRPConfig.objects.get_or_create(pk=1)
if brp_config.brp_service:
return

brp_config.brp_service = hc_config.brp_personen_service
brp_config.save()


class Migration(migrations.Migration):

dependencies = [
("brp", "0002_nice_verbose_name"),
("haalcentraal", "0002_copy_config_from_prefill"),
]

operations = [
migrations.RunPython(copy_config_to_hc, copy_config_from_hc),
]
16 changes: 16 additions & 0 deletions src/openforms/contrib/brp/migrations/0004_delete_brpconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 3.2.21 on 2023-09-23 21:12

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("brp", "0003_copy_config_to_haalcentraal"),
]

operations = [
migrations.DeleteModel(
name="BRPConfig",
),
]
43 changes: 0 additions & 43 deletions src/openforms/contrib/brp/models.py

This file was deleted.

18 changes: 18 additions & 0 deletions src/openforms/contrib/haal_centraal/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Provide Haal Centraal shared functionality.
`Haal Centraal`_ is an initiative to expose Dutch-government data via a number of APIs,
in a centralized way ("direct bij de bron"). It is often confused or merged with the
"Haal Centraal BRP Persoon bevragen", but this is really just a single API under the
Haal Centraal umbrella.
This package provides API configuration and (base) client implementations for those
Haal Centraal API's that we consume in Open Forms.
.. note:: Technically the BAG API (see :module:`openforms.contrib.kadaster.clients.bag`)
also falls under the umbrella of Haal Centraal, however all API root URLs still point
to ``kadaster.nl`` domains and as such the code is organized in
``openforms.contrib.kadaster``.
.. _Haal Centraal: https://vng-realisatie.github.io/Haal-Centraal/
"""
17 changes: 17 additions & 0 deletions src/openforms/contrib/haal_centraal/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib import admin
from django.utils.translation import gettext_lazy as _

from solo.admin import SingletonModelAdmin

from .models import HaalCentraalConfig


@admin.register(HaalCentraalConfig)
class HaalCentraalConfigAdmin(SingletonModelAdmin):
fieldsets = (
(
_("BRP Personen Bevragen"),
{"fields": ("brp_personen_service", "brp_personen_version")},
),
)
autocomplete_fields = ("brp_personen_service",)
8 changes: 8 additions & 0 deletions src/openforms/contrib/haal_centraal/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _


class HaalCentraalConfig(AppConfig):
name = "openforms.contrib.haal_centraal"
label = "haalcentraal"
verbose_name = _("Haal Centraal configuration")
25 changes: 25 additions & 0 deletions src/openforms/contrib/haal_centraal/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from zgw_consumers_ext.api_client import ServiceClientFactory

from ..models import HaalCentraalConfig
from .brp import CLIENT_CLS_FOR_VERSION as BRP_CLIENT_CLS_FOR_VERSION, BRPClient


class NoServiceConfigured(RuntimeError):
pass


def get_brp_client(**kwargs) -> BRPClient:
config = HaalCentraalConfig.get_solo()
assert isinstance(config, HaalCentraalConfig)
if not (service := config.brp_personen_service):
raise NoServiceConfigured("No BRP service configured!")

version = config.brp_personen_version
ClientCls = BRP_CLIENT_CLS_FOR_VERSION.get(version)
if ClientCls is None:
raise RuntimeError(
f"No suitable client class configured for API version {version}"
)

service_client_factory = ServiceClientFactory(service)
return ClientCls.configure_from(service_client_factory, **kwargs)
Loading

0 comments on commit 220fb42

Please sign in to comment.