From e5c050be32f4f7ba322c13f497f97f554e7de299 Mon Sep 17 00:00:00 2001 From: will2z Date: Thu, 28 Dec 2023 19:14:04 +0800 Subject: [PATCH] add: deployment --- swan_lag/api/deployment.py | 35 +++++++++++++++++++++ swan_lag/api_client.py | 6 ++++ swan_lag/model/deployment.py | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 swan_lag/api/deployment.py create mode 100644 swan_lag/model/deployment.py diff --git a/swan_lag/api/deployment.py b/swan_lag/api/deployment.py new file mode 100644 index 0000000..673f507 --- /dev/null +++ b/swan_lag/api/deployment.py @@ -0,0 +1,35 @@ +from base64 import b64decode + +from swan_lag.api_client import APIClient +from swan_lag.model.deployment import * +from swan_lag.common.constants import * + +METHOD_SPACE_DEPLOYMENT="/spaces/{}/deployment" + +class DeploymentAPI(APIClient): + def __init__(self, api_key=None, is_calibration=False): + api_client = APIClient(api_key=api_key,is_testnet=is_calibration,login=False) + self.WithClient(api_client) + + @classmethod + def WithClient(self, api_client: APIClient): + self.api_client = api_client + self.LAG_API = api_client.LAG_API + self.token = self.api_client.token + self.account = self.api_client.account + self.rpc = self.api_client.rpc + + def get_deployment(self, space_uuid: str): + self.api_client.get_request(METHOD_SPACE_DEPLOYMENT.format(space_uuid)) + + def renew_deployment(self, space_uuid: str, duration: int, paid, tx_hash: str, chain_id:str): + self.api_client.post_request(METHOD_SPACE_DEPLOYMENT.format(space_uuid), { + "duration": duration, + "paid": paid, + "tx_hash": tx_hash, + "chain_id": chain_id, + }) + + + + diff --git a/swan_lag/api_client.py b/swan_lag/api_client.py index badea51..ae4bef5 100644 --- a/swan_lag/api_client.py +++ b/swan_lag/api_client.py @@ -70,3 +70,9 @@ def _request_without_params(self, method, request_path, lag_api, token): def _request_with_params(self, method, request_path, lag_api, params, token, files): return self._request(method, request_path, lag_api, params, token, files) + + def get_request(self, method_path, params=None): + return self._request(method_path, c.GET, self.LAG_API, params, self.token) + + def post_request(self, method_path, params=None): + return self._request(method_path, c.POST, self.LAG_API, params, self.token) diff --git a/swan_lag/model/deployment.py b/swan_lag/model/deployment.py new file mode 100644 index 0000000..6e1b83b --- /dev/null +++ b/swan_lag/model/deployment.py @@ -0,0 +1,61 @@ +class Task(object): + def __init__(self, name: str, uuid: str, status: str, leading_job_id, task_detail_cid: str, created_at, updated_at): + self.name = name + self.uuid = uuid + self.status = status + self.leading_job_id = leading_job_id + self.task_detail_cid = task_detail_cid + self.created_at = created_at + self.updated_at = updated_at + + +class Job(object): + def __init__(self, uuid, status, job_result_uri): + self.uuid = uuid + self.status = status + self.job_result_uri = job_result_uri + + +class Config(object): + def __init__(self, name: str, hardware_id: int, hardware: str, hardware_type: str, memory: int, vcpu: int, price_per_hour: float, description: str): + self.name = name + self.hardware_id = hardware_id + self.hardware = hardware + self.hardware_type = hardware_type + self.memory = memory + self.vcpu = vcpu + self.price_per_hour = price_per_hour + self.description = description + + +class Order(object): + def __init__(self, config: Config, duration: int, region: str): + self.config = config + self.duration = duration + self.region = region + + +class Space(object): + def __init__(self, order: Order, name: str, uuid: str, is_public: bool, license: str, expiration_time: str, likes: int, forked_space_uuid: str): + self.order = order + self.name = name + self.uuid = uuid + self.is_public = is_public + self.license = license + self.expiration_time = expiration_time + self.likes = likes + self.forked_space_uuid = forked_space_uuid + + +class SpaceDeployment(object): + def __init__(self, space: Space, jobs: list[Job], task: Task): + self.space = space + self.jobs = jobs + self.task = task + + +class Result(object): + def __init__(self, code: int, msg: str, data: any): + self.code = code + self.msg = msg + self.data = data