11from dataclasses import dataclass
22import time
33from typing import Dict , List
4-
54import requests
5+ from nucleus .constants import (
6+ JOB_CREATION_TIME_KEY ,
7+ JOB_ID_KEY ,
8+ JOB_LAST_KNOWN_STATUS_KEY ,
9+ JOB_TYPE_KEY ,
10+ STATUS_KEY ,
11+ )
612
713JOB_POLLING_INTERVAL = 5
814
915
1016@dataclass
1117class AsyncJob :
12- id : str
18+ job_id : str
19+ job_last_known_status : str
20+ job_type : str
21+ job_creation_time : str
1322 client : "NucleusClient" # type: ignore # noqa: F821
1423
1524 def status (self ) -> Dict [str , str ]:
16- return self .client .make_request (
25+ response = self .client .make_request (
1726 payload = {},
18- route = f"job/{ self .id } " ,
27+ route = f"job/{ self .job_id } " ,
1928 requests_command = requests .get ,
2029 )
30+ self .job_last_known_status = response [STATUS_KEY ]
31+ return response
2132
2233 def errors (self ) -> List [str ]:
2334 return self .client .make_request (
2435 payload = {},
25- route = f"job/{ self .id } /errors" ,
36+ route = f"job/{ self .job_id } /errors" ,
2637 requests_command = requests .get ,
2738 )
2839
@@ -42,6 +53,16 @@ def sleep_until_complete(self, verbose_std_out=True):
4253 if final_status ["status" ] == "Errored" :
4354 raise JobError (final_status , self )
4455
56+ @classmethod
57+ def from_json (cls , payload : dict , client ):
58+ return cls (
59+ job_id = payload [JOB_ID_KEY ],
60+ job_last_known_status = payload [JOB_LAST_KNOWN_STATUS_KEY ],
61+ job_type = payload [JOB_TYPE_KEY ],
62+ job_creation_time = payload [JOB_CREATION_TIME_KEY ],
63+ client = client ,
64+ )
65+
4566
4667class JobError (Exception ):
4768 def __init__ (self , job_status : Dict [str , str ], job : AsyncJob ):
0 commit comments