(w http.ResponseWriter, req *http.Request, data any, warnings annotations.Annotations, query string)
| 251 | } |
| 252 | |
| 253 | func (q *QueryAPI) respond(w http.ResponseWriter, req *http.Request, data any, warnings annotations.Annotations, query string) { |
| 254 | warn, info := warnings.AsStrings(query, 10, 10) |
| 255 | |
| 256 | resp := &v1.Response{ |
| 257 | Status: statusSuccess, |
| 258 | Data: data, |
| 259 | Warnings: warn, |
| 260 | Infos: info, |
| 261 | } |
| 262 | |
| 263 | codec, err := q.negotiateCodec(req, resp) |
| 264 | if err != nil { |
| 265 | api.RespondFromGRPCError(q.logger, w, httpgrpc.Errorf(http.StatusNotAcceptable, "%s", &apiError{errorNotAcceptable, err})) |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | b, err := codec.Encode(resp) |
| 270 | if err != nil { |
| 271 | level.Error(q.logger).Log("error marshaling response", "url", req.URL, "err", err) |
| 272 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 273 | return |
| 274 | } |
| 275 | |
| 276 | w.Header().Set("Content-Type", codec.ContentType().String()) |
| 277 | w.Header().Set("X-Uncompressed-Length", strconv.Itoa(len(b))) |
| 278 | w.WriteHeader(http.StatusOK) |
| 279 | if n, err := w.Write(b); err != nil { |
| 280 | level.Error(q.logger).Log("error writing response", "url", req.URL, "bytesWritten", n, "err", err) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func (q *QueryAPI) negotiateCodec(req *http.Request, resp *v1.Response) (v1.Codec, error) { |
| 285 | for _, clause := range goautoneg.ParseAccept(req.Header.Get("Accept")) { |
no test coverage detected