safer, more idiomatic proxy helper (#32732)

Co-authored-by: Sofia Papagiannaki <sofia@grafana.com>
This commit is contained in:
Owen Diehl
2021-04-07 15:36:50 -04:00
committed by GitHub
co-authored by Sofia Papagiannaki
parent 4527f712e0
commit 8b8fc293b7
5 changed files with 171 additions and 158 deletions
+13 -1
View File
@@ -4,7 +4,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
@@ -86,9 +88,19 @@ type AlertingProxy struct {
// withReq proxies a different request
func (p *AlertingProxy) withReq(
ctx *models.ReqContext,
req *http.Request,
method string,
u *url.URL,
body io.Reader,
extractor func([]byte) (interface{}, error),
headers map[string]string,
) response.Response {
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return response.Error(400, err.Error(), nil)
}
for h, v := range headers {
req.Header.Add(h, v)
}
newCtx, resp := replacedResponseWriter(ctx)
newCtx.Req.Request = req
p.DataProxy.ProxyDatasourceRequestWithID(newCtx, ctx.ParamsInt64("Recipient"))