github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/python/bacalhau_sdk/api.py (about)

     1  """Submit a job to the server."""
     2  
     3  import json
     4  
     5  from bacalhau_apiclient.api import job_api
     6  from bacalhau_apiclient.models.events_request import EventsRequest
     7  from bacalhau_apiclient.models.list_request import ListRequest
     8  from bacalhau_apiclient.models.state_request import StateRequest
     9  from bacalhau_apiclient.models.submit_request import SubmitRequest
    10  from bacalhau_apiclient.rest import ApiException
    11  
    12  from bacalhau_sdk.config import get_client_id, get_client_public_key, init_config, sign_for_client
    13  
    14  conf = init_config()
    15  client = job_api.ApiClient(conf)
    16  api_instance = job_api.JobApi(client)
    17  
    18  
    19  def submit(data: dict):
    20      """Submit a job to the server.
    21  
    22      Input `data` object is sanittized and signed before being sent to the server.
    23      """
    24      sanitized_data = client.sanitize_for_serialization(data)
    25      json_data = json.dumps(sanitized_data, indent=None, separators=(", ", ": "))
    26      json_bytes = json_data.encode("utf-8")
    27      signature = sign_for_client(json_bytes)
    28      client_public_key = get_client_public_key()
    29      submit_req = SubmitRequest(
    30          client_public_key=client_public_key,
    31          job_create_payload=sanitized_data,
    32          signature=signature,
    33      )
    34      return api_instance.submit(submit_req)
    35  
    36  
    37  def list():
    38      """List all jobs."""
    39      try:
    40          # Simply lists jobs.
    41          list_request = ListRequest(
    42              client_id=get_client_id(),
    43              sort_reverse=False,
    44              sort_by="created_at",
    45              return_all=False,
    46              max_jobs=5,
    47              exclude_tags=[],
    48              include_tags=[],
    49          )
    50          api_response = api_instance.list(list_request)
    51      except ApiException as e:
    52          print("Exception when calling JobApi->list: %s\n" % e)
    53      return api_response
    54  
    55  
    56  def results(job_id: str):
    57      """Get results."""
    58      try:
    59          # Returns the results of the job-id specified in the body payload.
    60          state_request = StateRequest(
    61              client_id=get_client_id(),
    62              job_id=job_id,
    63          )
    64          api_response = api_instance.results(state_request)
    65      except ApiException as e:
    66          print("Exception when calling JobApi->results: %s\n" % e)
    67      return api_response
    68  
    69  
    70  def states(job_id: str):
    71      """Get states."""
    72      try:
    73          # Returns the state of the job-id specified in the body payload.
    74          state_request = StateRequest(
    75              client_id=get_client_id(),
    76              job_id=job_id,
    77          )
    78          api_response = api_instance.states(state_request)
    79      except ApiException as e:
    80          print("Exception when calling JobApi->states: %s\n" % e)
    81      return api_response
    82  
    83  
    84  def events(job_id: str):
    85      """Get events."""
    86      # TODO - add tests
    87      try:
    88          # Returns the events of the job-id specified in the body payload.
    89          state_request = EventsRequest(
    90              client_id=get_client_id(),
    91              job_id=job_id,
    92          )
    93          api_response = api_instance.events(state_request)
    94      except ApiException as e:
    95          print("Exception when calling JobApi->events: %s\n" % e)
    96      return api_response