Skip to content

Commit 67742c3

Browse files
committed
switch to async client
1 parent 613eb90 commit 67742c3

File tree

6 files changed

+117
-102
lines changed

6 files changed

+117
-102
lines changed

examples/anthropic_tool_use.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from __future__ import annotations
22

3-
from gitpod import Gitpod
3+
from gitpod import AsyncGitpod
44
import gitpod.lib as util
55
from anthropic import Anthropic
66
from anthropic.types import ToolParam, MessageParam
77

8-
gpclient = Gitpod()
8+
gpclient = AsyncGitpod()
99
llmclient = Anthropic()
1010

1111
user_message: MessageParam = {
@@ -32,31 +32,31 @@
3232
]
3333

3434
async def create_environment(args: dict[str, str]) -> str:
35-
env_class = util.find_most_used_environment_class(gpclient)
35+
env_class = await util.find_most_used_environment_class(gpclient)
3636
if not env_class:
3737
raise Exception("No environment class found. Please create one first.")
38-
environment_id = gpclient.environments.create(
38+
environment_id = (await gpclient.environments.create(
3939
spec={
4040
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
4141
"content": {
4242
"initializer": {"specs": [{"contextUrl": {"url": args["context_url"]}}]},
4343
},
4444
"machine": {"class": env_class.id},
4545
}
46-
).environment.id
46+
)).environment.id
4747
print(f"\nCreated environment: {environment_id} - waiting for it to be ready...")
4848
await util.wait_for_environment_ready(gpclient, environment_id)
4949
print(f"\nEnvironment is ready: {environment_id}")
5050
return environment_id
5151

5252
async def execute_command(args: dict[str, str]) -> str:
53-
lines_iter = util.run_command(gpclient, args["environment_id"], args["command"])
53+
lines_iter = await util.run_command(gpclient, args["environment_id"], args["command"])
5454
lines = []
5555
async for line in lines_iter:
5656
lines.append(line)
5757
return "\n".join(lines)
5858

59-
async def main():
59+
async def main(cleanup: util.Disposables):
6060
messages = [user_message]
6161
while True:
6262
message = llmclient.messages.create(
@@ -80,6 +80,7 @@ async def main():
8080
try:
8181
if tool.name == "create_environment":
8282
environment_id = await create_environment(tool.input)
83+
cleanup.add(lambda: asyncio.run(gpclient.environments.delete(environment_id=environment_id)))
8384
messages.append({
8485
"role": "user",
8586
"content": [{
@@ -115,4 +116,6 @@ async def main():
115116

116117
if __name__ == "__main__":
117118
import asyncio
118-
asyncio.run(main())
119+
disposables = util.Disposables()
120+
with disposables:
121+
asyncio.run(main(disposables))

examples/fs_access.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
import sys
55
import asyncio
66
import paramiko
7-
from gitpod import Gitpod
7+
from gitpod import AsyncGitpod
88
import gitpod.lib as util
99
from gitpod.types.environment_spec_param import EnvironmentSpecParam
1010

1111
# Examples:
1212
# - ./examples/fs_access.py
1313
# - ./examples/fs_access.py https://github.com/gitpod-io/empty
1414
async def main(cleanup: util.Disposables) -> None:
15-
client = Gitpod()
15+
client = AsyncGitpod()
1616

1717
context_url = sys.argv[1] if len(sys.argv) > 1 else None
1818

19-
env_class = util.find_most_used_environment_class(client)
19+
env_class = await util.find_most_used_environment_class(client)
2020
if not env_class:
2121
print("Error: No environment class found. Please create one first.")
2222
sys.exit(1)
@@ -48,8 +48,8 @@ async def main(cleanup: util.Disposables) -> None:
4848
}]}
4949
}
5050

51-
environment_id = client.environments.create(spec=spec).environment.id
52-
cleanup.add(lambda: client.environments.delete(environment_id=environment_id))
51+
environment_id = (await client.environments.create(spec=spec)).environment.id
52+
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
5353

5454
env = util.EnvironmentState(client, environment_id)
5555
cleanup.add(lambda: asyncio.run(env.close()))

examples/run_command.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import sys
44
import asyncio
5-
from gitpod import Gitpod
5+
from gitpod import AsyncGitpod
66
import gitpod.lib as util
77
from gitpod.types.environment_spec_param import EnvironmentSpecParam
88

99
# Examples:
1010
# - ./examples/run_command.py 'echo "Hello World!"'
1111
# - ./examples/run_command.py 'echo "Hello World!"' https://github.com/gitpod-io/empty
1212
async def main(cleanup: util.Disposables) -> None:
13-
client = Gitpod()
13+
client = AsyncGitpod()
1414

1515
if len(sys.argv) < 2:
1616
print("Usage: ./examples/run_command.py '<COMMAND>' [CONTEXT_URL]")
@@ -19,7 +19,7 @@ async def main(cleanup: util.Disposables) -> None:
1919
command = sys.argv[1]
2020
context_url = sys.argv[2] if len(sys.argv) > 2 else None
2121

22-
env_class = util.find_most_used_environment_class(client)
22+
env_class = await util.find_most_used_environment_class(client)
2323
if not env_class:
2424
print("Error: No environment class found. Please create one first.")
2525
sys.exit(1)
@@ -39,14 +39,14 @@ async def main(cleanup: util.Disposables) -> None:
3939
}
4040

