Query string is needed to get the position information for the annotations, and it can be empty if the position information isn't needed.
(w http.ResponseWriter, req *http.Request, data any, warnings annotations.Annotations, query string)
| 2150 | // Query string is needed to get the position information for the annotations, and it |
| 2151 | // can be empty if the position information isn't needed. |
| 2152 | func (api *API) respond(w http.ResponseWriter, req *http.Request, data any, warnings annotations.Annotations, query string) { |
| 2153 | statusMessage := statusSuccess |
| 2154 | warn, info := warnings.AsStrings(query, 10, 10) |
| 2155 | |
| 2156 | resp := &Response{ |
| 2157 | Status: statusMessage, |
| 2158 | Data: data, |
| 2159 | Warnings: warn, |
| 2160 | Infos: info, |
| 2161 | } |
| 2162 | |
| 2163 | codec, err := api.negotiateCodec(req, resp) |
| 2164 | if err != nil { |
| 2165 | api.respondError(w, &apiError{errorNotAcceptable, err}, nil) |
| 2166 | return |
| 2167 | } |
| 2168 | |
| 2169 | b, err := codec.Encode(resp) |
| 2170 | if err != nil { |
| 2171 | api.logger.Error("error marshaling response", "url", req.URL, "err", err) |
| 2172 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 2173 | return |
| 2174 | } |
| 2175 | |
| 2176 | w.Header().Set("Content-Type", codec.ContentType().String()) |
| 2177 | w.WriteHeader(http.StatusOK) |
| 2178 | if n, err := w.Write(b); err != nil { |
| 2179 | api.logger.Error("error writing response", "url", req.URL, "bytesWritten", n, "err", err) |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | func (api *API) negotiateCodec(req *http.Request, resp *Response) (Codec, error) { |
| 2184 | for _, clause := range goautoneg.ParseAccept(req.Header.Get("Accept")) { |