-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy path__init__.py
More file actions
89 lines (75 loc) · 2.8 KB
/
Copy path__init__.py
File metadata and controls
89 lines (75 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from typing import Optional
import dstack._internal.core.services.api_client as api_client_service
from dstack._internal.core.errors import ConfigurationError
from dstack._internal.utils.logging import get_logger
from dstack._internal.utils.path import PathLike as PathLike
from dstack.api._public.backends import BackendCollection
from dstack.api._public.repos import RepoCollection
from dstack.api._public.runs import RunCollection
from dstack.api.server import APIClient
logger = get_logger(__name__)
class Client:
"""
High-level API client for interacting with the `dstack` server
Attributes:
project: The project name.
runs: Operations with runs.
repos: Operations with repositories.
backends: Operations with backends.
client: Low-level API client that supports all API endpoints.
"""
def __init__(
self,
api_client: APIClient,
project_name: str,
):
# """
# Args:
# api_client: low-level server API client
# project_name: project name used for runs
# """
self._client = api_client
self._project = project_name
self._repos = RepoCollection(api_client, project_name)
self._backends = BackendCollection(api_client, project_name)
self._runs = RunCollection(api_client, project_name, self)
@staticmethod
def from_config(
project_name: Optional[str] = None,
server_url: Optional[str] = None,
user_token: Optional[str] = None,
) -> "Client":
"""
Creates a Client using the default configuration from `~/.dstack/config.yml` if it exists.
Args:
project_name: The name of the project. required if `server_url` and `user_token` are specified.
server_url: The dstack server URL (e.g. `http://localhost:3000/` or `https://sky.dstack.ai`).
user_token: The dstack user token.
Returns:
A client instance.
"""
if server_url is not None and user_token is not None:
if project_name is None:
raise ConfigurationError("The project name is not specified")
api_client = APIClient(server_url, user_token)
else:
api_client, project_name = api_client_service.get_api_client(project_name=project_name)
return Client(
api_client=api_client,
project_name=project_name,
)
@property
def project(self) -> str:
return self._project
@property
def runs(self) -> RunCollection:
return self._runs
@property
def repos(self) -> RepoCollection:
return self._repos
@property
def backends(self) -> BackendCollection:
return self._backends
@property
def client(self) -> APIClient:
return self._client