(w http.ResponseWriter, data any)
| 168 | } |
| 169 | |
| 170 | func (api *API) respond(w http.ResponseWriter, data any) { |
| 171 | w.Header().Set("Content-Type", "application/json") |
| 172 | w.WriteHeader(http.StatusOK) |
| 173 | |
| 174 | b, err := json.Marshal(&response{ |
| 175 | Status: statusSuccess, |
| 176 | Data: data, |
| 177 | }) |
| 178 | if err != nil { |
| 179 | api.logger.Error("error marshaling JSON", "err", err) |
| 180 | api.respondError(w, apiError{ |
| 181 | typ: errorBadData, |
| 182 | err: err, |
| 183 | }, "") |
| 184 | } |
| 185 | |
| 186 | if _, err := w.Write(b); err != nil { |
| 187 | api.logger.Error("failed to write data to connection", "err", err) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func (api *API) respondError(w http.ResponseWriter, apiErr apiError, data any) { |
| 192 | w.Header().Set("Content-Type", "application/json") |
no test coverage detected