Skip to content

Commit 393adc9

Browse files
committed
Refactor ~worked. All tests passing except one
Signed-off-by: Joe Elliott <[email protected]>
1 parent 7f42799 commit 393adc9

File tree

3 files changed

+170
-103
lines changed

3 files changed

+170
-103
lines changed

api/client_test.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
package api
1515

1616
import (
17-
"context"
18-
"encoding/json"
1917
"net/http"
20-
"net/http/httptest"
2118
"net/url"
2219
"testing"
2320
)
@@ -114,72 +111,3 @@ func TestClientURL(t *testing.T) {
114111
}
115112
}
116113
}
117-
118-
func TestDoGetFallback(t *testing.T) {
119-
v := url.Values{"a": []string{"1", "2"}}
120-
121-
type testResponse struct {
122-
Values string
123-
Method string
124-
}
125-
126-
// Start a local HTTP server.
127-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
128-
req.ParseForm()
129-
r := &testResponse{
130-
Values: req.Form.Encode(),
131-
Method: req.Method,
132-
}
133-
134-
body, _ := json.Marshal(r)
135-
136-
if req.Method == http.MethodPost {
137-
if req.URL.Path == "/blockPost" {
138-
http.Error(w, string(body), http.StatusMethodNotAllowed)
139-
return
140-
}
141-
}
142-
143-
w.Write(body)
144-
}))
145-
// Close the server when test finishes.
146-
defer server.Close()
147-
148-
u, err := url.Parse(server.URL)
149-
if err != nil {
150-
t.Fatal(err)
151-
}
152-
client := &httpClient{client: *(server.Client())}
153-
154-
// Do a post, and ensure that the post succeeds.
155-
_, b, _, err := DoGetFallback(client, context.TODO(), u, v)
156-
if err != nil {
157-
t.Fatalf("Error doing local request: %v", err)
158-
}
159-
resp := &testResponse{}
160-
if err := json.Unmarshal(b, resp); err != nil {
161-
t.Fatal(err)
162-
}
163-
if resp.Method != http.MethodPost {
164-
t.Fatalf("Mismatch method")
165-
}
166-
if resp.Values != v.Encode() {
167-
t.Fatalf("Mismatch in values")
168-
}
169-
170-
// Do a fallbcak to a get.
171-
u.Path = "/blockPost"
172-
_, b, _, err = DoGetFallback(client, context.TODO(), u, v)
173-
if err != nil {
174-
t.Fatalf("Error doing local request: %v", err)
175-
}
176-
if err := json.Unmarshal(b, resp); err != nil {
177-
t.Fatal(err)
178-
}
179-
if resp.Method != http.MethodGet {
180-
t.Fatalf("Mismatch method")
181-
}
182-
if resp.Values != v.Encode() {
183-
t.Fatalf("Mismatch in values")
184-
}
185-
}

api/prometheus/v1/api.go

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,15 @@ func (qr *queryResult) UnmarshalJSON(b []byte) error {
517517
//
518518
// It is safe to use the returned API from multiple goroutines.
519519
func NewAPI(c api.Client) API {
520-
return &httpAPI{client: c}
520+
return &httpAPI{
521+
client: &apiClientImpl{
522+
client: c,
523+
},
524+
}
521525
}
522526

523527
type httpAPI struct {
524-
client api.Client
528+
client apiClient
525529
}
526530

527531
func (h *httpAPI) Alerts(ctx context.Context) (AlertsResult, error) {
@@ -532,7 +536,7 @@ func (h *httpAPI) Alerts(ctx context.Context) (AlertsResult, error) {
532536
return AlertsResult{}, err
533537
}
534538

535-
_, body, _, err := h.Do(ctx, req)
539+
_, body, _, err := h.client.Do(ctx, req)
536540
if err != nil {
537541
return AlertsResult{}, err
538542
}
@@ -549,7 +553,7 @@ func (h *httpAPI) AlertManagers(ctx context.Context) (AlertManagersResult, error
549553
return AlertManagersResult{}, err
550554
}
551555

552-
_, body, _, err := h.Do(ctx, req)
556+
_, body, _, err := h.client.Do(ctx, req)
553557
if err != nil {
554558
return AlertManagersResult{}, err
555559
}
@@ -566,7 +570,7 @@ func (h *httpAPI) CleanTombstones(ctx context.Context) error {
566570
return err
567571
}
568572

569-
_, _, _, err = h.Do(ctx, req)
573+
_, _, _, err = h.client.Do(ctx, req)
570574
return err
571575
}
572576

@@ -578,7 +582,7 @@ func (h *httpAPI) Config(ctx context.Context) (ConfigResult, error) {
578582
return ConfigResult{}, err
579583
}
580584

581-
_, body, _, err := h.Do(ctx, req)
585+
_, body, _, err := h.client.Do(ctx, req)
582586
if err != nil {
583587
return ConfigResult{}, err
584588
}
@@ -605,7 +609,7 @@ func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime
605609
return err
606610
}
607611

608-
_, _, _, err = h.Do(ctx, req)
612+
_, _, _, err = h.client.Do(ctx, req)
609613
return err
610614
}
611615

@@ -617,7 +621,7 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
617621
return FlagsResult{}, err
618622
}
619623

