Skip to content

Commit

Permalink
👽 [#4057] Address breaking changes in zgw-consumers
Browse files Browse the repository at this point in the history
Dropping gemma-zds-client means that we can't use any of its
API anymore.
  • Loading branch information
sergei-maertens committed Mar 26, 2024
1 parent b53be20 commit b27de95
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def set_kvk_service(apps, _):
"""
Derive the "common" API root for KVK interaction.
NOTE: the explanation below is kept for historical reasons. The API specs are
separate and so are the services/config. We don't use the API specs anymore,
but API gateways caused issues. See migration 0007 which reverts this change.
The zoeken/basisprofielen API's were historically separate services because they
have their own OpenAPI specs and zgw_consumers requires api_root to be unique. Not
doing this doesn't allow using zgw_consumers.Service.build_client (which needs the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from django.core.management import BaseCommand

import black
import requests
import yaml
from glom import GlomError, Path, glom
from zds_client.oas import SchemaFetcher
from zgw_consumers.models import Service

from ...attributes_generator import OpenApi3AttributesGenerator
Expand Down Expand Up @@ -70,11 +71,18 @@ def select_schema(root_schema, api_schema):
return None


def fetch_schema(url: str) -> dict:
# fetch the schema and load as yaml. Note that JSON documents are also valid YAML.
response = requests.get(url)
response.raise_for_status()
root_schema = yaml.safe_load(response.content)
return root_schema


def generate_prefill_from_spec_url(
url, api_path="", api_schema="", use_embeds=False, command=""
):
schema_fetcher = SchemaFetcher()
root_schema = schema_fetcher.fetch(url)
root_schema = fetch_schema(url)

# find the call with the http parameters we're interested in
if not api_path and not api_schema:
Expand Down Expand Up @@ -333,7 +341,7 @@ def handle(self, **options):

if url and not options["path"] and not options["schema"]:
# be helpful and suggest paths and schemas
schema = SchemaFetcher().fetch(url)
schema = fetch_schema(url)

if schema["paths"]:
self.stdout.write("path:")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from functools import lru_cache
from pathlib import Path
from typing import Any
from unittest import skip
Expand Down Expand Up @@ -47,16 +46,6 @@ def setUpClass(cls):
),
)

# prevent parsing the yaml over and over and over
cls.service.id = 1 # need pk for __hash__
# FIXME: update to new client approach
cls.service.build_client = lru_cache(1)(cls.service.build_client)

# populate the schema cache *before* any test runs, otherwise this causes flakiness
# in hypothesis tests
client = cls.service.build_client()
client.schema

@requests_mock.Mocker()
def test_it_performs_simple_get(self, m):
m.get("https://httpbin.org/get", json={"url": "https://httpbin.org/get"})
Expand Down

0 comments on commit b27de95

Please sign in to comment.