1
1
import requests
2
2
from 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
5
5
import time
6
6
7
7
8
- @dataclass
9
- class Asset :
10
- name : str
11
- url : str
12
-
13
-
14
8
class 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"
19
13
20
14
21
15
@dataclass
22
16
class JobEntity :
23
17
id : int
24
18
status : JobStatus
25
19
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 )
27
28
28
29
29
30
class StreamPotClient :
@@ -32,19 +33,17 @@ def __init__(self, secret: str, base_url: str = 'https://api.streampot.io/v1'):
32
33
self .base_url = base_url
33
34
self .actions = []
34
35
35
- def get_job (self , job_id : int ) -> dict :
36
+ def get_job (self , job_id : int ) -> JobEntity :
36
37
response = requests .get (f"{ self .base_url } /jobs/{ job_id } " , headers = self ._auth_header ())
37
38
response .raise_for_status ()
38
- return response .json ()
39
+
40
+ return _response_to_job (response .json ())
39
41
40
42
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 )
42
44
response .raise_for_status ()
43
45
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 ())
48
47
49
48
def run_and_wait (self , interval_ms : int = 1000 ) -> JobEntity :
50
49
job = self .run ()
@@ -53,15 +52,16 @@ def run_and_wait(self, interval_ms: int = 1000) -> JobEntity:
53
52
job = self .get_job (job .id )
54
53
return job
55
54
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
+ }
62
61
63
62
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
65
65
66
66
def merge_add (self , source : str ):
67
67
return self ._add_action ('mergeAdd' , source )
0 commit comments