Esempio n. 1
0
def create_remote_oio(io_url: str, object_url: str, object_type: str = "zaak") -> dict:
    if settings.CMIS_ENABLED:
        if object_type == "zaak":
            oio = ObjectInformatieObject.objects.create(
                informatieobject=io_url, zaak=object_url, object_type=object_type
            )
        elif object_type == "besluit":
            oio = ObjectInformatieObject.objects.create(
                informatieobject=io_url, besluit=object_url, object_type=object_type
            )

        response = {"url": oio.get_url()}
    else:
        client = Service.get_client(io_url)
        if client is None:
            raise UnknownService(f"{io_url} API should be added to Service model")

        body = {
            "informatieobject": io_url,
            "object": object_url,
            "objectType": object_type,
        }

        response = client.create("objectinformatieobject", data=body)
    return response
Esempio n. 2
0
    def clean(self):
        super().clean()

        app = self.cleaned_data.get("autorisaties_application")
        label = self.cleaned_data.get("label")
        app_id = self.cleaned_data.get("app_id")

        # the other fields become required fields if there's no application specified
        # that we can derive the values from
        if not app:
            for field in ["label", "app_id"]:
                value = self.cleaned_data.get(field)
                if not value:
                    form_field = self.fields[field]
                    self.add_error(
                        field,
                        forms.ValidationError(
                            form_field.error_messages["required"],
                            code="required"),
                    )

        else:
            ac_client = Service.get_client(app)
            application = ac_client.retrieve("applicatie", url=app)
            if not label:
                self.cleaned_data["label"] = application["label"]
            if not app_id:
                self.cleaned_data["app_id"] = application["url"]

        return self.cleaned_data
Esempio n. 3
0
def delete_remote_zaakbesluit(zaakbesluit_url: str) -> None:
    client = Service.get_client(zaakbesluit_url)
    if client is None:
        raise UnknownService(
            f"{zaakbesluit_url} API should be added to Service model")

    client.delete("zaakbesluit", zaakbesluit_url)
Esempio n. 4
0
 def get_drc_client(self, document_url: str) -> Service:
     try:
         return self.get_client(APITypes.drc)
     except NoService:
         client = Service.get_client(document_url)
         client._log.task = self.task
         return client
Esempio n. 5
0
    def perform_destroy(self, instance: Zaak):
        if instance.besluit_set.exists():
            raise ValidationError(
                {
                    api_settings.NON_FIELD_ERRORS_KEY:
                    _("All related Besluit objects should be destroyed before destroying the zaak"
                      )
                },
                code="pending-besluit-relation",
            )

        # check if we need to delete any remote OIOs
        autocommit = transaction.get_autocommit()
        assert autocommit is False, "Expected to be in a transaction.atomic block"
        # evaluate the queryset, because the transaction will delete the records with
        # a cascade
        oio_urls = instance.zaakinformatieobject_set.filter(
            _informatieobject__isnull=True).values_list(
                "_objectinformatieobject_url", flat=True)
        delete_params = [(url, Service.get_client(url)) for url in oio_urls]

        def _delete_oios():
            for url, client in delete_params:
                client.delete("objectinformatieobject", url=url)

        transaction.on_commit(_delete_oios)

        super().perform_destroy(instance)
Esempio n. 6
0
    def fetch_object(url: str, do_underscoreize=True) -> dict:
        from zgw_consumers.models import Service

        # TODO should we replace it with Service.get_client() and use it instead of requests?
        # but in this case we couldn't catch separate FetchJsonError
        client_auth_header = Service.get_auth_header(url)
        headers = client_auth_header or {}

        try:
            response = requests.get(url, headers=headers)
        except requests.exceptions.RequestException as exc:
            raise FetchError(exc.args[0]) from exc

        try:
            response.raise_for_status()
        except requests.HTTPError as exc:
            raise FetchError(exc.args[0]) from exc

        try:
            data = response.json()
        except json.JSONDecodeError as exc:
            raise FetchJsonError(exc.args[0]) from exc

        if not do_underscoreize:
            return data

        return underscoreize(data)
Esempio n. 7
0
def fetch_object(resource: str, url: str) -> dict:
    """
    Fetch a remote object by URL.
    """
    client = Service.get_client(url)
    if not client:
        raise UnknownService(f"{url} API should be added to Service model")
    obj = client.retrieve(resource, url=url)
    return obj
Esempio n. 8
0
def create_remote_oio(io_url: str, object_url: str, object_type: str = "zaak") -> dict:
    client = Service.get_client(io_url)
    if client is None:
        raise UnknownService(f"{io_url} API should be added to Service model")

    body = {"informatieobject": io_url, "object": object_url, "objectType": object_type}

    response = client.create("objectinformatieobject", data=body)
    return response
Esempio n. 9
0
 def client_from_url(self, url) -> ZGWClient:
     if url in self._clients:
         return self._clients[url]
     client = Service.get_client(url)
     if not client:
         raise ClientError(
             f"a ZGW service must be configured first for url '{url}'")
     self._clients[url] = client
     return client
Esempio n. 10
0
def get_auth(url: str) -> dict:
    logger.info("Authenticating for %s", url)
    auth_header = Service.get_auth_header(url)

    if auth_header is not None:
        return auth_header

    logger.warning("Could not authenticate for %s", url)
    return {}
