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