Skip to content

Commit

Permalink
Merge pull request #1292 from Amsterdam/feature/WON-41-toeristischeve…
Browse files Browse the repository at this point in the history
…rhuur-registraties

WON-41 Added vakantieverhuur registration endpoint by bagId
  • Loading branch information
remyvdwereld authored Dec 30, 2024
2 parents 659c35a + 15909fc commit 6675bd6
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 56 deletions.
8 changes: 2 additions & 6 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ DEFAULT_THEME=Vakantieverhuur
DEFAULT_REASON=SIG melding
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL=https://api.acceptatie.toeristischeverhuur.nl/api/
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN=SECRET_ACCESS_TOKEN
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID=0503100000000209
VAKANTIEVERHUUR_REGISTRATIE_API_URL=https://acceptatie.toeristischeverhuur.nl/ext/api/Registrations/
VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN=SECRET_ACCESS_TOKEN
VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN=999999990
VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID=0503100000000209
VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER=05034542BC484F3891BB
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID=0503100000000209
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN=999999990
ZAKEN_CONTAINER_HOST=http://zaak-gateway:8000

DEFAULT_SCHEDULE_ACTIONS=Huisbezoek,Hercontrole
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ __pycache__/
app/.env
app/.coverage
private_media/
.local.env
14 changes: 14 additions & 0 deletions app/apps/addresses/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,17 @@ class MeldingenSerializer(serializers.Serializer):
totalPages = serializers.IntegerField()
totalRecords = serializers.IntegerField()
data = serializers.ListField(child=serializers.DictField())


class RegistrationNumberSerializer(serializers.Serializer):
registrationNumber = serializers.CharField(required=True)


