-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client)!: support asyncio and add better client type hint (#55)
Because - add `asyncio` support for IO bound request tasks This commit - add `asyncio` support in each endpoint - refactor `client` return type to align with protobuf message - add `instance` class to provide better type hinting - remove global client to avoid event_loop collision between `sync` and `async` clients - misc fixes fixes INS-2793 fixes INS-2800 fixes INS-2142
- Loading branch information
Showing
11 changed files
with
1,507 additions
and
656 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import Union | ||
|
||
import grpc | ||
|
||
import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service | ||
import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service | ||
import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service | ||
|
||
|
||
class InstillInstance: | ||
def __init__(self, stub, url: str, token: str, secure: bool, async_enabled: bool): | ||
self.url: str = url | ||
self.token: str = token | ||
self.async_enabled: bool = async_enabled | ||
self.metadata: Union[str, tuple] = "" | ||
if not secure: | ||
channel = grpc.insecure_channel(url) | ||
self.metadata = ( | ||
( | ||
"authorization", | ||
f"Bearer {token}", | ||
), | ||
) | ||
if async_enabled: | ||
async_channel = grpc.aio.insecure_channel(url) | ||
else: | ||
ssl_creds = grpc.ssl_channel_credentials() | ||
call_creds = grpc.access_token_call_credentials(token) | ||
creds = grpc.composite_channel_credentials(ssl_creds, call_creds) | ||
channel = grpc.secure_channel(target=url, credentials=creds) | ||
if async_enabled: | ||
async_channel = grpc.aio.secure_channel(target=url, credentials=creds) | ||
self.channel: grpc.Channel = channel | ||
self.client: Union[ | ||
model_service.ModelPublicServiceStub, | ||
pipeline_service.PipelinePublicServiceStub, | ||
mgmt_service.MgmtPublicServiceStub, | ||
] = stub(channel) | ||
if async_enabled: | ||
self.async_channel: grpc.Channel = async_channel | ||
self.async_client: Union[ | ||
model_service.ModelPublicServiceStub, | ||
pipeline_service.PipelinePublicServiceStub, | ||
mgmt_service.MgmtPublicServiceStub, | ||
] = stub(async_channel) |
Oops, something went wrong.