Creates a task based on the provided payload and returns the task ID. This method is responsible for initiating a task by preparing the request with the provided task details and optional callback URL, sending a synchronous request to the task creation endpoint, and
(self, task: TaskPayload, callback_url: Optional[str] = None)
| 72 | return response.json().get("balance", 0.0) |
| 73 | |
| 74 | def create_task(self, task: TaskPayload, callback_url: Optional[str] = None) -> int: |
| 75 | """ |
| 76 | Creates a task based on the provided payload and returns the task ID. |
| 77 | |
| 78 | This method is responsible for initiating a task by preparing the request with the |
| 79 | provided task details and optional callback URL, sending a synchronous request to the |
| 80 | task creation endpoint, and parsing the response to retrieve the task ID. |
| 81 | |
| 82 | Args: |
| 83 | task: The task configuration payload that defines the specifics of the task to be |
| 84 | created. |
| 85 | callback_url: An optional URL to be called back upon task completion or update. |
| 86 | |
| 87 | Returns: |
| 88 | The unique identifier of the created task. |
| 89 | |
| 90 | Raises: |
| 91 | CapmonsterException: If the task creation request fails or the response is not contains valid task id. |
| 92 | """ |
| 93 | payload = CreateTaskPayload(clientKey=self.api_key, task=task, callbackUrl=callback_url).model_dump( |
| 94 | exclude_none=True) |
| 95 | payload["softId"] = 30 |
| 96 | response = self.__make_sync_request(self.__CREATE_TASK_URL, payload).json() |
| 97 | return response.get("taskId", 0) |
| 98 | |
| 99 | async def create_task_async(self, task: TaskPayload, callback_url: Optional[str] = None) -> int: |
| 100 | """ |