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

Update stored query api #35

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Release History
- Add search entities api
- Increase maximum length of `query` parameter for datasource filter method.
- Add `suggest` parameter to entities aggregate method.
- Add possible errors to stored queries register method
- Add possible errors to stored queries edit method
- Add stored queries filtration method parameters
- Add stored query delete method
- Add `is_replist_compatible` property to `StoredQueryFilterView`
- Add `Search` to `QueryCompatibility` enum

2.13.0a1 (2024-07-10)
---------------------
Expand Down
5 changes: 5 additions & 0 deletions cybsi/api/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class ConflictError(APIError):
DuplicateDataSourceType = "DuplicateDataSourceType"
DuplicateKey = "DuplicateKey"
DuplicateLogin = "DuplicateLogin"
StoredQueryConflict = "StoredQueryConflict"

def __init__(self, content: JsonObject) -> None:
super().__init__(409, content, header="resource already exists")
Expand Down Expand Up @@ -305,6 +306,10 @@ class SemanticErrorCodes(CybsiAPIEnum):
"""User not found."""
WrongEntityAttribute = "WrongEntityAttribute"
"""The attribute is not registered for provided entity."""
InvalidStoredQuery = "InvalidStoredQuery"
"""Query is not compatible with replist."""
StoredQueryIsLocked = "StoredQueryIsLocked"
"""Stored query is in use. Probably used in replist."""


class _ErrorView(dict):
Expand Down
1 change: 1 addition & 0 deletions cybsi/api/search/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ class QueryCompatibility(CybsiAPIEnum):
"""Stored query compatibility."""

Replist = "Replist" # doc: Replists.
Search = "Search" # doc: Search.
43 changes: 43 additions & 0 deletions cybsi/api/search/stored_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def register(self, stored_query: "StoredQueryForm") -> RefView:
Returns:
Reference to a registered stored query.
Raises:
:class:`~cybsi.api.error.ConflictError`:
Stored query with such name already exists.
:class:`~cybsi.api.error.SemanticError`: Form contains logic errors.
Note:
Semantic error codes specific for this method:
Expand Down Expand Up @@ -89,12 +91,15 @@ def edit(
:class:`~cybsi.api.error.InvalidRequestError`:
Provided arguments have invalid values.
:class:`~cybsi.api.error.NotFoundError`: Stored query not found.
:class:`~cybsi.api.error.ConflictError`:
Stored query with such name already exists.
:class:`~cybsi.api.error.ResourceModifiedError`:
Stored query changed since last request. Update tag and retry.
:class:`~cybsi.api.error.SemanticError`: Form contains logic errors.
Note:
Semantic error codes specific for this method:
* :attr:`~cybsi.api.error.SemanticErrorCodes.InvalidQueryText`
* :attr:`~cybsi.api.error.SemanticErrorCodes.InvalidStoredQuery`
"""
form: Dict[str, Any] = {}
if name is not None:
Expand All @@ -104,10 +109,35 @@ def edit(
path = f"{self._path}/{query_uuid}"
self._connector.do_patch(path=path, tag=tag, json=form)

def delete(
self,
query_uuid: uuid.UUID,
) -> None:
"""Delete stored query.
.. versionadded:: 2.13
Note:
Calls `DELETE /search/stored-queries/{query_uuid}`.
Args:
query_uuid: Stored query uuid.
Raises:
:class:`~cybsi.api.error.NotFoundError`: Stored query not found.
:class:`~cybsi.api.error.SemanticError`: Request contains logic errors.
Note:
Semantic error codes specific for this method:
* :attr:`~cybsi.api.error.SemanticErrorCodes.StoredQueryIsLocked`
"""

path = f"{self._path}/{query_uuid}"
self._connector.do_delete(path=path)

def filter(
self,
*,
user_uuid: Optional[uuid.UUID] = None,
query_name: Optional[str] = None,
is_replist_compatible: Optional[bool] = None,
cursor: Optional[Cursor] = None,
limit: Optional[int] = None,
) -> Page["StoredQueryFilterView"]:
Expand All @@ -118,6 +148,9 @@ def filter(
Args:
user_uuid: User's identifier.
Filter stored queries by author's id.
query_name: Filter stored queries by specified substring (case-insensitive).
Substring length must be in range [1, 250].
is_replist_compatible: Filter stored queries by replist compatibility flag.
cursor: Page cursor.
limit: Page limit.
Returns:
Expand All @@ -132,6 +165,10 @@ def filter(

if user_uuid is not None:
params["userUUID"] = str(user_uuid)
if query_name is not None:
params["queryName"] = query_name
if is_replist_compatible is not None:
params["isReplistCompatible"] = is_replist_compatible
if cursor:
params["cursor"] = str(cursor)
if limit:
Expand Down Expand Up @@ -280,6 +317,12 @@ def author(self) -> RefView:

return RefView(self._get("author"))

@property
def is_replist_compatible(self) -> bool:
"""Replist compatibility flag."""

return self._get("isReplistCompatible")


class StoredQueryView(_TaggedRefView, StoredQueryFilterView):
"""View of a stored query,
Expand Down
Loading