-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #632 from maykinmedia/feature/1475-dump-mocks
[#1475] Added basic API mock-endpoint feature
- Loading branch information
Showing
13 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
27 changes: 27 additions & 0 deletions
27
...is/openklant-read/contactmomenten/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbb2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbb2", | ||
"url": "https://contactmomenten.nl/api/v1/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbb2", | ||
"identificatie": "BA321", | ||
"type": "OIP Contact Formulier", | ||
"kanaal": "Contact Formulier", | ||
"status": "afgehandeld", | ||
"antwoord": "Hier is een vergunning voor nodig vanwege het bestemmingsplan. Deze kan je aanvragen bij loket woonzaken.", | ||
"vorigContactmoment": null, | ||
"volgendContactmoment": null, | ||
"bronorganisatie": "WwXGZrg", | ||
"registratiedatum": "2022-05-15T23:12:13+00:00", | ||
"voorkeurskanaal": "xyz", | ||
"voorkeurstaal": "vzT", | ||
"tekst": "Mag ik mijn garage veranderen in een kantoor?\n\n", | ||
"onderwerpLinks": [ | ||
], | ||
"initiatiefnemer": "gemeente", | ||
"medewerker": "", | ||
"medewerkerIdentificatie": null, | ||
"klantcontactmomenten": [ | ||
|
||
], | ||
"objectcontactmomenten": [ | ||
], | ||
"onderwerp": "Verbouwing garage" | ||
} |
27 changes: 27 additions & 0 deletions
27
...is/openklant-read/contactmomenten/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb", | ||
"url": "https://contactmomenten.nl/api/v1/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb", | ||
"identificatie": "AB123", | ||
"type": "OIP Contact Formulier", | ||
"kanaal": "MAIL", | ||
"status": "in_behandeling", | ||
"antwoord": "", | ||
"vorigContactmoment": null, | ||
"volgendContactmoment": null, | ||
"bronorganisatie": "WwXGZrg", | ||
"registratiedatum": "2022-05-21T23:11:50+00:00", | ||
"voorkeurskanaal": "xyz", | ||
"voorkeurstaal": "vzT", | ||
"tekst": "Hoeveel uren les heb ik nodig voor een rijbewijs?", | ||
"onderwerpLinks": [ | ||
], | ||
"initiatiefnemer": "gemeente", | ||
"medewerker": "", | ||
"medewerkerIdentificatie": null, | ||
"klantcontactmomenten": [ | ||
|
||
], | ||
"objectcontactmomenten": [ | ||
], | ||
"onderwerp": "Aanvraag rijbewijs" | ||
} |
23 changes: 23 additions & 0 deletions
23
src/open_inwoner/apimock/apis/openklant-read/contactmomenten/klantcontactmomenten.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"count": 1, | ||
"previous": null, | ||
"next": null, | ||
"results": [ | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-cccccccccccc", | ||
"url": "https://contactmomenten.nl/api/v1/klantcontactmomenten/aaaaaaaa-aaaa-aaaa-aaaa-cccccccccccc", | ||
"klant": "https://klanten.nl/api/v1/klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"contactmoment": "https://contactmomenten.nl/api/v1/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb", | ||
"rol": "gesprekspartner", | ||
"gelezen": true | ||
}, | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-ccccccccccc2", | ||
"url": "https://contactmomenten.nl/api/v1/klantcontactmomenten/aaaaaaaa-aaaa-aaaa-aaaa-ccccccccccc2", | ||
"klant": "https://klanten.nl/api/v1/klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"contactmoment": "https://contactmomenten.nl/api/v1/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbb2", | ||
"rol": "gesprekspartner", | ||
"gelezen": true | ||
} | ||
] | ||
} |
8 changes: 8 additions & 0 deletions
8
...klant-read/contactmomenten/klantcontactmomenten/aaaaaaaa-aaaa-aaaa-aaaa-ccccccccccc2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-ccccccccccc2", | ||
"url": "https://contactmomenten.nl/api/v1/klantcontactmomenten/aaaaaaaa-aaaa-aaaa-aaaa-ccccccccccc2", | ||
"klant": "https://klanten.nl/api/v1/klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"contactmoment": "https://contactmomenten.nl/api/v1/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbb2", | ||
"rol": "belanghebbende", | ||
"gelezen": false | ||
} |
8 changes: 8 additions & 0 deletions
8
...klant-read/contactmomenten/klantcontactmomenten/aaaaaaaa-aaaa-aaaa-aaaa-cccccccccccc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-cccccccccccc", | ||
"url": "https://contactmomenten.nl/api/v1/klantcontactmomenten/aaaaaaaa-aaaa-aaaa-aaaa-cccccccccccc", | ||
"klant": "https://klanten.nl/api/v1/klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"contactmoment": "https://contactmomenten.nl/api/v1/contactmoment/aaaaaaaa-aaaa-aaaa-aaaa-bbbbbbbbbbbb", | ||
"rol": "belanghebbende", | ||
"gelezen": false | ||
} |
19 changes: 19 additions & 0 deletions
19
...woner/apimock/apis/openklant-read/klanten/klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"url": "https://klanten.nl/api/v1/klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"bronorganisatie": "123456789", | ||
"klantnummer": "123456", | ||
"bedrijfsnaam": "Mijn Bedrijf", | ||
"functie": "Functie", | ||
"websiteUrl": "https://www.maykin.nl/", | ||
"voornaam": "Piet", | ||
"voorvoegselAchternaam": "van der", | ||
"achternaam": "Test", | ||
"telefoonnummer": "0123456789", | ||
"emailadres": null, | ||
"adres": null, | ||
"subject": "", | ||
"subjectType": null, | ||
"aanmaakkanaal": "", | ||
"geverifieerd": false | ||
} |
26 changes: 26 additions & 0 deletions
26
src/open_inwoner/apimock/apis/openklant-read/klanten/klanten.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"count": 1, | ||
"previous": null, | ||
"next": null, | ||
"results": [ | ||
{ | ||
"uuid": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"url": "https://klanten.nl/api/v1/klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", | ||
"bronorganisatie": "123456789", | ||
"klantnummer": "123456", | ||
"bedrijfsnaam": "Mijn Bedrijf", | ||
"functie": "Functie", | ||
"websiteUrl": "https://www.maykin.nl/", | ||
"voornaam": "Piet", | ||
"voorvoegselAchternaam": "van der", | ||
"achternaam": "Test", | ||
"telefoonnummer": "0123456789", | ||
"emailadres": null, | ||
"adres": null, | ||
"subject": "", | ||
"subjectType": null, | ||
"aanmaakkanaal": "", | ||
"geverifieerd": false | ||
} | ||
] | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from open_inwoner.accounts.tests.factories import UserFactory | ||
|
||
|
||
class APIMockTest(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
cls.url = reverse( | ||
"apimock:mock", | ||
kwargs={ | ||
"set_name": "openklant-read", | ||
"api_name": "klanten", | ||
"endpoint": "klanten", | ||
}, | ||
) | ||
|
||
def test_basic_response(self): | ||
response = self.client.get(self.url) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
data = response.json() | ||
self.assertIn("results", data) | ||
|
||
# check url transform | ||
url = data["results"][0]["url"] | ||
self.assertNotIn("https://klanten.nl/", url) | ||
self.assertIn("http://testserver/", url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.urls import path | ||
|
||
from open_inwoner.apimock.views import APIMockView | ||
|
||
app_name = "apimock" | ||
|
||
urlpatterns = [ | ||
path( | ||
"<slug:set_name>/<slug:api_name>/<path:endpoint>", | ||
APIMockView.as_view(), | ||
name="mock", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from django.http import Http404, JsonResponse | ||
from django.views import View | ||
|
||
|
||
class APIMockView(View): | ||
""" | ||
simple API mock that serves sets of JSON from a directory structure that matches the API structure | ||
./apimock/api/openklant-read/klanten/klant/aabbaacc.json -> http://server/apimock/openklant-read/klanten/klant/aabbaacc | ||
with URL transformer | ||
""" | ||
|
||
# TODO move replacers to config file in set directory | ||
url_replacers = [ | ||
("https://contactmomenten.nl/api/v1/", "contactmomenten"), | ||
("https://klanten.nl/api/v1/", "klanten"), | ||
] | ||
|
||
def get(self, request, *args, **kwargs): | ||
set_name = kwargs["set_name"] | ||
api_name = kwargs["api_name"] | ||
endpoint = kwargs["endpoint"] | ||
|
||
endpoint = endpoint.rstrip("/") | ||
|
||
base_dir = Path(__file__).parent.resolve() | ||
base_dir /= "apis" | ||
|
||
api_dir = base_dir / set_name / api_name | ||
if not api_dir.exists(): | ||
raise Http404("bad api_dir") | ||
|
||
file_path = api_dir / (endpoint + ".json") | ||
if not file_path.exists(): | ||
raise Http404("bad endpoint") | ||
|
||
# make dynamic from url conf? | ||
prefix = request.build_absolute_uri(f"/apimock/{set_name}/") | ||
|
||
with open(file_path, "r") as f: | ||
data = json.load(f) | ||
process_urls(data, prefix, self.url_replacers) | ||
return JsonResponse(data) | ||
|
||
|
||
def process_urls(data, prefix, url_replacers): | ||
""" | ||
recursive replace URL prefixes | ||
""" | ||
if isinstance(data, list): | ||
for i, e in enumerate(data): | ||
data[i] = process_urls(e, prefix, url_replacers) | ||
return data | ||
elif isinstance(data, dict): | ||
for k, e in data.items(): | ||
data[k] = process_urls(e, prefix, url_replacers) | ||
return data | ||
elif isinstance(data, str): | ||
return replace_url_prefix(data, prefix, url_replacers) | ||
else: | ||
return data | ||
|
||
|
||
def replace_url_prefix(s, prefix, url_replacers): | ||
for url, api_name in url_replacers: | ||
if s.startswith(url): | ||
return f"{prefix}{api_name}/{s[len(url) :]}" | ||
return s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters