github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/requester/publicapi/endpoints_states.go (about)

     1  package publicapi
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/http"
     7  
     8  	"github.com/filecoin-project/bacalhau/pkg/model"
     9  	"github.com/filecoin-project/bacalhau/pkg/publicapi/handlerwrapper"
    10  	"github.com/filecoin-project/bacalhau/pkg/system"
    11  )
    12  
    13  type stateRequest struct {
    14  	ClientID string `json:"client_id" example:"ac13188e93c97a9c2e7cf8e86c7313156a73436036f30da1ececc2ce79f9ea51"`
    15  	JobID    string `json:"job_id" example:"9304c616-291f-41ad-b862-54e133c0149e"`
    16  }
    17  
    18  type stateResponse struct {
    19  	State model.JobState `json:"state"`
    20  }
    21  
    22  // states godoc
    23  //
    24  //	@ID						pkg/requester/publicapi/states
    25  //	@Summary				Returns the state of the job-id specified in the body payload.
    26  //	@Description.markdown	endpoints_states
    27  //	@Tags					Job
    28  //	@Accept					json
    29  //	@Produce				json
    30  //	@Param					stateRequest	body		stateRequest	true	" "
    31  //	@Success				200				{object}	stateResponse
    32  //	@Failure				400				{object}	string
    33  //	@Failure				500				{object}	string
    34  //	@Router					/requester/states [post]
    35  func (s *RequesterAPIServer) states(res http.ResponseWriter, req *http.Request) {
    36  	ctx := req.Context()
    37  	var stateReq stateRequest
    38  	if err := json.NewDecoder(req.Body).Decode(&stateReq); err != nil {
    39  		http.Error(res, err.Error(), http.StatusBadRequest)
    40  		return
    41  	}
    42  	res.Header().Set(handlerwrapper.HTTPHeaderClientID, stateReq.ClientID)
    43  	res.Header().Set(handlerwrapper.HTTPHeaderJobID, stateReq.JobID)
    44  	ctx = system.AddJobIDToBaggage(ctx, stateReq.JobID)
    45  
    46  	js, err := getJobStateFromRequest(ctx, s, stateReq)
    47  	if err != nil {
    48  		http.Error(res, err.Error(), http.StatusInternalServerError)
    49  		return
    50  	}
    51  
    52  	res.WriteHeader(http.StatusOK)
    53  	err = json.NewEncoder(res).Encode(stateResponse{
    54  		State: js,
    55  	})
    56  	if err != nil {
    57  		http.Error(res, err.Error(), http.StatusInternalServerError)
    58  		return
    59  	}
    60  }
    61  
    62  func getJobStateFromRequest(ctx context.Context, apiServer *RequesterAPIServer, stateReq stateRequest) (model.JobState, error) {
    63  	return apiServer.jobStore.GetJobState(ctx, stateReq.JobID)
    64  }