class RegistrationDetailsSerializer(serializers.Serializer):
registrationNumber = serializers.CharField(required=True)
requester = serializers.DictField()
rentalHouse = serializers.DictField()
requestForOther = serializers.BooleanField()
requestForBedAndBreakfast = serializers.BooleanField()
createdAt = serializers.DateTimeField()
agreementDate = serializers.DateTimeField()
64 changes: 63 additions & 1 deletion app/apps/addresses/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
DistrictSerializer,
HousingCorporationSerializer,
MeldingenSerializer,
RegistrationDetailsSerializer,
RegistrationNumberSerializer,
ResidentsSerializer,
)
from apps.cases.models import Advertisement
Expand All @@ -21,7 +23,11 @@
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from utils.api_queries_brp import get_brp_by_nummeraanduiding_id
from utils.api_queries_toeristische_verhuur import get_vakantieverhuur_meldingen
from utils.api_queries_toeristische_verhuur import (
get_vakantieverhuur_meldingen,
get_vakantieverhuur_registration,
get_vakantieverhuur_registrations_by_bag_id,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -240,3 +246,59 @@ def meldingen(self, request, bag_id):
{"error": "Toeristische verhuur meldingen could not be obtained"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

@extend_schema(
description="Gets all registrations for holiday rental by bag_id",
responses={status.HTTP_200_OK: RegistrationDetailsSerializer(many=True)},
)
@action(
detail=True,
url_path="registrations",
methods=["get"],
pagination_class=None,
)
def registrations(self, request, bag_id):
try:
(
registrations_data,
status_code,
) = get_vakantieverhuur_registrations_by_bag_id(
bag_id,
)
serialized_registrations = RegistrationNumberSerializer(
data=registrations_data, many=True
)
serialized_registrations.is_valid(raise_exception=True)

# Fetch details for each registration number
detailed_registrations = []
for registration in serialized_registrations.data:
# Remove spaces from registration number
registration_number = registration["registrationNumber"].replace(
" ", ""
)
try:
# Fetch detailed data for the current registrationNumber
(
registration_details,
detail_status_code,
) = get_vakantieverhuur_registration(registration_number)
if detail_status_code == 200: # Only append if successful
detailed_registrations.append(registration_details)
else:
print(
f"Failed to fetch details for {registration_number}. Status: {detail_status_code}"
)
except Exception as e:
print(f"Error fetching details for {registration_number}: {e}")

serializer = RegistrationDetailsSerializer(
detailed_registrations, many=True
)

return Response(serializer.data, status=status_code)
except Exception:
return Response(
{"error": "Toeristische verhuur registrations could not be obtained"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
22 changes: 7 additions & 15 deletions app/apps/health/health_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
do_bag_search_pdok_by_bag_id,
)
from utils.api_queries_toeristische_verhuur import (
get_bag_vakantieverhuur_registrations,
get_bsn_vakantieverhuur_registrations,
get_vakantieverhuur_meldingen,
get_vakantieverhuur_registration,
get_vakantieverhuur_registrations_by_bag_id,
get_vakantieverhuur_registrations_by_bsn_number,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -265,22 +264,15 @@ class VakantieVerhuurRegistratieCheck(BaseHealthCheckBackend):
def check_status(self):

try:
registration = get_vakantieverhuur_registration(
settings.VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER
)
assert bool(
registration
), "The registration data is empty and could not be retrieved"

bsn_registrations = get_bsn_vakantieverhuur_registrations(
settings.VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN
bsn_registrations = get_vakantieverhuur_registrations_by_bsn_number(
settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN
)
assert (
len(bsn_registrations) > 0
), "The registration data is empty and could not be retrieved using the BSN number"

bag_registrations = get_bag_vakantieverhuur_registrations(
settings.VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID
bag_registrations = get_vakantieverhuur_registrations_by_bag_id(
settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID
)
assert (
len(bag_registrations) > 0
Expand Down Expand Up @@ -311,7 +303,7 @@ def check_status(self):

try:
get_vakantieverhuur_meldingen(
settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID,
settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID,
query_params=params,
use_retry=False,
)
Expand Down
19 changes: 4 additions & 15 deletions app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,11 @@ def get_redis_url():
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN = os.getenv(
"VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN"
)
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID = os.getenv(
"VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_HEALTH_CHECK_BAG_ID", "0503100000000209"
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID = os.getenv(
"VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BAG_ID", "0503100000000209"
)

VAKANTIEVERHUUR_REGISTRATIE_API_URL = os.getenv("VAKANTIEVERHUUR_REGISTRATIE_API_URL")
VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN = os.getenv(
"VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN"
)
VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN = os.getenv(
"VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BSN"
)
VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID = os.getenv(
"VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_BAG_ID"
)
VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER = os.getenv(
"VAKANTIEVERHUUR_REGISTRATIE_API_HEALTH_CHECK_REGISTRATION_NUMBER"
VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN = os.getenv(
"VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_BSN"
)

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
Expand Down
42 changes: 23 additions & 19 deletions app/utils/api_queries_toeristische_verhuur.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,61 @@ def _get_vakantieverhuur_meldingen_internal():
return _get_vakantieverhuur_meldingen_internal()


@retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR))
@retry(stop=stop_after_attempt(1), after=after_log(logger, logging.ERROR))
def get_vakantieverhuur_registration(registration_number):
"""
Get the Vakantieverhuur registration
"""
header = {"x-api-key": settings.VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN}
url = (
f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}{registration_number}"
)
header = {
"x-api-key": settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN
}
url = f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}registrations/{registration_number}"

response = requests.get(
url=url,
headers=header,
verify="/usr/local/share/ca-certificates/adp_rootca.crt",
timeout=30,
)
response.raise_for_status()

return response.json()
return response.json(), response.status_code


@retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR))
def get_bsn_vakantieverhuur_registrations(bsn_number):
def get_vakantieverhuur_registrations_by_bag_id(bag_id):
"""
Get the Vakantieverhuur registrations using a BSN number
Get the Vakantieverhuur registration by bag_id
"""
header = {"x-api-key": settings.VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN}
url = f"{settings.VAKANTIEVERHUUR_REGISTRATIE_API_URL}bsn/{bsn_number}"
header = {
"x-api-key": settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN
}
url = f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}registrations/adresseerbaarObjectIdentificatie/{bag_id}"

response = requests.get(
url=url,
headers=header,
verify="/usr/local/share/ca-certificates/adp_rootca.crt",
timeout=30,
)
response.raise_for_status()

return response.json()
return response.json(), response.status_code


@retry(stop=stop_after_attempt(3), after=after_log(logger, logging.ERROR))
def get_bag_vakantieverhuur_registrations(bag_id):
def get_vakantieverhuur_registrations_by_bsn_number(bsn_number):
"""
Get the Vakantieverhuur registrations using a BSN number
Get the Vakantieverhuur registration by bsn_number
"""
header = {"x-api-key": settings.VAKANTIEVERHUUR_REGISTRATIE_API_ACCESS_TOKEN}
url = f"{settings.VAKANTIEVERHUUR_REGISTRATIE_API_URL}bagid/{bag_id}"
header = {
"x-api-key": settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_ACCESS_TOKEN
}
url = f"{settings.VAKANTIEVERHUUR_TOERISTISCHE_VERHUUR_API_URL}registrations/bsn/{bsn_number}"

response = requests.get(
url=url,
headers=header,
verify="/usr/local/share/ca-certificates/adp_rootca.crt",
timeout=30,
)
response.raise_for_status()

return response.json()
return response.json(), response.status_code

0 comments on commit 6675bd6

Please sign in to comment.