Skip to content

Commit

Permalink
Address review comments for python client algorithm store
Browse files Browse the repository at this point in the history
  • Loading branch information
bartvanb committed Feb 27, 2024
1 parent 30fdd91 commit 5373424
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 21 deletions.
7 changes: 2 additions & 5 deletions vantage6-client/vantage6/client/__init__.py
Expand Up @@ -62,7 +62,6 @@ def __init__(self, *args, log_level="debug", **kwargs) -> None:

# set collaboration id to None
self.collaboration_id = None
self.current_collaboration = None

# Display welcome message
self.log.info(" Welcome to")
Expand Down Expand Up @@ -188,8 +187,6 @@ def setup_collaboration(self, collaboration_id: int) -> None:
if "msg" in response:
self.log.info("--> %s", response["msg"])
return
# set current collaboration
self.current_collaboration = response

def wait_for_results(self, task_id: int, interval: float = 1) -> dict:
"""
Expand Down Expand Up @@ -630,8 +627,8 @@ def create(
Parameters
----------
collaboration : int
Collaboration id to which this node belongs. If no id provided the
collaboration that the user set up earlier is used.
Collaboration id to which this node belongs. If no ID was provided the,
collaboration from `client.setup_collaboration()` is used.
organization : int, optional
Organization id to which this node belongs. If no id provided
the users organization is used. Default value is None
Expand Down
26 changes: 19 additions & 7 deletions vantage6-client/vantage6/client/subclients/algorithm.py
Expand Up @@ -23,7 +23,7 @@ def get(self, id_: int) -> dict:
return self.parent.request(
f"algorithm/{id_}",
is_for_algorithm_store=True,
headers=self._get_server_url_header(),
headers=self.__get_server_url_header(),
)

@post_filtering(iterable=True)
Expand Down Expand Up @@ -59,7 +59,7 @@ def list(
return self.parent.request(
"algorithm",
is_for_algorithm_store=True,
headers=self._get_server_url_header(),
headers=self.__get_server_url_header(),
params={
"name": name,
"description": description,
Expand All @@ -77,7 +77,6 @@ def create(
image: str,
partitioning: str,
vantage6_version: str,
# note the capital L in List to avoid conflict with list() function
functions: List[dict],
) -> dict:
"""
Expand Down Expand Up @@ -131,7 +130,7 @@ def create(
"algorithm",
method="post",
is_for_algorithm_store=True,
headers=self._get_server_url_header(),
headers=self.__get_server_url_header(),
json={
"name": name,
"image": image,
Expand All @@ -142,21 +141,34 @@ def create(
},
)

def delete(self, id_: int) -> None:
def delete(self, id_: int) -> dict:
"""
Delete an algorithm from the algorithm store
Parameters
----------
id_ : int
Id of the algorithm
Returns
-------
dict
The deleted algorithm
"""
return self.parent.request(
f"algorithm/{id_}",
method="delete",
is_for_algorithm_store=True,
headers=self._get_server_url_header(),
headers=self.__get_server_url_header(),
)

def _get_server_url_header(self):
def __get_server_url_header(self) -> dict:
"""
Get the server url for request header
Returns
-------
dict
The server url in a dictionary so it can be used as header
"""
return {"server_url": self.parent.base_path}
68 changes: 59 additions & 9 deletions vantage6-client/vantage6/client/subclients/algorithm_store.py
Expand Up @@ -8,6 +8,7 @@ class AlgorithmStoreSubClient(ClientBase.SubClient):
def __init__(self, parent: ClientBase):
super().__init__(parent)
self.store_url = None
self.store_id = None

def set(self, id_: int) -> dict:
""" "
Expand All @@ -24,8 +25,11 @@ def set(self, id_: int) -> dict:
The algorithm store.
"""
store = self.get(id_)
if "url" in store:
try:
self.store_url = store["url"]
self.store_id = id_
except KeyError:
self.parent.log.error("Algorithm store URL could not be set.")
return store

@post_filtering(iterable=False)
Expand Down Expand Up @@ -116,18 +120,24 @@ def create(
If True, the algorithm store will be linked to the collaboration even for
localhost urls - which is not recommended in production scenarios for
security reasons.
Returns
-------
dict
The algorithm store.
"""
if collaboration is None:
collaboration = self.parent.collaboration_id
if all_collaborations:
collaboration = None
elif collaboration is None and self.parent.collaboration_id is not None:
collaboration = self.parent.collaboration_id
elif collaboration is None:
raise ValueError(
self.parent.log.error(
"No collaboration given and default collaboration is not set. "
"Please provide a collaboration, set a default collaboration, or make "
"the algorithm store available to all collaborations with "
"all_collaborations=True."
)
return
data = {
"algorithm_store_url": algorithm_store_url,
"name": name,
Expand All @@ -139,44 +149,84 @@ def create(

def update(
self,
id_: int,
id_: int = None,
name: str = None,
collaboration: int = None,
all_collaborations: bool = None,
):
) -> dict:
"""Update an algorithm store.
Parameters
----------
id_ : int
The id of the algorithm store.
The id of the algorithm store. If not given, the algorithm store must be
set with client.algorithm_store.set().
name : str, optional
The name of the algorithm store.
collaboration : int, optional
The id of the collaboration to link the algorithm store to.
all_collaborations : bool, optional
If True, the algorithm store is linked to all collaborations. If False,
the collaboration_id must be given.
Returns
-------
dict
The updated algorithm store.
"""
id_ = self.__get_store_id(id_)
if id_ is None:
return
data = {
"name": name,
}
if collaboration is not None or all_collaborations:
data["collaboration_id"] = collaboration
return self.parent.request(f"algorithmstore/{id_}", method="patch", json=data)

def delete(self, id_: int):
def delete(self, id_: int = None) -> dict:
"""Delete an algorithm store.
Parameters
----------
id_ : int
The id of the algorithm store.
The id of the algorithm store. If not given, the algorithm store must be
set with client.algorithm_store.set().
Returns
-------
dict
The deleted algorithm store.
"""
id_ = self.__get_store_id(id_)
if id_ is None:
return
return self.parent.request(
f"algorithmstore/{id_}",
method="delete",
params={
"server_url": self.parent.base_path,
},
)

def __get_store_id(self, id_: int = None) -> int:
"""
Get the algorithm store id.
Parameters
----------
id_ : int
The id of the algorithm store. If not given, the algorithm store must be
set with client.algorithm_store.set().
Returns
-------
int
The algorithm store id.
"""
if id_ is None:
id_ = self.store_id
if id_ is None:
self.parent.log.error("No algorithm store id given or set.")
return
return id_

0 comments on commit 5373424

Please sign in to comment.