API Response implements http.ResponseWriter (#32046)

* rename response.Header -> response.SetHeader to free up method name for http.ResponseWriter ifc

* normalresponse implements http.ResponseWriter
This commit is contained in:
Owen Diehl 2021-03-17 13:12:28 -04:00 committed by GitHub
parent 36614b03f7
commit 7a4ab13a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 11 deletions

View File

@ -561,10 +561,10 @@ func CalculateDashboardDiff(c *models.ReqContext, apiOptions dtos.CalculateDiffO
}
if options.DiffType == dashdiffs.DiffDelta {
return response.Respond(200, result.Delta).Header("Content-Type", "application/json")
return response.Respond(200, result.Delta).SetHeader("Content-Type", "application/json")
}
return response.Respond(200, result.Delta).Header("Content-Type", "text/html")
return response.Respond(200, result.Delta).SetHeader("Content-Type", "text/html")
}
// RestoreDashboardVersion restores a dashboard to the given version.

View File

@ -175,7 +175,7 @@ func GetDashboardSnapshot(c *models.ReqContext) response.Response {
metrics.MApiDashboardSnapshotGet.Inc()
return response.JSON(200, dto).Header("Cache-Control", "public, max-age=3600")
return response.JSON(200, dto).SetHeader("Cache-Control", "public, max-age=3600")
}
func deleteExternalDashboardSnapshot(externalUrl string) error {

View File

@ -243,7 +243,7 @@ func (hs *HTTPServer) GetPluginMarkdown(c *models.ReqContext) response.Response
}
resp := response.Respond(200, content)
resp.Header("Content-Type", "text/plain; charset=utf-8")
resp.SetHeader("Content-Type", "text/plain; charset=utf-8")
return resp
}

View File

@ -1,6 +1,7 @@
package response
import (
"bytes"
"encoding/json"
"net/http"
@ -22,17 +23,33 @@ type Response interface {
func CreateNormalResponse(header http.Header, body []byte, status int) *NormalResponse {
return &NormalResponse{
header: header,
body: body,
body: bytes.NewBuffer(body),
status: status,
}
}
type NormalResponse struct {
status int
body []byte
body *bytes.Buffer
header http.Header
errMessage string
err error
http.ResponseWriter
}
// Write implements http.ResponseWriter
func (r *NormalResponse) Write(b []byte) (int, error) {
return r.body.Write(b)
}
// Header implements http.ResponseWriter
func (r *NormalResponse) Header() http.Header {
return r.header
}
// WriteHeader implements http.ResponseWriter
func (r *NormalResponse) WriteHeader(statusCode int) {
r.status = statusCode
}
// Status gets the response's status.
@ -42,7 +59,7 @@ func (r *NormalResponse) Status() int {
// Body gets the response's body.
func (r *NormalResponse) Body() []byte {
return r.body
return r.body.Bytes()
}
// Err gets the response's err.
@ -65,12 +82,12 @@ func (r *NormalResponse) WriteTo(ctx *models.ReqContext) {
header[k] = v
}
ctx.Resp.WriteHeader(r.status)
if _, err := ctx.Resp.Write(r.body); err != nil {
if _, err := ctx.Resp.Write(r.body.Bytes()); err != nil {
ctx.Logger.Error("Error writing to response", "err", err)
}
}
func (r *NormalResponse) Header(key, value string) *NormalResponse {
func (r *NormalResponse) SetHeader(key, value string) *NormalResponse {
r.header.Set(key, value)
return r
}
@ -137,7 +154,7 @@ func (r *RedirectResponse) Body() []byte {
// JSON creates a JSON response.
func JSON(status int, body interface{}) *NormalResponse {
return Respond(status, body).Header("Content-Type", "application/json")
return Respond(status, body).SetHeader("Content-Type", "application/json")
}
// JSONStreaming creates a streaming JSON response.
@ -211,7 +228,7 @@ func Respond(status int, body interface{}) *NormalResponse {
return &NormalResponse{
status: status,
body: b,
body: bytes.NewBuffer(b),
header: make(http.Header),
}
}