Esempio n. 11
0
def create_remote_zaakbesluit(besluit_url: str, zaak_url: str) -> dict:
    client = Service.get_client(zaak_url)
    if client is None:
        raise UnknownService(f"{zaak_url} API should be added to Service model")

    zaak_uuid = get_uuid_from_path(zaak_url)
    body = {"besluit": besluit_url}

    response = client.create("zaakbesluit", data=body, zaak_uuid=zaak_uuid)

    return response
Esempio n. 12
0
def fetcher(url: str, *args, **kwargs):
    """
    Fetch the URL using requests.
    If the NLX address is configured, rewrite absolute url to NLX url.
    """
    service = Service.get_service(url)
    if service and service.nlx:
        # rewrite url
        url = url.replace(service.api_root, service.nlx, 1)

    return requests.get(url, *args, **kwargs)
Esempio n. 13
0
 def get_client_for(self, document_url: str) -> ZGWClient:
     for client in self._clients:
         if document_url.startswith(client.base_url):
             return client
     else:
         service = Service.get_service(document_url)
         auth_headers = get_credentials(self.app_id, service).get(service)
         client = service.build_client()
         if auth_headers:
             client.set_auth_value(auth_headers)
         return client
Esempio n. 14
0
def get_brt_service(task: BaseTask) -> Service:
    """
    Extract the BRT Service object to use for the client.
    """
    try:
        return get_alias_service(task, ALIAS, service__api_type=APITypes.orc)
    except NoService:
        warnings.warn(
            "Falling back to static configuration, this support will be removed "
            "in BPTL 1.1",
            DeprecationWarning,
        )
        variables = task.get_variables()
        return Service(
            api_root=API_ROOT,
            label="BRT",
            auth_type=AuthTypes.api_key,
            header_key="X-Api-Key",
            header_value=check_variable(variables, "BRTKey"),
            oas=API_ROOT,
        )
Esempio n. 15
0
def check_objecttype(object_type, version, data):
    if not data:
        return

    client = Service.get_client(object_type)
    objecttype_version_url = f"{object_type}/versions/{version}"

    try:
        response = client.retrieve("objectversion", url=objecttype_version_url)
    except ClientError as exc:
        raise ValidationError(exc.args[0]) from exc

    try:
        schema = response["jsonSchema"]
    except KeyError:
        msg = f"{objecttype_version_url} does not appear to be a valid objecttype."
        raise ValidationError(msg)

    # TODO: Set warning header if objecttype is not published.

    try:
        jsonschema.validate(data, schema)
    except jsonschema.exceptions.ValidationError as exc:
        raise ValidationError(exc.args[0]) from exc
Esempio n. 16
0
def test_require_either_oas_url_or_file():
    oas_path = os.path.join(os.path.dirname(__file__), "schemas/drc.yaml")
    with open(oas_path, "r") as oas_file:
        service = Service(
            label="Test",
            api_type=APITypes.drc,
            api_root="http://foo.bar",
            # oas and oas_file both empty
            oas="",
            oas_file=None,
        )
        service.save()

    with pytest.raises(ValidationError) as excinfo:
        service.full_clean()

    # check both fields have a message
    error = excinfo.value.error_dict
    assert error["oas"][0].messages == ["Set either oas or oas_file"]
    assert error["oas_file"][0].messages == ["Set either oas or oas_file"]
Esempio n. 17
0
def test_require_exclusively_oas_url_or_file(settings, tmp_path):
    settings.MEDIA_ROOT = tmp_path
    oas_path = os.path.join(os.path.dirname(__file__), "schemas/drc.yaml")

    with open(oas_path, "r") as oas_file:
        service = Service(
            label="Test",
            api_type=APITypes.drc,
            api_root="http://foo.bar",
            # oas and oas_file both defined
            oas="http://foo.bar/schema.yaml",
            oas_file=File(oas_file, name="schema.yaml"),
        )
        service.save()

    with pytest.raises(ValidationError) as excinfo:
        service.full_clean()

    # check both fields have a message
    error = excinfo.value.error_dict
    assert error["oas"][0].messages == ["Set either oas or oas_file, not both"]
    assert error["oas_file"][0].messages == [
        "Set either oas or oas_file, not both"
    ]
Esempio n. 18
0
def delete_remote_oio(oio_url: str) -> None:
    client = Service.get_client(oio_url)
    if client is None:
        raise UnknownService(f"{oio_url} API should be added to Service model")

    client.delete("objectinformatieobject", oio_url)
Esempio n. 19
0
 def _client_from_url(self, url):
     service = Service.get_service(url)
     return service.build_client()
def _client_from_url(url: str):
    service = Service.get_service(url)
    if not service:
        raise ClientError("There is no Service configured for %r" % url)
    return service.build_client()
Esempio n. 21
0
def get_client(url: str) -> Optional[ZGWClient]:
    client = Service.get_client(url)
    return client
Esempio n. 22
0
 def get_by_url(self, url):
     service = Service.get_service(url)
     uuid = get_uuid_from_path(url)
     return self.get(service=service, uuid=uuid)
Esempio n. 23
0
def _get_applications(service: Service) -> List[Dict[str, Any]]:
    client = service.build_client()
    results = get_paginated_results(client, "applicatie")
    return results