11import logging
22from time import sleep
3- from typing import Literal
3+ from typing import Literal , overload
44
55from nuclei .client import NucleiClient
66from requests import Response
@@ -69,8 +69,90 @@ def wait_until_ticket_is_ready(
6969 raise RuntimeError (failure_message )
7070
7171
72+ @overload
73+ def get_task_result_pipeline (
74+ client : NucleiClient ,
75+ endpoint : str ,
76+ payload : dict ,
77+ verbose : bool ,
78+ response_type : Literal ["dict" ],
79+ save_failed_payload : bool ,
80+ failed_payload_filename : str ,
81+ ) -> dict : ...
82+
83+
84+ @overload
85+ def get_task_result_pipeline (
86+ client : NucleiClient ,
87+ endpoint : str ,
88+ payload : dict ,
89+ verbose : bool ,
90+ response_type : Literal ["bytes" ],
91+ save_failed_payload : bool ,
92+ failed_payload_filename : str ,
93+ ) -> bytes : ...
94+
95+
96+ def get_task_result_pipeline (
97+ client : NucleiClient ,
98+ endpoint : str ,
99+ payload : dict ,
100+ verbose : bool = False ,
101+ response_type : Literal ["dict" , "bytes" ] = "dict" ,
102+ save_failed_payload : bool = False ,
103+ failed_payload_filename : str = "debug_payload.json" ,
104+ ) -> dict | bytes :
105+ """
106+ Execute a task and return the result.
107+
108+ Parameters
109+ ----------
110+ client: NucleiClient
111+ client object created by [nuclei](https://github.com/cemsbv/nuclei)
112+ endpoint: str
113+ the endpoint to call
114+ payload: dict
115+ the payload of the request
116+ verbose: bool
117+ if True, print additional information to the console
118+ save_failed_payload: bool
119+ if True, save the payload to a file in case of failure
120+ failed_payload_filename: str
121+ the name of the file to save the failed payload to
122+ """
123+
124+ try :
125+ ticket = client .call_endpoint (
126+ "PileCore" ,
127+ endpoint ,
128+ version = "v4" ,
129+ schema = payload ,
130+ return_response = True ,
131+ )
132+
133+ wait_until_ticket_is_ready (client = client , ticket = ticket , verbose = verbose )
134+
135+ response = client .call_endpoint (
136+ "PileCore" , "/get-task-result" , version = "v4" , schema = ticket .json ()
137+ )
138+ except Exception as e :
139+ if save_failed_payload :
140+ # In case of failure, save the payload to a file for debugging
141+ with open (failed_payload_filename , "w" ) as f :
142+ from nuclei .client .utils import serialize_json_string
143+
144+ f .write (serialize_json_string (payload ))
145+ raise e
146+
147+ return response
148+
149+
72150def get_multi_cpt_api_result (
73- client : NucleiClient , payload : dict , verbose : bool = False
151+ client : NucleiClient ,
152+ payload : dict ,
153+ verbose : bool = False ,
154+ save_failed_payload : bool = False ,
155+ failed_payload_filename : str = "pilecore_multi_cpt_result_debug_payload.json" ,
74156) -> dict :
75157 """
76158 Wrapper around the PileCore endpoint "/bearing/multiple-cpts/results".
@@ -88,24 +170,24 @@ def get_multi_cpt_api_result(
88170 "Calculating bearing capacities... \n "
89171 "Depending on the amount of pile tip levels and CPT's this can take a while."
90172 )
91- ticket = client .call_endpoint (
92- "PileCore" ,
93- "/bearing/multiple-cpts/results" ,
94- version = "v4" ,
95- schema = payload ,
96- return_response = True ,
97- )
98-
99- wait_until_ticket_is_ready (client = client , ticket = ticket , verbose = verbose )
100-
101- return client .call_endpoint (
102- "PileCore" , "/get-task-result" , version = "v4" , schema = ticket .json ()
173+ return get_task_result_pipeline (
174+ client = client ,
175+ endpoint = "/bearing/multiple-cpts/results" ,
176+ payload = payload ,
177+ verbose = verbose ,
178+ response_type = "dict" ,
179+ save_failed_payload = save_failed_payload ,
180+ failed_payload_filename = failed_payload_filename ,
103181 )
104182
105183
106184def get_multi_cpt_api_report (
107- client : NucleiClient , payload : dict , verbose : bool = False
108- ) -> dict :
185+ client : NucleiClient ,
186+ payload : dict ,
187+ verbose : bool = False ,
188+ save_failed_payload : bool = False ,
189+ failed_payload_filename : str = "pilecore_multi_cpt_report_debug_payload.json" ,
190+ ) -> bytes :
109191 """
110192 Wrapper around the PileCore endpoint "/bearing/multiple-cpts/report".
111193
@@ -122,22 +204,23 @@ def get_multi_cpt_api_report(
122204 "Generate report... \n "
123205 "Depending on the amount of pile tip levels and CPT's this can take a while."
124206 )
125- ticket = client .call_endpoint (
126- "PileCore" ,
127- "/bearing/multiple-cpts/report" ,
128- version = "v4" ,
129- schema = payload ,
130- return_response = True ,
131- )
132- wait_until_ticket_is_ready (client = client , ticket = ticket , verbose = verbose )
133-
134- return client .call_endpoint (
135- "PileCore" , "/get-task-result" , version = "v4" , schema = ticket .json ()
207+ return get_task_result_pipeline (
208+ client = client ,
209+ endpoint = "/bearing/multiple-cpts/report" ,
210+ payload = payload ,
211+ verbose = verbose ,
212+ response_type = "bytes" ,
213+ save_failed_payload = save_failed_payload ,
214+ failed_payload_filename = failed_payload_filename ,
136215 )
137216
138217
139218def get_groups_api_result (
140- client : NucleiClient , payload : dict , verbose : bool = False
219+ client : NucleiClient ,
220+ payload : dict ,
221+ verbose : bool = False ,
222+ save_failed_payload : bool = False ,
223+ failed_payload_filename : str = "pilecore_group_cpts_debug_payload.json" ,
141224) -> dict :
142225 """
143226 Wrapper around the PileCore endpoint "/grouper/group_cpts".
@@ -155,23 +238,23 @@ def get_groups_api_result(
155238 "Finding groups... \n "
156239 "Depending on the amount of pile tip levels and CPT's this can take a while."
157240 )
158- ticket = client .call_endpoint (
159- "PileCore" ,
160- "/grouper/group-cpts" ,
161- version = "v4" ,
162- schema = payload ,
163- return_response = True ,
164- )
165-
166- wait_until_ticket_is_ready (client = client , ticket = ticket , verbose = verbose )
167-
168- return client .call_endpoint (
169- "PileCore" , "/get-task-result" , version = "v4" , schema = ticket .json ()
241+ return get_task_result_pipeline (
242+ client = client ,
243+ endpoint = "/grouper/group-cpts" ,
244+ payload = payload ,
245+ verbose = verbose ,
246+ response_type = "dict" ,
247+ save_failed_payload = save_failed_payload ,
248+ failed_payload_filename = failed_payload_filename ,
170249 )
171250
172251
173252def get_groups_api_report (
174- client : NucleiClient , payload : dict , verbose : bool = False
253+ client : NucleiClient ,
254+ payload : dict ,
255+ verbose : bool = False ,
256+ save_failed_payload : bool = False ,
257+ failed_payload_filename : str = "pilecore_grouper_report_debug_payload.json" ,
175258) -> bytes :
176259 """
177260 Wrapper around the PileCore endpoint "/grouper/generate_grouper_report".
@@ -189,18 +272,14 @@ def get_groups_api_report(
189272 "Generate report... \n "
190273 "Depending on the amount of pile tip levels and CPT's this can take a while."
191274 )
192- ticket = client .call_endpoint (
193- "PileCore" ,
194- "/grouper/generate-grouper-report" ,
195- version = "v4" ,
196- schema = payload ,
197- return_response = True ,
198- )
199-
200- wait_until_ticket_is_ready (client = client , ticket = ticket , verbose = verbose )
201-
202- return client .call_endpoint (
203- "PileCore" , "/get-task-result" , version = "v4" , schema = ticket .json ()
275+ return get_task_result_pipeline (
276+ client = client ,
277+ endpoint = "/grouper/generate-grouper-report" ,
278+ payload = payload ,
279+ verbose = verbose ,
280+ response_type = "bytes" ,
281+ save_failed_payload = save_failed_payload ,
282+ failed_payload_filename = failed_payload_filename ,
204283 )
205284
206285
@@ -209,6 +288,8 @@ def get_multi_cpt_api_result_tension(
209288 payload : dict ,
210289 verbose : bool = False ,
211290 standard : Literal ["NEN9997-1" , "CUR236" ] = "NEN9997-1" ,
291+ save_failed_payload : bool = False ,
292+ failed_payload_filename : str = "pilecore_multi_cpt_tension_result_debug_payload.json" ,
212293) -> dict :
213294 """
214295 Wrapper around the PileCore endpoint "/uplift/[nen or cur]/multiple-cpts/results".
@@ -234,18 +315,14 @@ def get_multi_cpt_api_result_tension(
234315 endpoint = "/uplift/cur/multiple-cpts/results"
235316 payload .pop ("construction_sequence" , None )
236317
237- ticket = client . call_endpoint (
238- "PileCore" ,
318+ return get_task_result_pipeline (
319+ client = client ,
239320 endpoint = endpoint ,
240- version = "v4" ,
241- schema = payload ,
242- return_response = True ,
243- )
244-
245- wait_until_ticket_is_ready (client = client , ticket = ticket , verbose = verbose )
246-
247- return client .call_endpoint (
248- "PileCore" , "/get-task-result" , version = "v4" , schema = ticket .json ()
321+ payload = payload ,
322+ verbose = verbose ,
323+ response_type = "dict" ,
324+ save_failed_payload = save_failed_payload ,
325+ failed_payload_filename = failed_payload_filename ,
249326 )
250327
251328
@@ -254,6 +331,8 @@ def get_multi_cpt_api_report_tension(
254331 payload : dict ,
255332 verbose : bool = False ,
256333 standard : Literal ["NEN9997-1" , "CUR236" ] = "NEN9997-1" ,
334+ save_failed_payload : bool = False ,
335+ failed_payload_filename : str = "pilecore_multi_cpt_tension_report_debug_payload.json" ,
257336) -> bytes :
258337 """
259338 Wrapper around the PileCore endpoint "/uplift/[nen or cur]/multiple-cpts/report".
@@ -280,15 +359,12 @@ def get_multi_cpt_api_report_tension(
280359 endpoint = "/uplift/cur/multiple-cpts/report"
281360 payload .pop ("construction_sequence" , None )
282361
283- ticket = client . call_endpoint (
284- "PileCore" ,
362+ return get_task_result_pipeline (
363+ client = client ,
285364 endpoint = endpoint ,
286- version = "v4" ,
287- schema = payload ,
288- return_response = True ,
289- )
290- wait_until_ticket_is_ready (client = client , ticket = ticket , verbose = verbose )
291-
292- return client .call_endpoint (
293- "PileCore" , "/get-task-result" , version = "v4" , schema = ticket .json ()
365+ payload = payload ,
366+ verbose = verbose ,
367+ response_type = "bytes" ,
368+ save_failed_payload = save_failed_payload ,
369+ failed_payload_filename = failed_payload_filename ,
294370 )
0 commit comments