Skip to content

Commit

Permalink
adapter.http: improve SpecificAssetId filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
jkhsjdhjs authored and s-heppner committed Apr 3, 2024
1 parent 973f81a commit 237aebe
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions basyx/aas/adapter/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 237aebe

Please sign in to comment.