Skip to content

Commit fe32f8a

Browse files
committed
WIP
1 parent 88d9238 commit fe32f8a

File tree

2 files changed

+57
-40
lines changed

2 files changed

+57
-40
lines changed

datacrunch/containers/containers.py

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import requests
12
from dataclasses import dataclass
23
from dataclasses_json import dataclass_json, Undefined # type: ignore
3-
from typing import List, Optional, Dict
4+
from typing import List, Optional, Dict, Any
45
from enum import Enum
56

7+
from datacrunch.http_client.http_client import HTTPClient
8+
69

710
# API endpoints
811
CONTAINER_DEPLOYMENTS_ENDPOINT = '/container-deployments'
@@ -256,39 +259,44 @@ class Deployment:
256259
:param is_spot: Whether is spot deployment
257260
:param endpoint_base_url: Optional base URL for the deployment endpoint
258261
:param scaling: Optional scaling configuration
262+
:param created_at: Optional timestamp when the deployment was created
263+
:param inference_key: Optional inference key for the deployment
259264
"""
260265
name: str
261266
container_registry_settings: ContainerRegistrySettings
262-
containers: List[Container]
267+
containers: List[Container] | List[ContainerInfo]
263268
compute: ComputeResource
264269
is_spot: bool = False
265270
endpoint_base_url: Optional[str] = None
266271
scaling: Optional[ScalingOptions] = None
272+
created_at: Optional[str] = None
267273

274+
inference_key: Optional[str] = None
268275

269-
@dataclass_json(undefined=Undefined.EXCLUDE)
270-
@dataclass
271-
class DeploymentInfo:
272-
"""Configuration for a container deployment.
273-
This class is read-only and includes system-managed fields.
276+
@classmethod
277+
def from_dict_with_inference_key(cls, data: Dict[str, Any], inference_key: str = None, **kwargs) -> 'Deployment':
278+
"""Create a Deployment instance from a dictionary with an inference key.
274279
275-
:param name: Name of the deployment
276-
:param container_registry_settings: Settings for accessing container registry
277-
:param containers: List of containers in the deployment
278-
:param compute: Compute resource configuration
279-
:param is_spot: Whether is spot deployment
280-
:param endpoint_base_url: Optional base URL for the deployment endpoint
281-
:param scaling: Optional scaling configuration
282-
:param created_at: Timestamp when the deployment was created
283-
"""
284-
name: str
285-
container_registry_settings: ContainerRegistrySettings
286-
containers: List[ContainerInfo]
287-
compute: ComputeResource
288-
is_spot: bool = False
289-
endpoint_base_url: Optional[str] = None
290-
scaling: Optional[ScalingOptions] = None
291-
created_at: Optional[str] = None
280+
:param data: Dictionary containing deployment data
281+
:param inference_key: inference key to set on the deployment
282+
:param **kwargs: Additional arguments to pass to from_dict
283+
:return: Deployment instance
284+
"""
285+
deployment = cls.from_dict(data, **kwargs)
286+
deployment._inference_key = inference_key
287+
return deployment
288+
289+
def run_sync(self, data: Dict[str, Any], path: str = "", timeout_seconds: int = 60 * 5):
290+
if self._inference_key is None:
291+
raise ValueError("Inference key is not set") # TODO: review this
292+
293+
# TODO: create a request object
294+
return requests.post(
295+
url=f"{self.endpoint_base_url}{path}",
296+
json=data,
297+
headers={"Authorization": f"Bearer {self._inference_key}"},
298+
timeout=timeout_seconds
299+
)
292300

293301

294302
@dataclass_json
@@ -397,67 +405,71 @@ def __init__(self, name: str, docker_config_json: str):
397405
class ContainersService:
398406
"""Service for managing container deployments"""
399407

400-
def __init__(self, http_client) -> None:
408+
def __init__(self, http_client: HTTPClient, inference_key: str = None) -> None:
401409
"""Initialize the containers service
402410
403411
:param http_client: HTTP client for making API requests
404412
:type http_client: Any
405413
"""
406414
self.client = http_client
415+
self._inference_key = inference_key
407416

408-
def get_deployments(self) -> List[DeploymentInfo]:
417+
def get_deployments(self) -> List[Deployment]:
409418
"""Get all deployments
410419
411420
:return: list of deployments
412-
:rtype: List[DeploymentInfo]
421+
:rtype: List[Deployment]
413422
"""
414423
response = self.client.get(CONTAINER_DEPLOYMENTS_ENDPOINT)
415-
return [DeploymentInfo.from_dict(deployment, infer_missing=True) for deployment in response.json()]
424+
return [Deployment.from_dict(deployment, infer_missing=True) for deployment in response.json()]
416425

417-
def get_deployment_by_name(self, deployment_name: str) -> DeploymentInfo:
426+
def get_deployment_by_name(self, deployment_name: str) -> Deployment:
418427
"""Get a deployment by name
419428
420429
:param deployment_name: name of the deployment
421430
:type deployment_name: str
422431
:return: deployment
423-
:rtype: DeploymentInfo
432+
:rtype: Deployment
424433
"""
425434
response = self.client.get(
426435
f"{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}")
427-
return DeploymentInfo.from_dict(response.json(), infer_missing=True)
436+
return Deployment.from_dict_with_inference_key(response.json(), self._inference_key)
437+
438+
# Function alias
439+
get_deployment = get_deployment_by_name
428440

429441
def create_deployment(
430442
self,
431443
deployment: Deployment
432-
) -> DeploymentInfo:
444+
) -> Deployment:
433445
"""Create a new deployment
434446
435447
:param deployment: deployment configuration
436448
:type deployment: Deployment
437449
:return: created deployment
438-
:rtype: DeploymentInfo
450+
:rtype: Deployment
439451
"""
440452
response = self.client.post(
441453
CONTAINER_DEPLOYMENTS_ENDPOINT,
442454
deployment.to_dict()
443455
)
444-
return DeploymentInfo.from_dict(response.json(), infer_missing=True)
456+
return Deployment.from_dict(response.json(), infer_missing=True)
445457

446-
def update_deployment(self, deployment_name: str, deployment: DeploymentInfo) -> DeploymentInfo:
458+
def update_deployment(self, deployment_name: str, deployment: Deployment) -> Deployment:
447459
"""Update an existing deployment
448460
449461
:param deployment_name: name of the deployment to update
450462
:type deployment_name: str
451463
:param deployment: updated deployment
452-
:type deployment: DeploymentInfo
464+
:type deployment: Deployment
453465
:return: updated deployment
454-
:rtype: DeploymentInfo
466+
:rtype: Deployment
455467
"""
456468
response = self.client.patch(
457469
f"{CONTAINER_DEPLOYMENTS_ENDPOINT}/{deployment_name}",
458470
deployment.to_dict()
459471
)
460-
return DeploymentInfo.from_dict(response.json(), infer_missing=True)
472+
return Deployment.from_dict(response.json(), infer_missing=True)
461473

462474
def delete_deployment(self, deployment_name: str) -> None:
463475
"""Delete a deployment
@@ -661,6 +673,9 @@ def get_compute_resources(self) -> List[ComputeResource]:
661673
resources.append(ComputeResource.from_dict(resource))
662674
return resources
663675