4141
print("Creating environment")
42-
environment_id = client.environments.create(spec=spec).environment.id
43-
cleanup.add(lambda: client.environments.delete(environment_id=environment_id))
42+
environment_id = (await client.environments.create(spec=spec)).environment.id
43+
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
4444

4545
print("Waiting for environment to be ready")
4646
await util.wait_for_environment_ready(client, environment_id)
4747

4848
print("Running command")
49-
lines = util.run_command(client, environment_id, command)
49+
lines = await util.run_command(client, environment_id, command)
5050
async for line in lines:
5151
print(line)
5252

examples/run_service.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
import sys
44
import asyncio
5-
from gitpod import Gitpod
5+
from gitpod import AsyncGitpod
66
import gitpod.lib as util
77
from gitpod.types.environment_spec_param import EnvironmentSpecParam
88

99
# Examples:
1010
# - ./examples/run_service.py
1111
# - ./examples/run_service.py https://github.com/gitpod-io/empty
1212
async def main(cleanup: util.Disposables) -> None:
13-
client = Gitpod()
13+
client = AsyncGitpod()
1414

1515
context_url = sys.argv[1] if len(sys.argv) > 1 else None
1616

17-
env_class = util.find_most_used_environment_class(client)
17+
env_class = await util.find_most_used_environment_class(client)
1818
if not env_class:
1919
print("Error: No environment class found. Please create one first.")
2020
sys.exit(1)
@@ -40,16 +40,16 @@ async def main(cleanup: util.Disposables) -> None:
4040
}
4141

4242
print("Creating environment")
43-
environment_id = client.environments.create(spec=spec).environment.id
44-
cleanup.add(lambda: client.environments.delete(environment_id=environment_id))
43+
environment_id = (await client.environments.create(spec=spec)).environment.id
44+
cleanup.add(lambda: asyncio.run(client.environments.delete(environment_id=environment_id)))
4545

4646
print("Waiting for environment to be ready")
4747
env = util.EnvironmentState(client, environment_id)
4848
cleanup.add(lambda: asyncio.run(env.close()))
4949
await env.wait_until_running()
5050