620-
_, body, _, err := h.Do(ctx, req)
624+
_, body, _, err := h.client.Do(ctx, req)
621625
if err != nil {
622626
return FlagsResult{}, err
623627
}
@@ -632,7 +636,7 @@ func (h *httpAPI) LabelNames(ctx context.Context) ([]string, api.Warnings, error
632636
if err != nil {
633637
return nil, nil, err
634638
}
635-
_, body, w, err := h.Do(ctx, req)
639+
_, body, w, err := h.client.Do(ctx, req)
636640
if err != nil {
637641
return nil, w, err
638642
}
@@ -646,7 +650,7 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelVal
646650
if err != nil {
647651
return nil, nil, err
648652
}
649-
_, body, w, err := h.Do(ctx, req)
653+
_, body, w, err := h.client.Do(ctx, req)
650654
if err != nil {
651655
return nil, w, err
652656
}
@@ -663,7 +667,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.
663667
q.Set("time", formatTime(ts))
664668
}
665669

666-
_, body, warnings, err := h.DoGetFallback(ctx, u, q)
670+
_, body, warnings, err := h.client.DoGetFallback(ctx, u, q)
667671
if err != nil {
668672
return nil, warnings, err
669673
}
@@ -681,7 +685,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
681685
q.Set("end", formatTime(r.End))
682686
q.Set("step", strconv.FormatFloat(r.Step.Seconds(), 'f', -1, 64))
683687

684-
_, body, warnings, err := h.DoGetFallback(ctx, u, q)
688+
_, body, warnings, err := h.client.DoGetFallback(ctx, u, q)
685689
if err != nil {
686690
return nil, warnings, err
687691
}
@@ -709,7 +713,7 @@ func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.T
709713
return nil, nil, err
710714
}
711715

712-
_, body, warnings, err := h.Do(ctx, req)
716+
_, body, warnings, err := h.client.Do(ctx, req)
713717
if err != nil {
714718
return nil, warnings, err
715719
}
@@ -731,7 +735,7 @@ func (h *httpAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult,
731735
return SnapshotResult{}, err
732736
}
733737

734-
_, body, _, err := h.Do(ctx, req)
738+
_, body, _, err := h.client.Do(ctx, req)
735739
if err != nil {
736740
return SnapshotResult{}, err
737741
}
@@ -748,7 +752,7 @@ func (h *httpAPI) Rules(ctx context.Context) (RulesResult, error) {
748752
return RulesResult{}, err
749753
}
750754

751-
_, body, _, err := h.Do(ctx, req)
755+
_, body, _, err := h.client.Do(ctx, req)
752756
if err != nil {
753757
return RulesResult{}, err
754758
}
@@ -765,7 +769,7 @@ func (h *httpAPI) Targets(ctx context.Context) (TargetsResult, error) {
765769
return TargetsResult{}, err
766770
}
767771

768-
_, body, _, err := h.Do(ctx, req)
772+
_, body, _, err := h.client.Do(ctx, req)
769773
if err != nil {
770774
return TargetsResult{}, err
771775
}
@@ -789,7 +793,7 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
789793
return nil, err
790794
}
791795

792-
_, body, _, err := h.Do(ctx, req)
796+
_, body, _, err := h.client.Do(ctx, req)
793797
if err != nil {
794798
return nil, err
795799
}
@@ -798,6 +802,18 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
798802
return res, json.Unmarshal(body, &res)
799803
}
800804

805+
// apiClient wraps a regular client and processes successful API responses.
806+
// Successful also includes responses that errored at the API level.
807+
type apiClient interface {
808+
URL(ep string, args map[string]string) *url.URL
809+
Do(context.Context, *http.Request) (*http.Response, []byte, api.Warnings, error)
810+
DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, api.Warnings, error)
811+
}
812+
813+
type apiClientImpl struct {
814+
client api.Client
815+
}
816+
801817
type apiResponse struct {
802818
Status string `json:"status"`
803819
Data json.RawMessage `json:"data"`
@@ -821,17 +837,21 @@ func errorTypeAndMsgFor(resp *http.Response) (ErrorType, string) {
821837
return ErrBadResponse, fmt.Sprintf("bad response code %d", resp.StatusCode)
822838
}
823839

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)
840+
func (h *apiClientImpl) URL(ep string, args map[string]string) *url.URL {
841+
return h.client.URL(ep, args)
842+
}
843+
844+
func (h *apiClientImpl) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, api.Warnings, error) {
845+
resp, body, err := h.client.Do(ctx, req)
826846
if err != nil {
827-
return resp, body, warnings, err
847+
return resp, body, nil, err
828848
}
829849

830850
code := resp.StatusCode
831851

832852
if code/100 != 2 && !apiError(code) {
833853
errorType, errorMsg := errorTypeAndMsgFor(resp)
834-
return resp, body, warnings, &Error{
854+
return resp, body, nil, &Error{
835855
Type: errorType,
836856
Msg: errorMsg,
837857
Detail: string(body),
@@ -842,7 +862,7 @@ func (h *httpAPI) Do(ctx context.Context, req *http.Request) (*http.Response, []
842862

843863
if http.StatusNoContent != code {
844864
if jsonErr := json.Unmarshal(body, &result); jsonErr != nil {
845-
return resp, body, warnings, &Error{
865+
return resp, body, nil, &Error{
846866
Type: ErrBadResponse,
847867
Msg: jsonErr.Error(),
848868
}
@@ -863,12 +883,12 @@ func (h *httpAPI) Do(ctx context.Context, req *http.Request) (*http.Response, []
863883
}
864884
}
865885

866-
return resp, []byte(result.Data), warnings, err
886+
return resp, []byte(result.Data), result.Warnings, err
867887

868888
}
869889

870890
// 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) {
891+
func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, api.Warnings, error) {
872892
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
873893
if err != nil {
874894
return nil, nil, nil, err

0 commit comments

Comments
 (0)