Skip to content

Commit

Permalink
feat: adaptation to the new version of middleware
Browse files Browse the repository at this point in the history
- removed FailedToObtainPlan exception (replaced with FailedToConnect)
- code simplified
  • Loading branch information
artable-dev committed Jan 29, 2024
1 parent 28f674b commit de5cdc8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 28 deletions.
42 changes: 20 additions & 22 deletions era_5g_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from era_5g_client.client_base import NetAppClientBase
from era_5g_client.dataclasses import MiddlewareInfo
from era_5g_client.exceptions import FailedToConnect, FailedToDeleteResource, FailedToObtainPlan, NetAppNotReady
from era_5g_client.exceptions import FailedToConnect, FailedToDeleteResource, NetAppNotReady
from era_5g_client.middleware_resource_checker import MiddlewareResourceChecker
from era_5g_interface.channels import CallbackInfoClient

Expand Down Expand Up @@ -118,7 +118,6 @@ def run_task(
is True. Defaults to None.
Raises:
FailedToObtainPlan: Raised when could not get the plan.
FailedToConnect: Raised when running the task failed.
"""

Expand All @@ -128,7 +127,7 @@ def run_task(
task_id, resource_lock, robot_id
) # Get the plan_id by sending the token and task_id.
if not self.action_plan_id:
raise FailedToObtainPlan("Failed to obtain action plan id...")
raise FailedToConnect("Failed to obtain action plan id...")

self.resource_checker = MiddlewareResourceChecker(
str(self.token),
Expand Down Expand Up @@ -267,29 +266,28 @@ def gateway_get_plan(self, taskid: str, resource_lock: bool, robot_id: str) -> s

assert self.middleware_info
# Request plan.
try:
self.logger.debug("Goal task is: " + str(taskid))
hed = {"Authorization": f"Bearer {str(self.token)}"}
data = {
"TaskId": str(taskid),
"LockResourceReUse": resource_lock,
"RobotId": robot_id,
}
r = requests.post(self.middleware_info.build_api_endpoint("Task/Plan"), json=data, headers=hed)
response = r.json()
self.logger.debug("Goal task is: " + str(taskid))
hed = {"Authorization": f"Bearer {str(self.token)}"}
data = {
"TaskId": str(taskid),
"DisableResourceReuse": resource_lock,
"RobotId": robot_id,
}
r = requests.post(self.middleware_info.build_api_endpoint("Task/Plan"), json=data, headers=hed)
response = r.json()

if not isinstance(response, dict):
raise FailedToConnect("Invalid response.")

if "statusCode" in response and (response["statusCode"] == 500 or response["statusCode"] == 400):
raise FailedToConnect(f"response {response['statusCode']}: {response['message']}")

if not isinstance(response, Dict):
raise FailedToConnect("Invalid response.")

if "statusCode" in response and (response["statusCode"] == 500 or response["statusCode"] == 400):
raise FailedToConnect(f"response {response['statusCode']}: {response['message']}")
# TODO: if "errors" in response:
# raise FailedToConnect(str(response["errors"]))
action_plan_id = str(response["ActionPlanId"])
try:
action_plan_id = str(response["task"]["ActionPlanId"])
self.logger.debug("ActionPlanId ** is: " + str(action_plan_id))
return action_plan_id
except KeyError as e:
raise FailedToObtainPlan(f"Could not get the plan: {e}")
raise FailedToConnect(f"Could not get the plan: {e}")

def delete_all_resources(self) -> None:
"""Delete all resources.
Expand Down
6 changes: 0 additions & 6 deletions era_5g_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ class FailedToInitialize(Era5gClientException):
pass


class FailedToObtainPlan(Era5gClientException):
"""Exception which is raised when the client could not get the plan from the Middleware."""

pass


class FailedToDeleteResource(Era5gClientException):
"""Exception which is raised when the client could not when could not delete Middleware resource."""

Expand Down

0 comments on commit de5cdc8

Please sign in to comment.