1
- from typing import Generator
1
+ from typing import TYPE_CHECKING , cast
2
2
3
3
import requests
4
4
5
5
from indico .client .request import GraphQLRequest , RequestChain
6
6
from indico .errors import IndicoInputError , IndicoRequestError
7
- from indico .queries .jobs import JobStatus
8
7
from indico .types .jobs import Job
9
8
9
+ from .jobs import JobStatus
10
10
11
- class _UploadSMExport (GraphQLRequest ):
11
+ if TYPE_CHECKING : # pragma: no cover
12
+ from typing import Dict , Iterator , Optional , Union # noqa: F401
13
+
14
+ from indico .typing import Payload
15
+
16
+
17
+ class _UploadSMExport (GraphQLRequest [str ]):
12
18
query = """
13
19
query exportUpload {
14
20
exportUpload {
@@ -22,25 +28,26 @@ def __init__(self, file_path: str):
22
28
self .file_path = file_path
23
29
super ().__init__ (self .query )
24
30
25
- def process_response (self , response ) -> str :
26
- resp = super ().process_response (response )["exportUpload" ]
31
+ def process_response (self , response : "Payload" ) -> str :
32
+ resp : "Dict[str, str]" = super ().parse_payload (response )["exportUpload" ]
27
33
signed_url = resp ["signedUrl" ]
28
34
storage_uri = resp ["storageUri" ]
29
35
30
36
with open (self .file_path , "rb" ) as file :
31
37
file_content = file .read ()
32
38
33
39
headers = {"Content-Type" : "application/zip" }
34
- response = requests .put (signed_url , data = file_content , headers = headers )
40
+ export_response = requests .put (signed_url , data = file_content , headers = headers )
35
41
36
- if response .status_code != 200 :
42
+ if export_response .status_code != 200 :
37
43
raise IndicoRequestError (
38
- f"Failed to upload static model export: { response .text } "
44
+ f"Failed to upload static model export: { export_response .text } " ,
45
+ export_response .status_code ,
39
46
)
40
47
return storage_uri
41
48
42
49
43
- class ProcessStaticModelExport (GraphQLRequest ):
50
+ class ProcessStaticModelExport (GraphQLRequest [ "Job" ] ):
44
51
"""
45
52
Process a static model export.
46
53
@@ -77,12 +84,12 @@ def __init__(
77
84
},
78
85
)
79
86
80
- def process_response (self , response ) -> Job :
81
- job_id = super ().process_response (response )["processStaticModelExport" ]["jobId" ]
87
+ def process_response (self , response : "Payload" ) -> Job :
88
+ job_id = super ().parse_payload (response )["processStaticModelExport" ]["jobId" ]
82
89
return Job (id = job_id )
83
90
84
91
85
- class UploadStaticModelExport (RequestChain ):
92
+ class UploadStaticModelExport (RequestChain [ "Union[Job, str]" ] ):
86
93
"""
87
94
Upload a static model export to Indico.
88
95
@@ -100,22 +107,27 @@ class UploadStaticModelExport(RequestChain):
100
107
"""
101
108
102
109
def __init__ (
103
- self , file_path : str , auto_process : bool = False , workflow_id : int | None = None
110
+ self ,
111
+ file_path : str ,
112
+ auto_process : bool = False ,
113
+ workflow_id : "Optional[int]" = None ,
104
114
):
105
- self .file_path = file_path
106
- self .auto_process = auto_process
107
- if auto_process and not workflow_id :
115
+ if auto_process and workflow_id is None :
108
116
raise IndicoInputError (
109
117
"Must provide `workflow_id` if `auto_process` is True."
110
118
)
111
119
120
+ self .file_path = file_path
121
+ self .auto_process = auto_process
112
122
self .workflow_id = workflow_id
113
123
114
- def requests (self ) -> Generator [str | Job , None , None ]:
124
+ def requests (
125
+ self ,
126
+ ) -> "Iterator[Union[_UploadSMExport, ProcessStaticModelExport, JobStatus]]" :
115
127
if self .auto_process :
116
128
yield _UploadSMExport (self .file_path )
117
129
yield ProcessStaticModelExport (
118
- storage_uri = self .previous , workflow_id = self .workflow_id
130
+ storage_uri = self .previous , workflow_id = cast ( int , self .workflow_id )
119
131
)
120
132
yield JobStatus (self .previous .id )
121
133
if self .previous .status == "FAILURE" :
0 commit comments