Skip to content

Commit

Permalink
fix(resources): fix nil response handling (#151)
Browse files Browse the repository at this point in the history
Because

- In the case of error response, will cause resource methods to fail

This commit

- return none if request failed
  • Loading branch information
heiruwu authored May 9, 2024
1 parent bb052de commit c6df35c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
28 changes: 19 additions & 9 deletions instill/resources/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=no-member,wrong-import-position,no-name-in-module
import time
from typing import Optional

import instill.protogen.model.model.v1alpha.model_definition_pb2 as model_definition_interface
import instill.protogen.model.model.v1alpha.model_pb2 as model_interface
Expand Down Expand Up @@ -47,12 +48,15 @@ def __init__(

self.resource = model

def __call__(self, task_inputs: list, silent: bool = False) -> list:
return self.client.model_service.trigger_model(
def __call__(self, task_inputs: list, silent: bool = False) -> Optional[list]:
response = self.client.model_service.trigger_model(
self.resource.id,
task_inputs,
silent=silent,
).task_outputs
)
if response is not None:
return response.task_outputs
return response

@property
def client(self):
Expand All @@ -78,17 +82,23 @@ def _update(self):
def get_definition(self) -> model_definition_interface.ModelDefinition:
return self.resource.model_definition

def get_readme(self, silent: bool = False) -> str:
return self.client.model_service.get_model_card(
def get_readme(self, silent: bool = False) -> Optional[str]:
response = self.client.model_service.get_model_card(
self.resource.id,
silent=silent,
).readme
)
if response is not None:
return response.readme
return response

def get_state(self, silent: bool = False) -> model_interface.Model.State:
return self.client.model_service.watch_model(
def get_state(self, silent: bool = False):
response = self.client.model_service.watch_model(
self.resource.id,
silent=silent,
).state
)
if response is not None:
return response.state
return response

def deploy(self, silent: bool = False) -> model_interface.Model.State:
self.client.model_service.deploy_model(
Expand Down
22 changes: 15 additions & 7 deletions instill/resources/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pylint: disable=no-member,wrong-import-position,no-name-in-module
from typing import Tuple, Union
from typing import Optional, Tuple, Union

import grpc
from google.longrunning import operations_pb2
Expand Down Expand Up @@ -36,13 +36,15 @@ def __call__(
self,
task_inputs: list,
silent: bool,
) -> Tuple[list, pipeline_interface.TriggerMetadata]:
) -> Optional[Tuple[list, pipeline_interface.TriggerMetadata]]:
resp = self.client.pipeline_service.trigger_pipeline(
self.resource.id,
task_inputs,
silent=silent,
)
return resp.outputs, resp.metadata
if resp is not None:
return resp.outputs, resp.metadata
return resp

@property
def client(self):
Expand All @@ -64,21 +66,27 @@ def _update(self):
self.resource = self.client.pipeline_service.get_pipeline(name=self.resource.id)

def get_operation(self, operation: operations_pb2.Operation, silent: bool = False):
return self.client.pipeline_service.get_operation(
response = self.client.pipeline_service.get_operation(
operation.name,
silent=silent,
).operation
)
if response is not None:
return response.operation
return response

def trigger_async(
self,
task_inputs: list,
silent: bool = False,
) -> operations_pb2.Operation:
return self.client.pipeline_service.trigger_async_pipeline(
response = self.client.pipeline_service.trigger_async_pipeline(
self.resource.id,
task_inputs,
silent=silent,
).operation
)
if response is not None:
return response.operation
return response

def get_recipe(self) -> pipeline_interface.Recipe:
return self.resource.recipe
Expand Down
13 changes: 6 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c6df35c

Please sign in to comment.