GetWorkload is a handler for the async-gateway service workload retrieval route
(w http.ResponseWriter, r *http.Request)
| 87 | |
| 88 | // GetWorkload is a handler for the async-gateway service workload retrieval route |
| 89 | func (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 | |
| 124 | func respondPlainText(w http.ResponseWriter, statusCode int, message string) { |
| 125 | w.Header().Set("Content-Type", "text/plain") |
nothing calls this directly
no test coverage detected