Skip to content

Commit

Permalink
chore(client): update client naming (#218)
Browse files Browse the repository at this point in the history
Because

- excessive suffix for client name

This commit

- update client naming
  • Loading branch information
heiruwu authored Oct 4, 2024
1 parent f524739 commit d0a3f58
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 121 deletions.
126 changes: 28 additions & 98 deletions instill/clients/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,162 +17,92 @@ def __init__(
requester_id="",
async_enabled: bool = False,
) -> None:
self.mgmt_service = MgmtClient(
self.mgmt = MgmtClient(
api_token=api_token,
url=url,
secure=secure,
requester_id=requester_id,
async_enabled=async_enabled,
)
if not self.mgmt_service.is_serving():
if not self.mgmt.is_serving():
Logger.w("Instill Core is required")
raise NotServingException

self.pipeline_service = PipelineClient(
self.pipeline = PipelineClient(
api_token=api_token,
url=url,
secure=secure,
lookup_func=self._lookup_namespace_uid,
requester_id=requester_id,
async_enabled=async_enabled,
)
if not self.pipeline_service.is_serving():
if not self.pipeline.is_serving():
Logger.w("Instill VDP is not serving, VDP functionalities will not work")

self.model_service = ModelClient(
self.model = ModelClient(
api_token=api_token,
url=url,
secure=secure,
lookup_func=self._lookup_namespace_uid,
requester_id=requester_id,
async_enabled=async_enabled,
)
if not self.model_service.is_serving():
if not self.model.is_serving():
Logger.w(
"Instill Model is not serving, Model functionalities will not work"
)

self.artifact_service = ArtifactClient(
self.artifact = ArtifactClient(
api_token=api_token,
url=url,
secure=secure,
lookup_func=self._lookup_namespace_uid,
requester_id=requester_id,
async_enabled=async_enabled,
)
if not self.artifact_service.is_serving():
if not self.artifact.is_serving():
Logger.w(
"Instill Artifact is not serving, Artifact functionalities will not work"
)

def _lookup_namespace_uid(self, namespace_id: str):
resp = self.mgmt_service.check_namespace(namespace_id)
resp = self.mgmt.check_namespace(namespace_id)
if resp.type == mgmt_interface.CheckNamespaceAdminResponse.NAMESPACE_USER:
namespace_uid = self.mgmt_service.get_user(namespace_id).user.uid
namespace_uid = self.mgmt.get_user(namespace_id).user.uid
elif (
resp.type
== mgmt_interface.CheckNamespaceAdminResponse.NAMESPACE_ORGANIZATION
):
namespace_uid = self.mgmt_service.get_organization(
namespace_id
).organization.uid
namespace_uid = self.mgmt.get_organization(namespace_id).organization.uid
else:
raise Exception("namespace ID not available")

return namespace_uid

def close(self):
self.mgmt_service.close()
self.pipeline_service.close()
self.model_service.close()
self.artifact_service.close()
self.mgmt.close()
self.pipeline.close()
self.model.close()
self.artifact.close()

async def async_close(self):
self.mgmt_service.async_close()
self.pipeline_service.async_close()
self.model_service.async_close()
self.artifact_service.async_close()
self.mgmt.async_close()
self.pipeline.async_close()
self.model.async_close()
self.artifact.async_close()

def get_mgmt_client(self) -> MgmtClient:
return self.mgmt_service
def get_mgmt(self) -> MgmtClient:
return self.mgmt

def get_artifact_client(self) -> ArtifactClient:
return self.artifact_service
def get_artifact(self) -> ArtifactClient:
return self.artifact

def get_pipeline_client(self) -> PipelineClient:
return self.pipeline_service
def get_pipeline(self) -> PipelineClient:
return self.pipeline

def get_model_client(self) -> ModelClient:
return self.model_service
def get_model(self) -> ModelClient:
return self.model


def init_core_client(api_token: str = "", async_enabled: bool = False) -> InstillClient:
def init_core_client(api_token: str, async_enabled: bool = False) -> InstillClient:
return InstillClient(api_token=api_token, async_enabled=async_enabled)


# def init_artifact_client(
# api_token: str = "", async_enabled: bool = False
# ) -> ArtifactClient:
# client = ArtifactClient(api_token=api_token, async_enabled=async_enabled)
# if not client.is_serving():
# Logger.w(
# "Instill Artifact is not serving, Artifact functionalities will not work"
# )
# raise NotServingException

# return client


# def init_model_client(api_token: str = "", async_enabled: bool = False) -> ModelClient:
# mgmt_service = MgmtClient(api_token=api_token, async_enabled=False)
# if not mgmt_service.is_serving():
# Logger.w("Instill Core is required")
# raise NotServingException

# user_name = mgmt_service.get_user().user.name
# # mgmt_service.close()

# client = ModelClient(
# lookup_func=
# namespace_id=user_name.split("/")[1],
# api_token=api_token,
# async_enabled=async_enabled,
# )
# if not client.is_serving():
# Logger.w("Instill Model is not serving, Model functionalities will not work")
# raise NotServingException

# return client


# def init_pipeline_client(
# namespace: str = "", api_token: str = "", async_enabled: bool = False
# ) -> PipelineClient:
# if namespace == "":
# mgmt_service = MgmtClient(api_token=api_token, async_enabled=False)
# if not mgmt_service.is_serving():
# Logger.w("Instill Core is required")
# raise NotServingException

# namespace = mgmt_service.get_user().user.name
# mgmt_service.close()
# else:
# namespace = f"organizations/{namespace}"

# client = PipelineClient(
# namespace=namespace, api_token=api_token, async_enabled=async_enabled
# )
# if not client.is_serving():
# Logger.w("Instill VDP is not serving, VDP functionalities will not work")
# raise NotServingException

# return client


# def init_mgmt_client(api_token: str = "", async_enabled: bool = False) -> MgmtClient:
# client = MgmtClient(api_token=api_token, async_enabled=async_enabled)
# if not client.is_serving():
# Logger.w("Instill Core is required")
# raise NotServingException

# return client
12 changes: 5 additions & 7 deletions instill/resources/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def __init__(
) -> None:
super().__init__()
self.client = client
get_resp = client.model_service.get_model(model_name=name, silent=True)
get_resp = client.model.get_model(model_name=name, silent=True)
if get_resp is None:
model = client.model_service.create_model(
model = client.model.create_model(
name=name,
definition=definition,
configuration=configuration,
Expand All @@ -32,7 +32,7 @@ def __init__(
self.resource = model

def __call__(self, task_inputs: list, silent: bool = False) -> Optional[list]:
response = self.client.model_service.trigger_model(
response = self.client.model.trigger(
self.resource.id,
task_inputs,
silent=silent,
Expand All @@ -58,16 +58,14 @@ def resource(self, resource: model_interface.Model):
self._resource = resource

def _update(self):
self.resource = self.client.model_service.get_model(
model_name=self.resource.id
).model
self.resource = self.client.model.get_model(model_name=self.resource.id).model

def get_definition(self) -> model_definition_interface.ModelDefinition:
return self.resource.model_definition

def delete(self, silent: bool = False):
if self.resource is not None:
self.client.model_service.delete_model(
self.client.model.delete_model(
self.resource.id,
silent=silent,
)
28 changes: 12 additions & 16 deletions instill/resources/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional, Tuple, Union

import grpc
from google.longrunning import operations_pb2
from google.longrunning.operations_pb2 import Operation
from google.protobuf import json_format
from google.protobuf.field_mask_pb2 import FieldMask
from google.protobuf.struct_pb2 import Struct
Expand All @@ -25,12 +25,12 @@ def __init__(
self.client = client
get_resp = None
pipeline = None
get_resp = client.pipeline_service.get_pipeline(
get_resp = client.pipeline.get_pipeline(
namespace_id=namespace_id,
pipeline_id=pipeline_id,
)
if get_resp is None:
pipeline = client.pipeline_service.create_pipeline(
pipeline = client.pipeline.create_pipeline(
namespace_id=namespace_id,
recipe=recipe,
).pipeline
Expand All @@ -46,7 +46,7 @@ def __call__(
task_inputs: list,
silent: bool = False,
) -> Optional[Tuple[list, pipeline_interface.TriggerMetadata]]:
resp = self.client.pipeline_service.trigger_pipeline(
resp = self.client.pipeline.trigger(
self.resource.id,
task_inputs,
silent=silent,
Expand All @@ -72,10 +72,10 @@ def resource(self, resource: pipeline_interface.Pipeline):
self._resource = resource

def _update(self):
self.resource = self.client.pipeline_service.get_pipeline(name=self.resource.id)
self.resource = self.client.pipeline.get_pipeline(name=self.resource.id)

def get_operation(self, operation: operations_pb2.Operation, silent: bool = False):
response = self.client.pipeline_service.get_operation(
def get_operation(self, operation: Operation, silent: bool = False):
response = self.client.pipeline.get_operation(
operation.name,
silent=silent,
)
Expand All @@ -87,8 +87,8 @@ def trigger_async(
self,
task_inputs: list,
silent: bool = False,
) -> operations_pb2.Operation:
response = self.client.pipeline_service.trigger_async_pipeline(
) -> Operation:
response = self.client.pipeline.trigger_async(
self.resource.id,
task_inputs,
silent=silent,
Expand All @@ -103,7 +103,7 @@ def get_recipe(self) -> dict:
def update_recipe(self, recipe: Struct, silent: bool = False):
pipeline = self.resource
pipeline.recipe.CopyFrom(recipe)
self.client.pipeline_service.update_pipeline(
self.client.pipeline.update_pipeline(
pipeline,
FieldMask(paths=["recipe"]),
silent=silent,
Expand All @@ -112,9 +112,7 @@ def update_recipe(self, recipe: Struct, silent: bool = False):

def validate_pipeline(self, silent: bool = True) -> bool:
try:
self.client.pipeline_service.validate_pipeline(
name=self.resource.id, silent=silent
)
self.client.pipeline.validate_pipeline(name=self.resource.id, silent=silent)
return True
except grpc.RpcError as rpc_error:
Logger.w(rpc_error.code())
Expand All @@ -123,6 +121,4 @@ def validate_pipeline(self, silent: bool = True) -> bool:

def delete(self, silent: bool = False):
if self.resource is not None:
self.client.pipeline_service.delete_pipeline(
self.resource.id, silent=silent
)
self.client.pipeline.delete_pipeline(self.resource.id, silent=silent)

0 comments on commit d0a3f58

Please sign in to comment.