Skip to content

Commit

Permalink
[#2804] Update headers for Haal Centraal API client
Browse files Browse the repository at this point in the history
    - add headers for 'doelbinding', 'origin-oin' , 'verwerking' and
      'gebruiker' to requests maded with version 2.1 of the Haal
      Centraal client
  • Loading branch information
pi-sigma committed Oct 14, 2024
1 parent df9656f commit 81a5765
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/open_inwoner/haalcentraal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ class BRP_2_1(BRPAPI):

def make_request(self, user_bsn: str) -> requests.Response:
url = "personen"

headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"x-gebruiker": "BurgerZelf",
}
if self.config.api_origin_oin: # See Taiga #755
headers["x-origin-oin"] = self.config.api_origin_oin
if self.config.api_doelbinding: # See Taiga #755
headers["x-doelbinding"] = self.config.api_doelbinding
if self.config.api_verwerking:
headers["x-verwerking"] = self.config.api_verwerking

response = self.client.post(
url=url,
json={
Expand All @@ -138,10 +151,7 @@ def make_request(self, user_bsn: str) -> requests.Response:
"type": "RaadpleegMetBurgerservicenummer",
"burgerservicenummer": [user_bsn],
},
headers={
"Content-Type": "application/json",
"Accept": "application/json",
},
headers=headers,
verify=False,
)
return response
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 4.2.16 on 2024-10-14 13:33

from django.db import migrations, models

import open_inwoner.haalcentraal.validators


class Migration(migrations.Migration):

dependencies = [
("haalcentraal", "0002_auto_20230206_1511"),
]

operations = [
migrations.AddField(
model_name="haalcentraalconfig",
name="api_verwerking",
field=models.CharField(
blank=True,
help_text="Value of the 'x-verwerking' header for Haalcentraal BRP API requests",
max_length=242,
validators=[
open_inwoner.haalcentraal.validators.validate_verwerking_header
],
verbose_name="API 'verwerking' header",
),
),
migrations.AlterField(
model_name="haalcentraalconfig",
name="api_doelbinding",
field=models.CharField(
blank=True,
help_text="Value of the 'x-doelbinding' header for Haalcentraal BRP API requests.",
max_length=64,
verbose_name="API 'doelbinding' header",
),
),
migrations.AlterField(
model_name="haalcentraalconfig",
name="api_origin_oin",
field=models.CharField(
blank=True,
help_text="Value of the 'x-origin-oin' header for Haalcentraal BRP API requests.",
max_length=64,
verbose_name="API 'OIN' header",
),
),
]
15 changes: 13 additions & 2 deletions src/open_inwoner/haalcentraal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from solo.models import SingletonModel
from zgw_consumers.constants import APITypes

from .validators import validate_verwerking_header


class HaalCentraalConfigManager(models.Manager):
def get_queryset(self):
Expand All @@ -29,15 +31,24 @@ class HaalCentraalConfig(SingletonModel):
max_length=64,
blank=True,
help_text=_(
"Value of the 'x-origin-oin' header for Haalcentraal BRP v1.3 API requests."
"Value of the 'x-origin-oin' header for Haalcentraal BRP API requests."
),
)
api_doelbinding = models.CharField(
verbose_name=_("API 'doelbinding' header"),
max_length=64,
blank=True,
help_text=_(
"Value of the 'x-doelbinding' header for Haalcentraal BRP v1.3 API requests."
"Value of the 'x-doelbinding' header for Haalcentraal BRP API requests."
),
)
api_verwerking = models.CharField(
_("API 'verwerking' header"),
max_length=242,
blank=True,
validators=[validate_verwerking_header],
help_text=_(
"Value of the 'x-verwerking' header for Haalcentraal BRP API requests"
),
)

Expand Down
17 changes: 17 additions & 0 deletions src/open_inwoner/haalcentraal/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@
import requests_mock

from ..api import BRP_2_1
from ..models import HaalCentraalConfig
from .mixins import HaalCentraalMixin


@requests_mock.Mocker()
class BRPClientTest(HaalCentraalMixin, TestCase):
def setUp(self):
config = HaalCentraalConfig.get_solo()
config.api_doelbinding = "test_header_doelbinding"
config.api_origin_oin = "test_header_origin_oin"
config.api_verwerking = "test_header_verwerking"
config.save()

def test_request_content_type(self, m):
self._setUpMocks_v_2(m)
self._setUpService()
Expand All @@ -20,6 +28,15 @@ def test_request_content_type(self, m):
self.assertEqual(
m.request_history[0].headers["Content-Type"], "application/json"
)
self.assertEqual(
m.request_history[0].headers["x-origin-oin"], "test_header_origin_oin"
)
self.assertEqual(
m.request_history[0].headers["x-doelbinding"], "test_header_doelbinding"
)
self.assertEqual(
m.request_history[0].headers["x-verwerking"], "test_header_verwerking"
)

data = {
"fields": [
Expand Down
10 changes: 10 additions & 0 deletions src/open_inwoner/haalcentraal/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _


def validate_verwerking_header(value: str) -> None:
if value.count("@") > 1:
raise ValidationError(
_("The x-verwerking header must contain at most one '@' character."),
code="no_multiple_at_characters",
)

0 comments on commit 81a5765

Please sign in to comment.