5151
print("Starting Lama Service")
52-
lines = util.run_service(client, environment_id, {
52+
lines = await util.run_service(client, environment_id, {
5353
"name":"Lama Service",
5454
"description":"Lama Service",
5555
"reference":"lama-service"

src/gitpod/lib/automation.py

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import asyncio
22
import httpx
3-
from typing import AsyncIterator
3+
from typing import AsyncIterator, Awaitable, Callable, Optional
44

5-
from gitpod import Gitpod
5+
from gitpod._client import AsyncGitpod
66
from gitpod.types.environments.automations import service_create_params
77
TASK_REFERENCE = "gitpod-python-sdk"
88

9-
def run_service(
10-
client: Gitpod,
9+
async def run_service(
10+
client: AsyncGitpod,
1111
environment_id: str,
1212
metadata: service_create_params.ServiceMetadataParam,
1313
spec: service_create_params.ServiceSpecParam
@@ -16,38 +16,38 @@ def run_service(
1616
if not reference:
1717
raise ValueError("metadata.reference is required")
1818

19-
services = client.environments.automations.services.list(
19+
services = (await client.environments.automations.services.list(
2020
filter={
2121
"references": [reference],
2222
"environment_ids": [environment_id]
2323
}
24-
).services
24+
)).services
2525

2626
service_id = ""
2727
if not services:
28-
service_id = client.environments.automations.services.create(
28+
service_id = (await client.environments.automations.services.create(
2929
environment_id=environment_id,
3030
spec=spec,
3131
metadata=metadata
32-
).service.id
32+
)).service.id
3333
else:
3434
service_id = services[0].id
3535

36-
client.environments.automations.services.start(id=service_id)
37-
log_url = wait_for_service_log_url(client, environment_id, service_id)
36+
await client.environments.automations.services.start(id=service_id)
37+
log_url = await wait_for_service_log_url(client, environment_id, service_id)
3838
return stream_logs(client, environment_id, log_url)
3939

40-
def run_command(client: Gitpod, environment_id: str, command: str) -> AsyncIterator[str]:
41-
tasks = client.environments.automations.tasks.list(
40+
async def run_command(client: AsyncGitpod, environment_id: str, command: str) -> AsyncIterator[str]:
41+
tasks = (await client.environments.automations.tasks.list(
4242
filter={
4343
"references": [TASK_REFERENCE],
4444
"environment_ids": [environment_id]
4545
}
46-
).tasks
46+
)).tasks
4747

4848
task_id = ""
4949
if not tasks:
50-
task_id = client.environments.automations.tasks.create(
50+
task_id = (await client.environments.automations.tasks.create(
5151
spec={
5252
"command": command,
5353
},
@@ -57,57 +57,57 @@ def run_command(client: Gitpod, environment_id: str, command: str) -> AsyncItera
5757
"description": "Gitpod Python SDK Task",
5858
"reference": TASK_REFERENCE,
5959
},
60-
).task.id
60+
)).task.id
6161
else:
6262
task_id = tasks[0].id
63-
client.environments.automations.tasks.update(
63+
await client.environments.automations.tasks.update(
6464
id=task_id,
6565
spec={
6666
"command": command,
6767
},
6868
)
6969

70-
task_execution_id = client.environments.automations.tasks.start(id=task_id).task_execution.id
71-
log_url = wait_for_task_log_url(client, environment_id, task_execution_id)
70+
task_execution_id = (await client.environments.automations.tasks.start(id=task_id)).task_execution.id
71+
log_url = await wait_for_task_log_url(client, environment_id, task_execution_id)
7272
return stream_logs(client, environment_id, log_url)
7373

74-
def wait_for_task_log_url(client: Gitpod, environment_id: str, task_execution_id: str) -> str:
75-
def get_log_url():
76-
execution = client.environments.automations.tasks.executions.retrieve(id=task_execution_id).task_execution
74+
async def wait_for_task_log_url(client: AsyncGitpod, environment_id: str, task_execution_id: str) -> str:
75+
async def get_log_url():
76+
execution = (await client.environments.automations.tasks.executions.retrieve(id=task_execution_id)).task_execution
7777
return execution.status.log_url
7878

79-
return wait_for_log_url(client, environment_id, task_execution_id, get_log_url, "RESOURCE_TYPE_TASK_EXECUTION")
79+
return await wait_for_log_url(client, environment_id, task_execution_id, get_log_url, "RESOURCE_TYPE_TASK_EXECUTION")
8080

81-
def wait_for_service_log_url(client: Gitpod, environment_id: str, service_id: str) -> str:
82-
def get_log_url():
83-
service = client.environments.automations.services.retrieve(id=service_id).service
81+
async def wait_for_service_log_url(client: AsyncGitpod, environment_id: str, service_id: str) -> str:
82+
async def get_log_url():
83+
service = (await client.environments.automations.services.retrieve(id=service_id)).service
8484
if service.status.phase != "SERVICE_PHASE_RUNNING":
8585
return None
8686
return service.status.log_url
8787

88-
return wait_for_log_url(client, environment_id, service_id, get_log_url, "RESOURCE_TYPE_SERVICE")
88+
return await wait_for_log_url(client, environment_id, service_id, get_log_url, "RESOURCE_TYPE_SERVICE")
8989

90-
def wait_for_log_url(client: Gitpod, environment_id: str, resource_id: str, get_log_url_fn, resource_type: str) -> str:
91-
log_url = get_log_url_fn()
90+
async def wait_for_log_url(client: AsyncGitpod, environment_id: str, resource_id: str, get_log_url_fn: Callable[[], Awaitable[Optional[str]]], resource_type: str) -> str:
91+
log_url = await get_log_url_fn()
9292
if log_url:
9393
return log_url
9494

95-
event_stream = client.events.watch(environment_id=environment_id, timeout=None)
95+
event_stream = await client.events.watch(environment_id=environment_id, timeout=None)
9696
try:
97-
log_url = get_log_url_fn()
97+
log_url = await get_log_url_fn()
9898
if log_url:
9999
return log_url
100100

101-
for event in event_stream:
101+
async for event in event_stream:
102102
if event.resource_type == resource_type and event.resource_id == resource_id:
103-
log_url = get_log_url_fn()
103+
log_url = await get_log_url_fn()
104104
if log_url is not None:
105105
return log_url
106106
finally:
107-
event_stream.http_response.close()
107+
await event_stream.http_response.aclose()
108108

109-
async def stream_logs(client: Gitpod, environment_id: str, log_url: str) -> AsyncIterator[str]:
110-
logs_access_token = client.environments.create_logs_token(environment_id=environment_id).access_token
109+
async def stream_logs(client: AsyncGitpod, environment_id: str, log_url: str) -> AsyncIterator[str]:
110+
logs_access_token = (await client.environments.create_logs_token(environment_id=environment_id)).access_token
111111
async with httpx.AsyncClient() as http_client:
112112
retries = 3
113113
while retries > 0:

0 commit comments

Comments
 (0)