From 237aebedafba93cf00acc9fbd02f4fef1dc52f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20M=C3=B6ller?= Date: Tue, 2 Apr 2024 14:20:20 +0200 Subject: [PATCH] adapter.http: improve `SpecificAssetId` filtering --- basyx/aas/adapter/http.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/basyx/aas/adapter/http.py b/basyx/aas/adapter/http.py index ee4dd4ed1..8790bd6cc 100644 --- a/basyx/aas/adapter/http.py +++ b/basyx/aas/adapter/http.py @@ -604,34 +604,24 @@ def handle_request(self, request: Request): # ------ AAS REPO ROUTES ------- def get_aas_all(self, request: Request, url_args: Dict, **_kwargs) -> Response: - def asset_id_matches(spec_asset_id, specific_asset_ids): - """Checks if a specific asset ID matches any within a list.""" - return any( - spec_asset_id == asset_id - for asset_id in specific_asset_ids - ) - response_t = get_response_type(request) - aas_iterable: Iterator[model.AssetAdministrationShell] = self._get_all_obj_of_type( - model.AssetAdministrationShell) + aas: Iterator[model.AssetAdministrationShell] = self._get_all_obj_of_type(model.AssetAdministrationShell) - # Filter by 'idShort' if provided in the request id_short = request.args.get("idShort") if id_short is not None: - aas_iterable = filter(lambda shell: shell.id_short == id_short, aas_iterable) + aas = filter(lambda shell: shell.id_short == id_short, aas) - # Filtering by base64url encoded SpecificAssetIds if provided asset_ids = request.args.getlist("assetIds") if asset_ids is not None: # Decode and instantiate SpecificAssetIds - spec_asset_ids = map(lambda asset_id: HTTPApiDecoder.base64urljson(asset_id, model.SpecificAssetId, - False), asset_ids) + # This needs to be a list, otherwise we can only iterate it once. + specific_asset_ids: List[model.SpecificAssetId] = list( + map(lambda asset_id: HTTPApiDecoder.base64urljson(asset_id, model.SpecificAssetId, False), asset_ids)) # Filter AAS based on these SpecificAssetIds - aas_iterable = filter(lambda shell: all( - asset_id_matches(spec_asset_id, shell.asset_information.specific_asset_id) - for spec_asset_id in spec_asset_ids), aas_iterable) + aas = filter(lambda shell: all(specific_asset_id in shell.asset_information.specific_asset_id + for specific_asset_id in specific_asset_ids), aas) - return response_t(list(aas_iterable)) + return response_t(list(aas)) def post_aas(self, request: Request, url_args: Dict, map_adapter: MapAdapter) -> Response: response_t = get_response_type(request)