Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide/client: add timeout in seconds #26

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion hide/client/hide_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,31 @@ def run_task(
project_id: str,
command: Optional[str] = None,
alias: Optional[str] = None,
timeout: Optional[int] = None,
) -> model.TaskResult:
if not command and not alias:
raise HideClientError("Either 'command' or 'alias' must be provided")

if command and alias:
raise HideClientError("Cannot provide both 'command' and 'alias'")

if timeout and timeout <= 0:
raise HideClientError("Timeout must be a positive integer")

payload = {}
if command:
payload["command"] = command
if alias:
payload["alias"] = alias

headers = None
if timeout:
headers = headers = {"X-Timeout-Seconds": str(timeout)}

response = requests.post(
f"{self.base_url}/projects/{project_id}/tasks", json=payload
f"{self.base_url}/projects/{project_id}/tasks",
json=payload,
headers=headers,
)
if not response.ok:
raise HideClientError(response.text)
Expand Down
7 changes: 5 additions & 2 deletions hide/toolkit/toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ def get_tasks(self) -> str:
return f"Failed to get tasks: {e}"

def run_task(
self, command: Optional[str] = None, alias: Optional[str] = None
self,
command: Optional[str] = None,
alias: Optional[str] = None,
timeout: Optional[int] = None,
) -> str:
"""
Run a task in the project. Provide either command or alias. Command will be executed in the shell.
Run a task in the project. Provide either command or alias. Set timeout in seconds. Command will be executed in the shell.
For the list of available tasks and their aliases, use the `get_tasks` tool.
"""
try:
Expand Down
8 changes: 6 additions & 2 deletions tests/test_hide_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def test_run_task_command_success(client):
result = client.run_task(PROJECT_ID, command="echo Hello")
assert result == model.TaskResult(stdout="output", stderr="", exit_code=0)
mock_post.assert_called_once_with(
"http://localhost/projects/123/tasks", json={"command": "echo Hello"}
"http://localhost/projects/123/tasks",
json={"command": "echo Hello"},
headers=None,
)


Expand All @@ -117,7 +119,9 @@ def test_run_task_alias_success(client):
result = client.run_task(PROJECT_ID, alias="build")
assert result == model.TaskResult(stdout="output", stderr="", exit_code=0)
mock_post.assert_called_once_with(
"http://localhost/projects/123/tasks", json={"alias": "build"}
"http://localhost/projects/123/tasks",
json={"alias": "build"},
headers=None,
)


Expand Down
Loading