Skip to content

Commit 7f42799

Browse files
committed
First pass unravel
Signed-off-by: Joe Elliott <number101010@gmail.com>
1 parent 99d85f4 commit 7f42799

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

api/client.go

+4-29
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,7 @@ func (cfg *Config) roundTripper() http.RoundTripper {
5757
// Client is the interface for an API client.
5858
type Client interface {
5959
URL(ep string, args map[string]string) *url.URL
60-
Do(context.Context, *http.Request) (*http.Response, []byte, Warnings, error)
61-
}
62-
63-
// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request.
64-
func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error) {
65-
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
66-
if err != nil {
67-
return nil, nil, nil, err
68-
}
69-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
70-
71-
resp, body, warnings, err := c.Do(ctx, req)
72-
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
73-
u.RawQuery = args.Encode()
74-
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
75-
if err != nil {
76-
return nil, nil, warnings, err
77-
}
78-
79-
} else {
80-
if err != nil {
81-
return resp, body, warnings, err
82-
}
83-
return resp, body, warnings, nil
84-
}
85-
return c.Do(ctx, req)
60+
Do(context.Context, *http.Request) (*http.Response, []byte, error)
8661
}
8762

8863
// NewClient returns a new Client.
@@ -120,7 +95,7 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
12095
return &u
12196
}
12297

123-
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
98+
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
12499
if ctx != nil {
125100
req = req.WithContext(ctx)
126101
}
@@ -132,7 +107,7 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
132107
}()
133108

134109
if err != nil {
135-
return nil, nil, nil, err
110+
return nil, nil, err
136111
}
137112

138113
var body []byte
@@ -152,5 +127,5 @@ func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response,
152127
case <-done:
153128
}
154129

155-
return resp, body, nil, err
130+
return resp, body, err
156131
}

api/prometheus/v1/api.go

+45-24
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"fmt"
2222
"math"
2323
"net/http"
24+
"net/url"
2425
"strconv"
26+
"strings"
2527
"time"
2628
"unsafe"
2729

@@ -515,7 +517,7 @@ func (qr *queryResult) UnmarshalJSON(b []byte) error {
515517
//
516518
// It is safe to use the returned API from multiple goroutines.
517519
func NewAPI(c api.Client) API {
518-
return &httpAPI{client: apiClient{c}}
520+
return &httpAPI{client: c}
519521
}
520522

521523
type httpAPI struct {
@@ -530,7 +532,7 @@ func (h *httpAPI) Alerts(ctx context.Context) (AlertsResult, error) {
530532
return AlertsResult{}, err
531533
}
532534

533-
_, body, _, err := h.client.Do(ctx, req)
535+
_, body, _, err := h.Do(ctx, req)
534536
if err != nil {
535537
return AlertsResult{}, err
536538
}
@@ -547,7 +549,7 @@ func (h *httpAPI) AlertManagers(ctx context.Context) (AlertManagersResult, error
547549
return AlertManagersResult{}, err
548550
}
549551

550-
_, body, _, err := h.client.Do(ctx, req)
552+
_, body, _, err := h.Do(ctx, req)
551553
if err != nil {
552554
return AlertManagersResult{}, err
553555
}
@@ -564,7 +566,7 @@ func (h *httpAPI) CleanTombstones(ctx context.Context) error {
564566
return err
565567
}
566568

567-
_, _, _, err = h.client.Do(ctx, req)
569+
_, _, _, err = h.Do(ctx, req)
568570
return err
569571
}
570572

@@ -576,7 +578,7 @@ func (h *httpAPI) Config(ctx context.Context) (ConfigResult, error) {
576578
return ConfigResult{}, err
577579
}
578580

579-
_, body, _, err := h.client.Do(ctx, req)
581+
_, body, _, err := h.Do(ctx, req)
580582
if err != nil {
581583
return ConfigResult{}, err
582584
}
@@ -603,7 +605,7 @@ func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime
603605
return err
604606
}
605607

606-
_, _, _, err = h.client.Do(ctx, req)
608+
_, _, _, err = h.Do(ctx, req)
607609
return err
608610
}
609611

@@ -615,7 +617,7 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
615617
return FlagsResult{}, err
616618
}
617619

618-
_, body, _, err := h.client.Do(ctx, req)
620+
_, body, _, err := h.Do(ctx, req)
619621
if err != nil {
620622
return FlagsResult{}, err
621623
}
@@ -630,7 +632,7 @@ func (h *httpAPI) LabelNames(ctx context.Context) ([]string, api.Warnings, error
630632
if err != nil {
631633
return nil, nil, err
632634
}
633-
_, body, w, err := h.client.Do(ctx, req)
635+
_, body, w, err := h.Do(ctx, req)
634636
if err != nil {
635637
return nil, w, err
636638
}
@@ -644,7 +646,7 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelVal
644646
if err != nil {
645647
return nil, nil, err
646648
}
647-
_, body, w, err := h.client.Do(ctx, req)
649+
_, body, w, err := h.Do(ctx, req)
648650
if err != nil {
649651
return nil, w, err
650652
}
@@ -661,7 +663,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.
661663
q.Set("time", formatTime(ts))
662664
}
663665

