Skip to content

Commit

Permalink
refactor: restructure the code of bentocloud client
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Sep 12, 2024
1 parent 6f94ff6 commit cb02c61
Show file tree
Hide file tree
Showing 22 changed files with 1,084 additions and 962 deletions.
2 changes: 1 addition & 1 deletion src/_bentoml_sdk/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def resolve(
target_model_store=model_store,
)
return stored
model = cloud_client.pull_model(self.tag, model_store=model_store)
model = cloud_client.model.pull(self.tag, model_store=model_store)
assert model is not None, "non-bentoml model"
return model

Expand Down
4 changes: 2 additions & 2 deletions src/bentoml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

# BentoML built-in types
from ._internal.bento import Bento
from ._internal.cloud import YataiClient
from ._internal.cloud import BentoCloudClient
from ._internal.context import ServiceContext as Context
from ._internal.context import server_context
from ._internal.models import Model
Expand Down Expand Up @@ -244,7 +244,7 @@ def __getattr__(name: str) -> Any:
"Runner",
"Runnable",
"monitoring",
"YataiClient", # Yatai REST API Client
"BentoCloudClient", # BentoCloud REST API Client
# bento APIs
"list",
"get",
Expand Down
63 changes: 61 additions & 2 deletions src/bentoml/_internal/cloud/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
from .base import CloudClient as CloudClient
from .bentocloud import BentoCloudClient as BentoCloudClient
from __future__ import annotations

import attrs

from bentoml._internal.cloud.client import RestApiClient

from .bento import BentoAPI
from .config import CloudClientConfig
from .deployment import DeploymentAPI
from .model import ModelAPI
from .secret import SecretAPI
from .yatai import YataiClient as YataiClient

DEFAULT_ENDPOINT = "https://cloud.bentoml.com"


@attrs.frozen
class BentoCloudClient:
"""
BentoCloudClient is a client for the BentoCloud API.
Args:
api_key: The API key to use for the client. env: BENTO_CLOUD_API_KEY
endpoint: The endpoint to use for the client. env: BENTO_CLOUD_ENDPOINT
Attributes:
bento: BentoAPI
model: ModelAPI
deployment: Deployment
"""

bento: BentoAPI
model: ModelAPI
deployment: DeploymentAPI
secret: SecretAPI

def __init__(
self, api_key: str | None = None, endpoint: str = DEFAULT_ENDPOINT
) -> None:
if api_key is None:
from ..configuration.containers import BentoMLContainer

cfg = CloudClientConfig.get_config()
ctx = cfg.get_context(BentoMLContainer.cloud_context.get())
api_key = ctx.api_token
endpoint = ctx.endpoint

client = RestApiClient(endpoint, api_key)
bento = BentoAPI(client)
model = ModelAPI(client)
deployment = DeploymentAPI(client)
secret = SecretAPI(client)

self.__attrs_init__(
bento=bento, model=model, deployment=deployment, secret=secret
)

@classmethod
def for_context(cls, context: str | None = None) -> "BentoCloudClient":
cfg = CloudClientConfig.get_config()
ctx = cfg.get_context(context)
return cls(ctx.api_token, ctx.endpoint)
7 changes: 1 addition & 6 deletions src/bentoml/_internal/cloud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from rich.console import RenderResult

FILE_CHUNK_SIZE = 100 * 1024 * 1024 # 100Mb
UPLOAD_RETRY_COUNT = 3


@attrs.define
Expand Down Expand Up @@ -168,9 +169,3 @@ def __enter__(self) -> Spinner:

def __exit__(self, *_: t.Any) -> None:
self.stop()


class CloudClient:
# Moved atrributes to __init__ because otherwise it will keep all the log when running SDK.
def __init__(self):
self.spinner = Spinner()
Loading

0 comments on commit cb02c61

Please sign in to comment.