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 authored and swrichards committed Oct 24, 2024
1 parent c60b121 commit f382e3c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 11 deletions.
17 changes: 13 additions & 4 deletions src/open_inwoner/haalcentraal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ class BRP_2_1(BRPAPI):

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

headers = {
"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 +150,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-15 08:42

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
48 changes: 43 additions & 5 deletions src/open_inwoner/haalcentraal/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +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 test_request_content_type(self, m):
def test_brp_client_request_content(self, m):
self._setUpMocks_v_2(m)
self._setUpService()

api_client = BRP_2_1()
response = api_client.make_request("999993847")

self.assertEqual(
m.request_history[0].headers["Content-Type"], "application/json"
)

data = {
"fields": [
"naam.geslachtsnaam",
Expand All @@ -42,3 +39,44 @@ def test_request_content_type(self, m):
}

self.assertEqual(json.loads(response.request.body), data)

def test_brp_client_additional_request_headers_defined(self, m):
self._setUpMocks_v_2(m)
self._setUpService()

config = HaalCentraalConfig.get_solo()
config.api_origin_oin = "test_x-origin-oin_header"
config.api_doelbinding = "test_x-doelbinding_header"
config.api_verwerking = "test_x-verwerking_header"
config.save()

api_client = BRP_2_1()
api_client.make_request("999993847")

self.assertEqual(
m.request_history[0].headers["Content-Type"], "application/json"
)
self.assertEqual(
m.request_history[0].headers["x-origin-oin"], "test_x-origin-oin_header"
)
self.assertEqual(
m.request_history[0].headers["x-doelbinding"], "test_x-doelbinding_header"
)
self.assertEqual(
m.request_history[0].headers["x-verwerking"], "test_x-verwerking_header"
)

def test_brp_client_additional_request_headers_not_defined(self, m):
self._setUpMocks_v_2(m)
self._setUpService()

api_client = BRP_2_1()
api_client.make_request("999993847")

self.assertEqual(
m.request_history[0].headers["Content-Type"], "application/json"
)
# check that that additional headers are absent (not empty strings)
self.assertIsNone(m.request_history[0].headers.get("x-origin-oin"))
self.assertIsNone(m.request_history[0].headers.get("x-doelbinding"))
self.assertIsNone(m.request_history[0].headers.get("x-verwerking"))
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 f382e3c

Please sign in to comment.