676+
# Function alias
677+
get_gpus = get_compute_resources
678+
664679
def get_secrets(self) -> List[Secret]:
665680
"""Get all secrets
666681

datacrunch/datacrunch.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class DataCrunchClient:
1818
"""Client for interacting with DataCrunch's public API"""
1919

20-
def __init__(self, client_id: str, client_secret: str, base_url: str = "https://api.datacrunch.io/v1") -> None:
20+
def __init__(self, client_id: str, client_secret: str, base_url: str = "https://api.datacrunch.io/v1", inference_key: str = None) -> None:
2121
"""The DataCrunch client
2222
2323
:param client_id: client id
@@ -26,6 +26,8 @@ def __init__(self, client_id: str, client_secret: str, base_url: str = "https://
2626
:type client_secret: str
2727
:param base_url: base url for all the endpoints, optional, defaults to "https://api.datacrunch.io/v1"
2828
:type base_url: str, optional
29+
:param inference_key: inference key, optional
30+
:type inference_key: str, optional
2931
"""
3032

3133
# Constants
@@ -70,5 +72,5 @@ def __init__(self, client_id: str, client_secret: str, base_url: str = "https://
7072
"""Locations service. Get locations"""
7173

7274
self.containers: ContainersService = ContainersService(
73-
self._http_client)
75+
self._http_client, inference_key)
7476
"""Containers service. Deploy, manage, and monitor container deployments"""

0 commit comments

Comments
 (0)