Skip to content

Commit

Permalink
chore(client): update protogen and fix client (#235)
Browse files Browse the repository at this point in the history
Because

- protobuf definitions are updated

This commit

- update client code accordingly
  • Loading branch information
joremysh authored Oct 22, 2024
1 parent 954f94b commit c6d8436
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 19 deletions.
3 changes: 2 additions & 1 deletion instill/clients/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import instill.protogen.common.healthcheck.v1beta.healthcheck_pb2 as healthcheck
from instill.clients.base import Client, RequestFactory
from instill.clients.instance import InstillInstance
from instill.helpers.const import HOST_URL_PROD
from instill.utils.error_handler import grpc_handler


Expand All @@ -18,7 +19,7 @@ def __init__(
self,
api_token: str,
lookup_func: Callable[[str], str],
url: str = "api.instill.tech",
url: str = HOST_URL_PROD,
secure: bool = True,
requester_id: str = "",
async_enabled: bool = False,
Expand Down
80 changes: 77 additions & 3 deletions instill/clients/artifact.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
# pylint: disable=no-member,wrong-import-position,too-many-lines,no-name-in-module
from datetime import datetime
from typing import Callable, List, Optional

# common
from google.protobuf import timestamp_pb2

# artifact
import instill.protogen.artifact.artifact.v1alpha.artifact_pb2 as artifact_interface
import instill.protogen.artifact.artifact.v1alpha.artifact_public_service_pb2_grpc as artifact_service
import instill.protogen.artifact.artifact.v1alpha.chunk_pb2 as chunk_interface
import instill.protogen.artifact.artifact.v1alpha.file_catalog_pb2 as file_catalog_interface
import instill.protogen.artifact.artifact.v1alpha.object_pb2 as object_interface
import instill.protogen.artifact.artifact.v1alpha.qa_pb2 as qa_interface

# common
import instill.protogen.common.healthcheck.v1beta.healthcheck_pb2 as healthcheck
from instill.clients.base import Client, RequestFactory
from instill.clients.instance import InstillInstance
from instill.helpers.const import HOST_URL_PROD
from instill.utils.error_handler import grpc_handler
from instill.utils.process_file import process_file

Expand All @@ -21,7 +25,7 @@ def __init__(
self,
api_token: str,
lookup_func: Callable[[str], str],
url: str = "api.instill.tech",
url: str = HOST_URL_PROD,
secure: bool = True,
requester_id: str = "",
async_enabled: bool = False,
Expand Down Expand Up @@ -520,3 +524,73 @@ def get_file_catalog(
),
metadata=self.host.metadata + self.metadata,
).send_sync()

@grpc_handler
def get_object_upload_url(
self,
namespace_id: str,
object_name: str,
url_expire_days: int,
last_modified_time: datetime,
object_expire_days: int,
async_enabled: bool = False,
) -> object_interface.GetObjectUploadURLResponse:
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(last_modified_time)

if async_enabled:
return RequestFactory(
method=self.host.async_client.GetObjectUploadURL,
request=object_interface.GetObjectUploadURLRequest(
namespace_id=namespace_id,
object_name=object_name,
url_expire_days=url_expire_days,
last_modified_time=timestamp,
object_expire_days=object_expire_days,
),
metadata=self.host.metadata + self.metadata,
).send_async()

return RequestFactory(
method=self.host.client.GetObjectUploadURL,
request=object_interface.GetObjectUploadURLRequest(
namespace_id=namespace_id,
object_name=object_name,
url_expire_days=url_expire_days,
last_modified_time=timestamp,
object_expire_days=object_expire_days,
),
metadata=self.host.metadata + self.metadata,
).send_sync()

@grpc_handler
def get_object_download_url(
self,
namespace_id: str,
object_uid: str,
object_name: str,
url_expire_days: int,
async_enabled: bool = False,
) -> object_interface.GetObjectDownloadURLResponse:
if async_enabled:
return RequestFactory(
method=self.host.async_client.GetObjectDownloadURL,
request=object_interface.GetObjectDownloadURLRequest(
namespace_id=namespace_id,
object_uid=object_uid,
object_name=object_name,
url_expire_days=url_expire_days,
),
metadata=self.host.metadata + self.metadata,
).send_async()

return RequestFactory(
method=self.host.client.GetObjectDownloadURL,
request=object_interface.GetObjectDownloadURLRequest(
namespace_id=namespace_id,
object_uid=object_uid,
object_name=object_name,
url_expire_days=url_expire_days,
),
metadata=self.host.metadata + self.metadata,
).send_sync()
19 changes: 17 additions & 2 deletions instill/clients/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from instill.clients.mgmt import MgmtClient
from instill.clients.model import ModelClient
from instill.clients.pipeline import PipelineClient
from instill.helpers.const import HOST_URL_PROD
from instill.utils.error_handler import NotServingException
from instill.utils.logger import Logger

Expand All @@ -13,7 +14,7 @@ class InstillClient:
def __init__(
self,
api_token: str,
url: str = "api.instill.tech",
url: str = HOST_URL_PROD,
secure: bool = True,
requester_id="",
async_enabled: bool = False,
Expand Down Expand Up @@ -124,23 +125,27 @@ def get_app(self) -> AppClient:
def init_core_client(
api_token: str,
requester_id="",
url: str = HOST_URL_PROD,
async_enabled: bool = False,
) -> InstillClient:
return InstillClient(
api_token=api_token,
requester_id=requester_id,
url=url,
async_enabled=async_enabled,
)


def init_artifact_client(
api_token: str,
requester_id: str = "",
url: str = HOST_URL_PROD,
async_enabled: bool = False,
) -> ArtifactClient:
client = InstillClient(
api_token=api_token,
requester_id=requester_id,
url=url,
async_enabled=async_enabled,
)
if not client.get_artifact().is_serving():
Expand All @@ -155,11 +160,13 @@ def init_artifact_client(
def init_model_client(
api_token: str,
requester_id="",
url: str = HOST_URL_PROD,
async_enabled: bool = False,
) -> ModelClient:
client = InstillClient(
api_token=api_token,
requester_id=requester_id,
url=url,
async_enabled=async_enabled,
)
if not client.get_model().is_serving():
Expand All @@ -172,11 +179,13 @@ def init_model_client(
def init_pipeline_client(
api_token: str,
requester_id="",
url: str = HOST_URL_PROD,
async_enabled: bool = False,
) -> PipelineClient:
client = InstillClient(
api_token=api_token,
requester_id=requester_id,
url=url,
async_enabled=async_enabled,
)
if not client.get_pipeline().is_serving():
Expand All @@ -189,11 +198,13 @@ def init_pipeline_client(
def init_mgmt_client(
api_token: str,
requester_id="",
url: str = HOST_URL_PROD,
async_enabled: bool = False,
) -> MgmtClient:
client = InstillClient(
api_token=api_token,
requester_id=requester_id,
url=url,
async_enabled=async_enabled,
)
if not client.get_mgmt().is_serving():
Expand All @@ -204,11 +215,15 @@ def init_mgmt_client(


def init_app_client(
api_token: str = "", requester_id: str = "", async_enabled: bool = False
api_token: str = "",
requester_id: str = "",
url: str = HOST_URL_PROD,
async_enabled: bool = False,
) -> AppClient:
client = InstillClient(
api_token=api_token,
requester_id=requester_id,
url=url,
async_enabled=async_enabled,
)
if not client.get_app().is_serving():
Expand Down
3 changes: 2 additions & 1 deletion instill/clients/mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import instill.protogen.core.mgmt.v1beta.mgmt_public_service_pb2_grpc as mgmt_service
from instill.clients.base import Client, RequestFactory
from instill.clients.instance import InstillInstance
from instill.helpers.const import HOST_URL_PROD
from instill.utils.error_handler import grpc_handler


class MgmtClient(Client):
def __init__(
self,
api_token: str,
url: str = "api.instill.tech",
url: str = HOST_URL_PROD,
secure: bool = True,
requester_id: str = "",
async_enabled: bool = False,
Expand Down
52 changes: 47 additions & 5 deletions instill/clients/model.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# pylint: disable=no-member,wrong-import-position,too-many-lines,no-name-in-module
from datetime import datetime
from typing import Callable, List, Optional

from google.protobuf import field_mask_pb2
from google.protobuf import field_mask_pb2, timestamp_pb2
from google.protobuf.struct_pb2 import Struct

# common
import instill.protogen.common.healthcheck.v1beta.healthcheck_pb2 as healthcheck
import instill.protogen.common.task.v1alpha.task_pb2 as task_interface
import instill.protogen.model.model.v1alpha.model_definition_pb2 as model_definition_interface
Expand All @@ -14,6 +14,7 @@
import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service
from instill.clients.base import Client, RequestFactory
from instill.clients.instance import InstillInstance
from instill.helpers.const import HOST_URL_PROD
from instill.utils.error_handler import grpc_handler


Expand All @@ -22,7 +23,7 @@ def __init__(
self,
api_token: str,
lookup_func: Callable[[str], str],
url: str = "api.instill.tech",
url: str = HOST_URL_PROD,
secure: bool = True,
requester_id: str = "",
async_enabled: bool = False,
Expand Down Expand Up @@ -952,7 +953,6 @@ def list_model_runs(
return RequestFactory(
method=self.host.async_client.ListModelRuns,
request=model_interface.ListModelRunsRequest(
view=model_definition_interface.VIEW_FULL,
namespace_id=namespace_id,
model_id=model_id,
page_size=page_size,
Expand All @@ -966,7 +966,6 @@ def list_model_runs(
return RequestFactory(
method=self.host.client.ListModelRuns,
request=model_interface.ListModelRunsRequest(
view=model_definition_interface.VIEW_FULL,
namespace_id=namespace_id,
model_id=model_id,
page_size=page_size,
Expand All @@ -976,3 +975,46 @@ def list_model_runs(
),
metadata=self.host.metadata + self.metadata,
).send_sync()

@grpc_handler
def list_model_runs_by_credit_owner(
self,
start: datetime,
stop: datetime,
page_size: int = 10,
page: int = 0,
order_by: str = "",
filter_str: str = "",
async_enabled: bool = False,
) -> model_interface.ListModelRunsByCreditOwnerResponse:
start_timestamp = timestamp_pb2.Timestamp()
start_timestamp.FromDatetime(start)
stop_timestamp = timestamp_pb2.Timestamp()
stop_timestamp.FromDatetime(stop)

if async_enabled:
return RequestFactory(
method=self.host.async_client.ListModelRunsByCreditOwner,
request=model_interface.ListModelRunsByCreditOwnerRequest(
start=start_timestamp,
stop=stop_timestamp,
page_size=page_size,
page=page,
order_by=order_by,
filter=filter_str,
),
metadata=self.host.metadata + self.metadata,
).send_async()

return RequestFactory(
method=self.host.client.ListModelRunsByCreditOwner,
request=model_interface.ListModelRunsByCreditOwnerRequest(
start=start_timestamp,
stop=stop_timestamp,
page_size=page_size,
page=page,
order_by=order_by,
filter=filter_str,
),
metadata=self.host.metadata + self.metadata,
).send_sync()
Loading

0 comments on commit c6d8436

Please sign in to comment.