1+ import time
12import requests
23from requests .exceptions import HTTPError
34
5+
46class MusicAiClient :
5- def __init__ (self , api_key ):
7+ def __init__ (self , api_key , job_monitor_interval = 2 ):
68 self .api_key = api_key
79 self .base_url = 'https://api.music.ai/api'
10+ self .job_monitor_interval = job_monitor_interval
811
912 def get_headers (self ):
1013 return {
@@ -28,6 +31,12 @@ def get_job(self, job_id):
2831 raise HTTPError (f'Error getting job: { response .status_code } { response .text } ' )
2932 return response .json ()
3033
34+ def get_job_status (self , job_id ):
35+ response = requests .get (f'{ self .base_url } /job/{ job_id } /status' , headers = self .get_headers ())
36+ if response .status_code // 100 != 2 :
37+ raise HTTPError (f'Error getting job: { response .status_code } { response .text } ' )
38+ return response .json ()
39+
3140 def get_jobs (self ):
3241 response = requests .get (f'{ self .base_url } /job' , headers = self .get_headers ())
3342 if response .status_code // 100 != 2 :
@@ -51,8 +60,15 @@ def delete_job(self, job_id):
5160 raise HTTPError (f'Error deleting job: { response .status_code } { response .text } ' )
5261 return response .json ()
5362
63+ def wait_for_job_completion (self , id ):
64+ while True :
65+ job = self .get_job_status (id )
66+ if job ['status' ] in ['SUCCEEDED' , 'FAILED' ]:
67+ return self .get_job (id )
68+ time .sleep (self .job_monitor_interval ),
69+
5470 def get_application_info (self ):
5571 response = requests .get (f'{ self .base_url } /application' , headers = self .get_headers ())
5672 if response .status_code // 100 != 2 :
5773 raise HTTPError (f'Error getting application info: { response .status_code } { response .text } ' )
58- return response .json ()
74+ return response .json ()
0 commit comments