MCPcopy
hub / github.com/cortexlabs/cortex / GetWorkload

Method GetWorkload

pkg/async-gateway/endpoint.go:89–122  ·  view source on GitHub ↗

GetWorkload is a handler for the async-gateway service workload retrieval route

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

87
88// GetWorkload is a handler for the async-gateway service workload retrieval route
89func (e *Endpoint) GetWorkload(w http.ResponseWriter, r *http.Request) {
90 vars := mux.Vars(r)
91 id, ok := vars["id"]
92 if !ok {
93 respondPlainText(w, http.StatusBadRequest, "error: missing request id in url path")
94 return
95 }
96
97 apiName := r.Header.Get(consts.CortexAPINameHeader)
98 if apiName == "" {
99 respondPlainText(w, http.StatusBadRequest, fmt.Sprintf("error: missing %s key in request header", consts.CortexAPINameHeader))
100 return
101 }
102 r.Header.Del(consts.CortexAPINameHeader)
103
104 log := e.logger.With(zap.String("id", id), zap.String("apiName", apiName))
105
106 res, err := e.service.GetWorkload(id, apiName)
107 if err != nil {
108 respondPlainText(w, http.StatusInternalServerError, fmt.Sprintf("error: %v", err))
109 logErrorWithTelemetry(log, errors.Wrap(err, "failed to get workload"))
110 return
111 }
112 if res.Status == async.StatusNotFound {
113 respondPlainText(w, http.StatusNotFound, fmt.Sprintf("error: id %s not found", res.ID))
114 logErrorWithTelemetry(log, errors.ErrorUnexpected(fmt.Sprintf("error: id %s not found", res.ID)))
115 return
116 }
117
118 if err = respondJSON(w, http.StatusOK, res); err != nil {
119 logErrorWithTelemetry(log, errors.Wrap(err, "failed to encode json response"))
120 return
121 }
122}
123
124func respondPlainText(w http.ResponseWriter, statusCode int, message string) {
125 w.Header().Set("Content-Type", "text/plain")

Callers

nothing calls this directly

Calls 7

WrapFunction · 0.92
ErrorUnexpectedFunction · 0.92
respondPlainTextFunction · 0.85
logErrorWithTelemetryFunction · 0.85
respondJSONFunction · 0.70
GetWorkloadMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected