mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 10:17:05 +08:00
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:
@ -561,10 +561,10 @@ func CalculateDashboardDiff(c *models.ReqContext, apiOptions dtos.CalculateDiffO
|
|||||||
}
|
}
|
||||||
|
|
||||||
if options.DiffType == dashdiffs.DiffDelta {
|
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.
|
// RestoreDashboardVersion restores a dashboard to the given version.
|
||||||
|
@ -175,7 +175,7 @@ func GetDashboardSnapshot(c *models.ReqContext) response.Response {
|
|||||||
|
|
||||||
metrics.MApiDashboardSnapshotGet.Inc()
|
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 {
|
func deleteExternalDashboardSnapshot(externalUrl string) error {
|
||||||
|
@ -243,7 +243,7 @@ func (hs *HTTPServer) GetPluginMarkdown(c *models.ReqContext) response.Response
|
|||||||
}
|
}
|
||||||
|
|
||||||
resp := response.Respond(200, content)
|
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
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package response
|
package response
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -22,17 +23,33 @@ type Response interface {
|
|||||||
func CreateNormalResponse(header http.Header, body []byte, status int) *NormalResponse {
|
func CreateNormalResponse(header http.Header, body []byte, status int) *NormalResponse {
|
||||||
return &NormalResponse{
|
return &NormalResponse{
|
||||||
header: header,
|
header: header,
|
||||||
body: body,
|
body: bytes.NewBuffer(body),
|
||||||
status: status,
|
status: status,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type NormalResponse struct {
|
type NormalResponse struct {
|
||||||
status int
|
status int
|
||||||
body []byte
|
body *bytes.Buffer
|
||||||
header http.Header
|
header http.Header
|
||||||
errMessage string
|
errMessage string
|
||||||
err error
|
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.
|
// Status gets the response's status.
|
||||||
@ -42,7 +59,7 @@ func (r *NormalResponse) Status() int {
|
|||||||
|
|
||||||
// Body gets the response's body.
|
// Body gets the response's body.
|
||||||
func (r *NormalResponse) Body() []byte {
|
func (r *NormalResponse) Body() []byte {
|
||||||
return r.body
|
return r.body.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Err gets the response's err.
|
// Err gets the response's err.
|
||||||
@ -65,12 +82,12 @@ func (r *NormalResponse) WriteTo(ctx *models.ReqContext) {
|
|||||||
header[k] = v
|
header[k] = v
|
||||||
}
|
}
|
||||||
ctx.Resp.WriteHeader(r.status)
|
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)
|
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)
|
r.header.Set(key, value)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
@ -137,7 +154,7 @@ func (r *RedirectResponse) Body() []byte {
|
|||||||
|
|
||||||
// JSON creates a JSON response.
|
// JSON creates a JSON response.
|
||||||
func JSON(status int, body interface{}) *NormalResponse {
|
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.
|
// JSONStreaming creates a streaming JSON response.
|
||||||
@ -211,7 +228,7 @@ func Respond(status int, body interface{}) *NormalResponse {
|
|||||||
|
|
||||||
return &NormalResponse{
|
return &NormalResponse{
|
||||||
status: status,
|
status: status,
|
||||||
body: b,
|
body: bytes.NewBuffer(b),
|
||||||
header: make(http.Header),
|
header: make(http.Header),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user