FraunhoferIOSB / FROST-Python-Client

Python Client Library for FROST.
GNU Lesser General Public License v3.0
8 stars 8 forks source link

Get datastreams from FROST servers with ID of type str #26

Closed jfabius closed 11 months ago

jfabius commented 11 months ago

Problem

When a FROST server is configured to use strings as Thing id's, queries for retrieving datastreams of a single thing raise errors.

Example

things = (
    service
    .things()
    .query()
    .list()
)
print([t.id for t in things])

Out: ['some_string']

ds = (
    things
    .get(0)
    .get_datastreams()
    .query()
    .list()
)

Out: ERROR:root:Query failed with status-code 404, Not a valid path: Path /Things(some_string)/Datastreams is not valid: Encountered an error at (or somewhere around) input:1:9 Was expecting one of the following: T_LONG, T_STR_LIT Found string "some_string" of type T_NAME

Identified issue

The get_path method of class SensorThingsService (line 89-94) does not compose valid paths when the parent.id is of type string.

def get_path(self, parent, relation):
    if parent is None:
        return relation
    this_entity_type = entity_type.get_list_for_class(type(parent))
    return "{entity_type}({id})/{relation}".format(entity_type=this_entity_type, id=parent.id, relation=relation)

Proposed solution

This can be easily fixed by checking the type of the id and adding quotes.

def get_path(self, parent, relation):
    if parent is None:
        return relation
    this_entity_type = entity_type.get_list_for_class(type(parent))
    _id = f"'{parent.id}'" if isinstance(parent.id, str) else parent.id
    return "{entity_type}({id})/{relation}".format(entity_type=this_entity_type, id=_id, relation=relation)
vogljonathan commented 11 months ago

Thank you very much for this well-structured issue. I have adopted your suggested solution. However, I could imagine that there may still be problems with string-based ids in other places. Please do not hesitate to report them or to suggest extended functionality for this. By the way, I have closed issue #9 (also related to string based id), this was already fixed.

jfabius commented 11 months ago

Cheers! Thanks for the quick action. I'll keep you updated if I find anything more.