-
Notifications
You must be signed in to change notification settings - Fork 56
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
feat: Query parameter support for entity store query API #3383
feat: Query parameter support for entity store query API #3383
Conversation
Codecov ReportAttention: Patch coverage is Additional details and impacted files📢 Thoughts on this report? Let us know! |
Robot Results
|
I don't see a In practice the entity tree will be mostly never deep. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will be happy to approve, once docs added.
3513b1b
to
8d8d432
Compare
@@ -1015,20 +1015,22 @@ def register_entity( | |||
|
|||
@keyword("List Entities") | |||
def list_entities( | |||
self, device_name: str = None | |||
self, params: Dict[str, str] = None, device_name: str = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the putting all of the query parameter mappings in the params is a good idea here (it just makes it a lot harder to use).
self, params: Dict[str, str] = None, device_name: str = None | |
self, entity_type: Optional[str, str] = None, parent: Optional[str, str] = None, root: Optional[str, str] = None, device_name: str = None |
Note I'm using entity_type
instead of type
as type
might be a reserved word in python (but check this yourself).
And if people still want to use a dictionary, then we could just add kwargs support to the function signature, and then people can pass this stuff as a dictionary (as that is more python like)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved by 1de9671
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved
* 404: Not Found | ||
``` | ||
Entity not found with topic id: device/unknown/topic/id | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not to sure about returning 404 when the query does not match any entities.
Generally having no results in a query is not an error. Trying to access a resource that does not exist, would warrant a 404 response.
If you look at the REST API from Cumulocity, the query endpoints (e.g. GET /inventory/managedObjects
don't return a 404 NOT FOUND HTTP status code when there are no matching results, it just returns a 200 HTTP status and an empty array in the body (assuming we're talking about json bodies here). Technically speaking, I believe the "resource" in GET /v1/entities
is /v1/entities
itself...which does exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this was a confusing case for me as well and my original implementation was retuning empty array for non-existent entity. I changed it to 404 because empty array is returned for a leaf entity that doesn't have any children as well. So, when we get an empty array, we won't be able to differentiate if it's because there are no entities matching the query or if it's a non-existent entity. That's why I changed it to 404 for the latter case so that there is a clear distinction.
But, if the convention is empty array, then I'll change it back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I would change it back to returning an empty array. This is much easier to deal with for people consuming the API as generally error handling generally wants to split "there are no results" from "the api request failed".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plus one could use the root
query parameter as that returns child entities and the root entity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if you want to check the existence of a device, then you should be using GET http://localhost:8000/tedge/entity-store/v1/entities/{entity}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted that change in 1de9671
Item 1: Response body Content-Type on errors.Currently errors responses are returned to the user with the Given that the responses of successful queries are json, having the error messages in the same format makes it easier to parse by clients, for example: Using an invalid value for the curl -s 'http://localhost:8000/tedge/entity-store/v1/entities?type=asdfasdf' | jq
parse error: Invalid numeric literal at line 1, column 8 So for example if the error message is in json format, it could look something like this: curl -s 'http://localhost:8000/tedge/entity-store/v1/entities?type=asdfasdf' | jq
{
"error": "Invalid entity type: asdfasdf"
} The user is still free to control whether they want to pipe results at all from a failing curl command using |
Item 2: Empty query parameter values should be treated as missing query parameter valuesTreating empty values as missing values makes it very easy for users to write a simple wrapper around the calls without having to complicated string matching. For example, the following currently returns an error. $ curl -s 'http://localhost:8000/tedge/entity-store/v1/entities?type=' -v
Invalid entity type: But if we were to treat an empty query parameter value the same as an absent value, then it makes it much easier to build the query parameters. For example, below shows two implementations that do exactly the same thing (wrap the type filter so the user can provide a given value), and it shows how simple it could be versus if we treat empty values as an error (due to being an invalid type). # Option 1 (proposal): treat empty query param values as non-existing values
FILTER_BY_TYPE="${1:-}"
curl -s 'http://localhost:8000/tedge/entity-store/v1/entities?type=${FILTER_BY_TYPE}' -v
# Option 2 (current): empty query param values are treated like any explicit values
# vs. having to add a lot of empty string checking when building the query parameters
FILTER_BY_TYPE="${1:-}"
FILTER_ARGS=
if [ -n "$FILTER_BY_TYPE" ]; then
if [ -n "$FILTER_ARGS" ]; then
FILTER_ARGS="$FILTER_ARGS&type=${FILTER_BY_TYPE}"
else
FILTER_ARGS="type=${FILTER_BY_TYPE}"
fi
fi
curl -s 'http://localhost:8000/tedge/entity-store/v1/entities?${FILTER_ARGS}' -v |
|
||
| Parameter | Description | Examples | | ||
|-----------|--------------------------------------------------------------|-------------------------------------| | ||
| `root` | The topic id of the root node where to start the search from | `device/child2//` | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worthwhile mentioned that using the root
query parameter includes the root element.
"The topic id of the root node where to start the search from (including the root entity)"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved by 1de9671
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, overall looks nice, just a few minor things to change.
But I'm sorry I just realised that I've been reviewing the entire entity api not just the query api (probably because I was looking at the "register.md" file without a diff view in my editor). So all of the non-query related changes can be ignored and done in a follow up PR (we shouldn't corrupt the scope).
Item 1 is ok, though item 2 is still returning an error when using empty query parameters.
Though using empty |
Addressed by 2302609 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved
81c6126
to
af96b3f
Compare
af96b3f
to
f060b7a
Compare
f060b7a
to
2a4a658
Compare
Proposed changes
root
parent
type
Decided not to add the
depth
parameter as agreed here.Multiple values for the same parameter is also not supported.
Types of changes
Paste Link to the issue
#3339
Checklist
cargo fmt
as mentioned in CODING_GUIDELINEScargo clippy
as mentioned in CODING_GUIDELINESFurther comments