MCPcopy Create free account
hub / github.com/prometheus/prometheus / negotiateCodec

Method negotiateCodec

web/api/v1/api.go:2183–2198  ·  view source on GitHub ↗
(req *http.Request, resp *Response)

Source from the content-addressed store, hash-verified

2181}
2182
2183func (api *API) negotiateCodec(req *http.Request, resp *Response) (Codec, error) {
2184 for _, clause := range goautoneg.ParseAccept(req.Header.Get("Accept")) {
2185 for _, codec := range api.codecs {
2186 if codec.ContentType().Satisfies(clause) && codec.CanEncode(resp) {
2187 return codec, nil
2188 }
2189 }
2190 }
2191
2192 defaultCodec := api.codecs[0]
2193 if !defaultCodec.CanEncode(resp) {
2194 return nil, fmt.Errorf("cannot encode response as %s", defaultCodec.ContentType())
2195 }
2196
2197 return defaultCodec, nil
2198}
2199
2200func (api *API) respondError(w http.ResponseWriter, apiErr *apiError, data any) {
2201 json := jsoniter.ConfigCompatibleWithStandardLibrary

Callers 1

respondMethod · 0.95

Calls 4

SatisfiesMethod · 0.80
GetMethod · 0.65
ContentTypeMethod · 0.65
CanEncodeMethod · 0.65

Tested by

no test coverage detected