예제 #1
0
 def get_job_result():
     with self.call_api("get", ("tasks", namespace, task_id)) as response:
         reply = response.json()
         if reply.get("status") != "ok":
             raise encapsia_api.EncapsiaApiError(response.text)
         rest_api_result = reply["result"]
         task_status = rest_api_result["status"]
         if task_status == "finished":
             reply = self.get(("jobs", namespace, job_id))
             joblog = reply["result"]["logs"][0]
             assert joblog["status"] == "success"
             result = joblog["output"]
             if download:
                 filename = pathlib.Path(download)
                 with filename.open("wt") as f:
                     json.dump(result, f, indent=4)
                 return FileDownloadResponse(filename, "application/json")
             else:
                 return result
         elif task_status == "failed":
             raise encapsia_api.EncapsiaApiFailedTaskError(
                 "Failed Task behind the Job. See Exception payload attribute.",
                 payload=rest_api_result,
             )
         else:
             return NoResultYet
예제 #2
0
 def get_result():
     reply = self.get(("dbctl", "action", name, action_id))
     rest_api_result = reply["result"]
     action_status = rest_api_result["status"]
     action_result = rest_api_result["result"]
     if action_status == "finished":
         return action_result
     elif action_status == "failed":
         raise encapsia_api.EncapsiaApiFailedTaskError(
             "Failed dbctl task. See Exception payload attribute.",
             payload=rest_api_result,
         )
     else:
         return NoResultYet
예제 #3
0
 def get_task_result():
     with self.call_api(
         "get", ("tasks", namespace, task_id), stream=True
     ) as response:
         if response.headers.get("Content-type") == "application/json":
             reply = response.json()
             if reply.get("status") != "ok":
                 raise encapsia_api.EncapsiaApiError(response.text)
             rest_api_result = reply["result"]
             task_status = rest_api_result["status"]
             task_result = rest_api_result["result"]
             if task_status == "finished":
                 if download:
                     filename = pathlib.Path(download)
                     with filename.open("wt") as f:
                         json.dump(task_result, f, indent=4)
                     return FileDownloadResponse(filename, "application/json")
                 else:
                     return task_result
             elif task_status == "failed":
                 raise encapsia_api.EncapsiaApiFailedTaskError(
                     "Failed Task. See Exception payload attribute.",
                     payload=rest_api_result,
                 )
             else:
                 return NoResultYet
         else:
             # Stream the response directly to the given file.
             # Note we don't care whether this is JSON, CSV, or some other type.
             if download:
                 filename = pathlib.Path(download)
                 stream_response_to_file(response, filename)
                 return FileDownloadResponse(
                     filename, response.headers.get("Content-type")
                 )
             else:
                 return response.text