1
1
import asyncio
2
2
import httpx
3
- from typing import AsyncIterator
3
+ from typing import AsyncIterator , Awaitable , Callable , Optional
4
4
5
- from gitpod import Gitpod
5
+ from gitpod . _client import AsyncGitpod
6
6
from gitpod .types .environments .automations import service_create_params
7
7
TASK_REFERENCE = "gitpod-python-sdk"
8
8
9
- def run_service (
10
- client : Gitpod ,
9
+ async def run_service (
10
+ client : AsyncGitpod ,
11
11
environment_id : str ,
12
12
metadata : service_create_params .ServiceMetadataParam ,
13
13
spec : service_create_params .ServiceSpecParam
@@ -16,38 +16,38 @@ def run_service(
16
16
if not reference :
17
17
raise ValueError ("metadata.reference is required" )
18
18
19
- services = client .environments .automations .services .list (
19
+ services = ( await client .environments .automations .services .list (
20
20
filter = {
21
21
"references" : [reference ],
22
22
"environment_ids" : [environment_id ]
23
23
}
24
- ).services
24
+ )) .services
25
25
26
26
service_id = ""
27
27
if not services :
28
- service_id = client .environments .automations .services .create (
28
+ service_id = ( await client .environments .automations .services .create (
29
29
environment_id = environment_id ,
30
30
spec = spec ,
31
31
metadata = metadata
32
- ).service .id
32
+ )) .service .id
33
33
else :
34
34
service_id = services [0 ].id
35
35
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 )
38
38
return stream_logs (client , environment_id , log_url )
39
39
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 (
42
42
filter = {
43
43
"references" : [TASK_REFERENCE ],
44
44
"environment_ids" : [environment_id ]
45
45
}
46
- ).tasks
46
+ )) .tasks
47
47
48
48
task_id = ""
49
49
if not tasks :
50
- task_id = client .environments .automations .tasks .create (
50
+ task_id = ( await client .environments .automations .tasks .create (
51
51
spec = {
52
52
"command" : command ,
53
53
},
@@ -57,57 +57,57 @@ def run_command(client: Gitpod, environment_id: str, command: str) -> AsyncItera
57
57
"description" : "Gitpod Python SDK Task" ,
58
58
"reference" : TASK_REFERENCE ,
59
59
},
60
- ).task .id
60
+ )) .task .id
61
61
else :
62
62
task_id = tasks [0 ].id
63
- client .environments .automations .tasks .update (
63
+ await client .environments .automations .tasks .update (
64
64
id = task_id ,
65
65
spec = {
66
66
"command" : command ,
67
67
},
68
68
)
69
69
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 )
72
72
return stream_logs (client , environment_id , log_url )
73
73
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
77
77
return execution .status .log_url
78
78
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" )
80
80
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
84
84
if service .status .phase != "SERVICE_PHASE_RUNNING" :
85
85
return None
86
86
return service .status .log_url
87
87
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" )
89
89
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 ()
92
92
if log_url :
93
93
return log_url
94
94
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 )
96
96
try :
97
- log_url = get_log_url_fn ()
97
+ log_url = await get_log_url_fn ()
98
98
if log_url :
99
99
return log_url
100
100
101
- for event in event_stream :
101
+ async for event in event_stream :
102
102
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 ()
104
104
if log_url is not None :
105
105
return log_url
106
106
finally :
107
- event_stream .http_response .close ()
107
+ await event_stream .http_response .aclose ()
108
108
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
111
111
async with httpx .AsyncClient () as http_client :
112
112
retries = 3
113
113
while retries > 0 :
0 commit comments