664-
_, body, warnings, err := api.DoGetFallback(h.client, ctx, u, q)
666+
_, body, warnings, err := h.DoGetFallback(ctx, u, q)
665667
if err != nil {
666668
return nil, warnings, err
667669
}
@@ -679,7 +681,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
679681
q.Set("end", formatTime(r.End))
680682
q.Set("step", strconv.FormatFloat(r.Step.Seconds(), 'f', -1, 64))
681683

682-
_, body, warnings, err := api.DoGetFallback(h.client, ctx, u, q)
684+
_, body, warnings, err := h.DoGetFallback(ctx, u, q)
683685
if err != nil {
684686
return nil, warnings, err
685687
}
@@ -707,7 +709,7 @@ func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.T
707709
return nil, nil, err
708710
}
709711

710-
_, body, warnings, err := h.client.Do(ctx, req)
712+
_, body, warnings, err := h.Do(ctx, req)
711713
if err != nil {
712714
return nil, warnings, err
713715
}
@@ -729,7 +731,7 @@ func (h *httpAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult,
729731
return SnapshotResult{}, err
730732
}
731733

732-
_, body, _, err := h.client.Do(ctx, req)
734+
_, body, _, err := h.Do(ctx, req)
733735
if err != nil {
734736
return SnapshotResult{}, err
735737
}
@@ -746,7 +748,7 @@ func (h *httpAPI) Rules(ctx context.Context) (RulesResult, error) {
746748
return RulesResult{}, err
747749
}
748750

749-
_, body, _, err := h.client.Do(ctx, req)
751+
_, body, _, err := h.Do(ctx, req)
750752
if err != nil {
751753
return RulesResult{}, err
752754
}
@@ -763,7 +765,7 @@ func (h *httpAPI) Targets(ctx context.Context) (TargetsResult, error) {
763765
return TargetsResult{}, err
764766
}
765767

766-
_, body, _, err := h.client.Do(ctx, req)
768+
_, body, _, err := h.Do(ctx, req)
767769
if err != nil {
768770
return TargetsResult{}, err
769771
}
@@ -787,7 +789,7 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
787789
return nil, err
788790
}
789791

790-
_, body, _, err := h.client.Do(ctx, req)
792+
_, body, _, err := h.Do(ctx, req)
791793
if err != nil {
792794
return nil, err
793795
}
@@ -796,12 +798,6 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
796798
return res, json.Unmarshal(body, &res)
797799
}
798800

799-
// apiClient wraps a regular client and processes successful API responses.
800-
// Successful also includes responses that errored at the API level.
801-
type apiClient struct {
802-
api.Client
803-
}
804-
805801
type apiResponse struct {
806802
Status string `json:"status"`
807803
Data json.RawMessage `json:"data"`
@@ -825,8 +821,8 @@ func errorTypeAndMsgFor(resp *http.Response) (ErrorType, string) {
825821
return ErrBadResponse, fmt.Sprintf("bad response code %d", resp.StatusCode)
826822
}
827823

828-
func (c apiClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, api.Warnings, error) {
829-
resp, body, warnings, err := c.Client.Do(ctx, req)
824+
func (h *httpAPI) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, api.Warnings, error) {
825+
resp, body, warnings, err := h.Do(ctx, req)
830826
if err != nil {
831827
return resp, body, warnings, err
832828
}
@@ -871,6 +867,31 @@ func (c apiClient) Do(ctx context.Context, req *http.Request) (*http.Response, [
871867

872868
}
873869

870+
// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request.
871+
func (h *httpAPI) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, api.Warnings, error) {
872+
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
873+
if err != nil {
874+
return nil, nil, nil, err
875+
}
876+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
877+
878+
resp, body, warnings, err := h.Do(ctx, req)
879+
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
880+
u.RawQuery = args.Encode()
881+
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
882+
if err != nil {
883+
return nil, nil, warnings, err
884+
}
885+
886+
} else {
887+
if err != nil {
888+
return resp, body, warnings, err
889+
}
890+
return resp, body, warnings, nil
891+
}
892+
return h.Do(ctx, req)
893+
}
894+
874895
func formatTime(t time.Time) string {
875896
return strconv.FormatFloat(float64(t.Unix())+float64(t.Nanosecond())/1e9, 'f', -1, 64)
876897
}

0 commit comments

Comments
 (0)