Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapter.http: implement AAS API submodel routes via redirects #27

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions basyx/aas/adapter/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import werkzeug.exceptions
import werkzeug.routing
import werkzeug.urls
import werkzeug.utils
from werkzeug.exceptions import BadRequest, Conflict, NotFound, UnprocessableEntity
from werkzeug.routing import MapAdapter, Rule, Submount
from werkzeug.wrappers import Request, Response
Expand Down Expand Up @@ -402,6 +403,12 @@ def __init__(self, object_store: model.AbstractObjectStore):
Rule("/", methods=["POST"], endpoint=self.post_aas_submodel_refs),
Rule("/<identifier:submodel_id>/", methods=["DELETE"],
endpoint=self.delete_aas_submodel_refs_specific)
]),
Submount("/submodels/<identifier:submodel_id>", [
Rule("/", methods=["PUT"], endpoint=self.put_aas_submodel_refs_submodel),
Rule("/", methods=["DELETE"], endpoint=self.delete_aas_submodel_refs_submodel),
Rule("/", endpoint=self.aas_submodel_refs_redirect),
Rule("/<path:path>/", endpoint=self.aas_submodel_refs_redirect)
])
])
]),
Expand Down Expand Up @@ -673,6 +680,49 @@ def delete_aas_submodel_refs_specific(self, request: Request, url_args: Dict, **
aas.commit()
return response_t()

def put_aas_submodel_refs_submodel(self, request: Request, url_args: Dict, **_kwargs) -> Response:
response_t = get_response_type(request)
aas = self._get_obj_ts(url_args["aas_id"], model.AssetAdministrationShell)
aas.update()
sm_ref = self._get_submodel_reference(aas, url_args["submodel_id"])
submodel = self._resolve_reference(sm_ref)
new_submodel = HTTPApiDecoder.request_body(request, model.Submodel, is_stripped_request(request))
# determine whether the id changed in advance, in case something goes wrong while updating the submodel
id_changed: bool = submodel.id != new_submodel.id
# TODO: https://github.com/eclipse-basyx/basyx-python-sdk/issues/216
submodel.update_from(new_submodel)
submodel.commit()
if id_changed:
aas.submodel.remove(sm_ref)
aas.submodel.add(model.ModelReference.from_referable(submodel))
aas.commit()
return response_t()

def delete_aas_submodel_refs_submodel(self, request: Request, url_args: Dict, **_kwargs) -> Response:
response_t = get_response_type(request)
aas = self._get_obj_ts(url_args["aas_id"], model.AssetAdministrationShell)
aas.update()
sm_ref = self._get_submodel_reference(aas, url_args["submodel_id"])
submodel = self._resolve_reference(sm_ref)
self.object_store.remove(submodel)
aas.submodel.remove(sm_ref)
aas.commit()
return response_t()

def aas_submodel_refs_redirect(self, request: Request, url_args: Dict, map_adapter: MapAdapter) -> Response:
aas = self._get_obj_ts(url_args["aas_id"], model.AssetAdministrationShell)
aas.update()
# the following makes sure the reference exists
self._get_submodel_reference(aas, url_args["submodel_id"])
redirect_url = map_adapter.build(self.get_submodel, {
"submodel_id": url_args["submodel_id"]
}, force_external=True)
if "path" in url_args:
redirect_url += url_args["path"] + "/"
if request.query_string:
redirect_url += "?" + request.query_string.decode("ascii")
return werkzeug.utils.redirect(redirect_url, 307)

# ------ SUBMODEL REPO ROUTES -------
def get_submodel_all(self, request: Request, url_args: Dict, **_kwargs) -> Response:
response_t = get_response_type(request)
Expand Down
Loading