11import requests
22from dataclasses import dataclass
3- from typing import List , Optional
4- from enum import Enum , auto
3+ from typing import Optional
4+ from enum import Enum
55import time
66
77
8- @dataclass
9- class Asset :
10- name : str
11- url : str
12-
13-
148class JobStatus (Enum ):
15- PENDING = auto ()
16- COMPLETED = auto ()
17- FAILED = auto ()
18- UPLOADING = auto ()
9+ PENDING = "pending"
10+ COMPLETED = "completed"
11+ FAILED = "failed"
12+ UPLOADING = "uploading"
1913
2014
2115@dataclass
2216class JobEntity :
2317 id : int
2418 status : JobStatus
2519 created_at : str
26- assets : Optional [List [Asset ]] = None
20+ logs : Optional [str ] = None
21+ outputs : Optional [dict ] = None
22+ completed_at : Optional [str ] = None
23+
24+
25+ def _response_to_job (data : dict ) -> JobEntity :
26+ data ['status' ] = JobStatus [data ['status' ].upper ()]
27+ return JobEntity (** data )
2728
2829
2930class StreamPotClient :
@@ -32,19 +33,17 @@ def __init__(self, secret: str, base_url: str = 'https://api.streampot.io/v1'):
3233 self .base_url = base_url
3334 self .actions = []
3435
35- def get_job (self , job_id : int ) -> dict :
36+ def get_job (self , job_id : int ) -> JobEntity :
3637 response = requests .get (f"{ self .base_url } /jobs/{ job_id } " , headers = self ._auth_header ())
3738 response .raise_for_status ()
38- return response .json ()
39+
40+ return _response_to_job (response .json ())
3941
4042 def run (self ) -> JobEntity :
41- response = requests .post (f"{ self .base_url } /" , headers = self ._auth_header (json = True ), json = self .actions )
43+ response = requests .post (f"{ self .base_url } /" , headers = self ._auth_header (), json = self .actions )
4244 response .raise_for_status ()
4345
44- job_data = response .json ()
45- job_data ['status' ] = JobStatus [job_data ['status' ].upper ()]
46-
47- return JobEntity (** job_data )
46+ return _response_to_job (response .json ())
4847
4948 def run_and_wait (self , interval_ms : int = 1000 ) -> JobEntity :
5049 job = self .run ()
@@ -53,15 +52,16 @@ def run_and_wait(self, interval_ms: int = 1000) -> JobEntity:
5352 job = self .get_job (job .id )
5453 return job
5554
56- def _auth_header (self , json : bool = False ) -> dict :
57- headers = { "Authorization" : f"Bearer { self . secret } " }
58- if json :
59- headers [ ' Accept' ] = 'application/json'
60- headers [ ' Content-Type' ] = 'application/json'
61- return headers
55+ def _auth_header (self ) -> dict :
56+ return {
57+ "Authorization" : f"Bearer { self . secret } " ,
58+ " Accept" : 'application/json' ,
59+ " Content-Type" : 'application/json'
60+ }
6261
6362 def _add_action (self , name : str , * values ):
64- self .actions .append ({"name" : name , "value" : values })
63+ self .actions .append ({"name" : name , "value" : list (values )})
64+ return self
6565
6666 def merge_add (self , source : str ):
6767 return self ._add_action ('mergeAdd' , source )
0 commit comments