11import asyncio
22import 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
66from gitpod .types .environments .automations import service_create_params
77TASK_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