First pass unravel
Signed-off-by: Joe Elliott <number101010@gmail.com>
This commit is contained in:
parent
99d85f4901
commit
7f4279992e
|
@ -57,32 +57,7 @@ func (cfg *Config) roundTripper() http.RoundTripper {
|
||||||
// Client is the interface for an API client.
|
// Client is the interface for an API client.
|
||||||
type Client interface {
|
type Client interface {
|
||||||
URL(ep string, args map[string]string) *url.URL
|
URL(ep string, args map[string]string) *url.URL
|
||||||
Do(context.Context, *http.Request) (*http.Response, []byte, Warnings, error)
|
Do(context.Context, *http.Request) (*http.Response, []byte, error)
|
||||||
}
|
|
||||||
|
|
||||||
// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request.
|
|
||||||
func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error) {
|
|
||||||
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, nil, err
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
|
|
||||||
resp, body, warnings, err := c.Do(ctx, req)
|
|
||||||
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
|
|
||||||
u.RawQuery = args.Encode()
|
|
||||||
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, warnings, err
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
return resp, body, warnings, err
|
|
||||||
}
|
|
||||||
return resp, body, warnings, nil
|
|
||||||
}
|
|
||||||
return c.Do(ctx, req)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a new Client.
|
// NewClient returns a new Client.
|
||||||
|
@ -120,7 +95,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
|
||||||
return &u
|
return &u
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
|
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
|
||||||
if ctx != nil {
|
if ctx != nil {
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
}
|
}
|
||||||
|
@ -132,7 +107,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var body []byte
|
var body []byte
|
||||||
|
@ -152,5 +127,5 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
|
||||||
case <-done:
|
case <-done:
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, body, nil, err
|
return resp, body, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
@ -515,7 +517,7 @@ func (qr *queryResult) UnmarshalJSON(b []byte) error {
|
||||||
//
|
//
|
||||||
// It is safe to use the returned API from multiple goroutines.
|
// It is safe to use the returned API from multiple goroutines.
|
||||||
func NewAPI(c api.Client) API {
|
func NewAPI(c api.Client) API {
|
||||||
return &httpAPI{client: apiClient{c}}
|
return &httpAPI{client: c}
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpAPI struct {
|
type httpAPI struct {
|
||||||
|
@ -530,7 +532,7 @@ func (h *httpAPI) Alerts(ctx context.Context) (AlertsResult, error) {
|
||||||
return AlertsResult{}, err
|
return AlertsResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return AlertsResult{}, err
|
return AlertsResult{}, err
|
||||||
}
|
}
|
||||||
|
@ -547,7 +549,7 @@ func (h *httpAPI) AlertManagers(ctx context.Context) (AlertManagersResult, error
|
||||||
return AlertManagersResult{}, err
|
return AlertManagersResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return AlertManagersResult{}, err
|
return AlertManagersResult{}, err
|
||||||
}
|
}
|
||||||
|
@ -564,7 +566,7 @@ func (h *httpAPI) CleanTombstones(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, _, err = h.client.Do(ctx, req)
|
_, _, _, err = h.Do(ctx, req)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -576,7 +578,7 @@ func (h *httpAPI) Config(ctx context.Context) (ConfigResult, error) {
|
||||||
return ConfigResult{}, err
|
return ConfigResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigResult{}, err
|
return ConfigResult{}, err
|
||||||
}
|
}
|
||||||
|
@ -603,7 +605,7 @@ func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, _, err = h.client.Do(ctx, req)
|
_, _, _, err = h.Do(ctx, req)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -615,7 +617,7 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
|
||||||
return FlagsResult{}, err
|
return FlagsResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return FlagsResult{}, err
|
return FlagsResult{}, err
|
||||||
}
|
}
|
||||||
|
@ -630,7 +632,7 @@ func (h *httpAPI) LabelNames(ctx context.Context) ([]string, api.Warnings, error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
_, body, w, err := h.client.Do(ctx, req)
|
_, body, w, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, w, err
|
return nil, w, err
|
||||||
}
|
}
|
||||||
|
@ -644,7 +646,7 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelVal
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
_, body, w, err := h.client.Do(ctx, req)
|
_, body, w, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, w, err
|
return nil, w, err
|
||||||
}
|
}
|
||||||
|
@ -661,7 +663,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.
|
||||||
q.Set("time", formatTime(ts))
|
q.Set("time", formatTime(ts))
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, warnings, err := api.DoGetFallback(h.client, ctx, u, q)
|
_, body, warnings, err := h.DoGetFallback(ctx, u, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
@ -679,7 +681,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
|
||||||
q.Set("end", formatTime(r.End))
|
q.Set("end", formatTime(r.End))
|
||||||
q.Set("step", strconv.FormatFloat(r.Step.Seconds(), 'f', -1, 64))
|
q.Set("step", strconv.FormatFloat(r.Step.Seconds(), 'f', -1, 64))
|
||||||
|
|
||||||
_, body, warnings, err := api.DoGetFallback(h.client, ctx, u, q)
|
_, body, warnings, err := h.DoGetFallback(ctx, u, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
@ -707,7 +709,7 @@ func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.T
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, warnings, err := h.client.Do(ctx, req)
|
_, body, warnings, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, warnings, err
|
return nil, warnings, err
|
||||||
}
|
}
|
||||||
|
@ -729,7 +731,7 @@ func (h *httpAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult,
|
||||||
return SnapshotResult{}, err
|
return SnapshotResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return SnapshotResult{}, err
|
return SnapshotResult{}, err
|
||||||
}
|
}
|
||||||
|
@ -746,7 +748,7 @@ func (h *httpAPI) Rules(ctx context.Context) (RulesResult, error) {
|
||||||
return RulesResult{}, err
|
return RulesResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return RulesResult{}, err
|
return RulesResult{}, err
|
||||||
}
|
}
|
||||||
|
@ -763,7 +765,7 @@ func (h *httpAPI) Targets(ctx context.Context) (TargetsResult, error) {
|
||||||
return TargetsResult{}, err
|
return TargetsResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return TargetsResult{}, err
|
return TargetsResult{}, err
|
||||||
}
|
}
|
||||||
|
@ -787,7 +789,7 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, body, _, err := h.client.Do(ctx, req)
|
_, body, _, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -796,12 +798,6 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
|
||||||
return res, json.Unmarshal(body, &res)
|
return res, json.Unmarshal(body, &res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// apiClient wraps a regular client and processes successful API responses.
|
|
||||||
// Successful also includes responses that errored at the API level.
|
|
||||||
type apiClient struct {
|
|
||||||
api.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
type apiResponse struct {
|
type apiResponse struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
Data json.RawMessage `json:"data"`
|
Data json.RawMessage `json:"data"`
|
||||||
|
@ -825,8 +821,8 @@ func errorTypeAndMsgFor(resp *http.Response) (ErrorType, string) {
|
||||||
return ErrBadResponse, fmt.Sprintf("bad response code %d", resp.StatusCode)
|
return ErrBadResponse, fmt.Sprintf("bad response code %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c apiClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, api.Warnings, error) {
|
func (h *httpAPI) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, api.Warnings, error) {
|
||||||
resp, body, warnings, err := c.Client.Do(ctx, req)
|
resp, body, warnings, err := h.Do(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return resp, body, warnings, err
|
return resp, body, warnings, err
|
||||||
}
|
}
|
||||||
|
@ -871,6 +867,31 @@ func (c apiClient) Do(ctx context.Context, req *http.Request) (*http.Response, [
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request.
|
||||||
|
func (h *httpAPI) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, api.Warnings, error) {
|
||||||
|
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
|
resp, body, warnings, err := h.Do(ctx, req)
|
||||||
|
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
|
||||||
|
u.RawQuery = args.Encode()
|
||||||
|
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, warnings, err
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
return resp, body, warnings, err
|
||||||
|
}
|
||||||
|
return resp, body, warnings, nil
|
||||||
|
}
|
||||||
|
return h.Do(ctx, req)
|
||||||
|
}
|
||||||
|
|
||||||
func formatTime(t time.Time) string {
|
func formatTime(t time.Time) string {
|
||||||
return strconv.FormatFloat(float64(t.Unix())+float64(t.Nanosecond())/1e9, 'f', -1, 64)
|
return strconv.FormatFloat(float64(t.Unix())+float64(t.Nanosecond())/1e9, 'f', -1, 64)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue