@@ -517,11 +517,15 @@ func (qr *queryResult) UnmarshalJSON(b []byte) error {
517
517
//
518
518
// It is safe to use the returned API from multiple goroutines.
519
519
func NewAPI (c api.Client ) API {
520
- return & httpAPI {client : c }
520
+ return & httpAPI {
521
+ client : & apiClientImpl {
522
+ client : c ,
523
+ },
524
+ }
521
525
}
522
526
523
527
type httpAPI struct {
524
- client api. Client
528
+ client apiClient
525
529
}
526
530
527
531
func (h * httpAPI ) Alerts (ctx context.Context ) (AlertsResult , error ) {
@@ -532,7 +536,7 @@ func (h *httpAPI) Alerts(ctx context.Context) (AlertsResult, error) {
532
536
return AlertsResult {}, err
533
537
}
534
538
535
- _ , body , _ , err := h .Do (ctx , req )
539
+ _ , body , _ , err := h .client . Do (ctx , req )
536
540
if err != nil {
537
541
return AlertsResult {}, err
538
542
}
@@ -549,7 +553,7 @@ func (h *httpAPI) AlertManagers(ctx context.Context) (AlertManagersResult, error
549
553
return AlertManagersResult {}, err
550
554
}
551
555
552
- _ , body , _ , err := h .Do (ctx , req )
556
+ _ , body , _ , err := h .client . Do (ctx , req )
553
557
if err != nil {
554
558
return AlertManagersResult {}, err
555
559
}
@@ -566,7 +570,7 @@ func (h *httpAPI) CleanTombstones(ctx context.Context) error {
566
570
return err
567
571
}
568
572
569
- _ , _ , _ , err = h .Do (ctx , req )
573
+ _ , _ , _ , err = h .client . Do (ctx , req )
570
574
return err
571
575
}
572
576
@@ -578,7 +582,7 @@ func (h *httpAPI) Config(ctx context.Context) (ConfigResult, error) {
578
582
return ConfigResult {}, err
579
583
}
580
584
581
- _ , body , _ , err := h .Do (ctx , req )
585
+ _ , body , _ , err := h .client . Do (ctx , req )
582
586
if err != nil {
583
587
return ConfigResult {}, err
584
588
}
@@ -605,7 +609,7 @@ func (h *httpAPI) DeleteSeries(ctx context.Context, matches []string, startTime
605
609
return err
606
610
}
607
611
608
- _ , _ , _ , err = h .Do (ctx , req )
612
+ _ , _ , _ , err = h .client . Do (ctx , req )
609
613
return err
610
614
}
611
615
@@ -617,7 +621,7 @@ func (h *httpAPI) Flags(ctx context.Context) (FlagsResult, error) {
617
621
return FlagsResult {}, err
618
622
}
619
623
620
- _ , body , _ , err := h .Do (ctx , req )
624
+ _ , body , _ , err := h .client . Do (ctx , req )
621
625
if err != nil {
622
626
return FlagsResult {}, err
623
627
}
@@ -632,7 +636,7 @@ func (h *httpAPI) LabelNames(ctx context.Context) ([]string, api.Warnings, error
632
636
if err != nil {
633
637
return nil , nil , err
634
638
}
635
- _ , body , w , err := h .Do (ctx , req )
639
+ _ , body , w , err := h .client . Do (ctx , req )
636
640
if err != nil {
637
641
return nil , w , err
638
642
}
@@ -646,7 +650,7 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string) (model.LabelVal
646
650
if err != nil {
647
651
return nil , nil , err
648
652
}
649
- _ , body , w , err := h .Do (ctx , req )
653
+ _ , body , w , err := h .client . Do (ctx , req )
650
654
if err != nil {
651
655
return nil , w , err
652
656
}
@@ -663,7 +667,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.
663
667
q .Set ("time" , formatTime (ts ))
664
668
}
665
669
666
- _ , body , warnings , err := h .DoGetFallback (ctx , u , q )
670
+ _ , body , warnings , err := h .client . DoGetFallback (ctx , u , q )
667
671
if err != nil {
668
672
return nil , warnings , err
669
673
}
@@ -681,7 +685,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
681
685
q .Set ("end" , formatTime (r .End ))
682
686
q .Set ("step" , strconv .FormatFloat (r .Step .Seconds (), 'f' , - 1 , 64 ))
683
687
684
- _ , body , warnings , err := h .DoGetFallback (ctx , u , q )
688
+ _ , body , warnings , err := h .client . DoGetFallback (ctx , u , q )
685
689
if err != nil {
686
690
return nil , warnings , err
687
691
}
@@ -709,7 +713,7 @@ func (h *httpAPI) Series(ctx context.Context, matches []string, startTime time.T
709
713
return nil , nil , err
710
714
}
711
715
712
- _ , body , warnings , err := h .Do (ctx , req )
716
+ _ , body , warnings , err := h .client . Do (ctx , req )
713
717
if err != nil {
714
718
return nil , warnings , err
715
719
}
@@ -731,7 +735,7 @@ func (h *httpAPI) Snapshot(ctx context.Context, skipHead bool) (SnapshotResult,
731
735
return SnapshotResult {}, err
732
736
}
733
737
734
- _ , body , _ , err := h .Do (ctx , req )
738
+ _ , body , _ , err := h .client . Do (ctx , req )
735
739
if err != nil {
736
740
return SnapshotResult {}, err
737
741
}
@@ -748,7 +752,7 @@ func (h *httpAPI) Rules(ctx context.Context) (RulesResult, error) {
748
752
return RulesResult {}, err
749
753
}
750
754
751
- _ , body , _ , err := h .Do (ctx , req )
755
+ _ , body , _ , err := h .client . Do (ctx , req )
752
756
if err != nil {
753
757
return RulesResult {}, err
754
758
}
@@ -765,7 +769,7 @@ func (h *httpAPI) Targets(ctx context.Context) (TargetsResult, error) {
765
769
return TargetsResult {}, err
766
770
}
767
771
768
- _ , body , _ , err := h .Do (ctx , req )
772
+ _ , body , _ , err := h .client . Do (ctx , req )
769
773
if err != nil {
770
774
return TargetsResult {}, err
771
775
}
@@ -789,7 +793,7 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
789
793
return nil , err
790
794
}
791
795
792
- _ , body , _ , err := h .Do (ctx , req )
796
+ _ , body , _ , err := h .client . Do (ctx , req )
793
797
if err != nil {
794
798
return nil , err
795
799
}
@@ -798,6 +802,18 @@ func (h *httpAPI) TargetsMetadata(ctx context.Context, matchTarget string, metri
798
802
return res , json .Unmarshal (body , & res )
799
803
}
800
804
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
+
801
817
type apiResponse struct {
802
818
Status string `json:"status"`
803
819
Data json.RawMessage `json:"data"`
@@ -821,17 +837,21 @@ func errorTypeAndMsgFor(resp *http.Response) (ErrorType, string) {
821
837
return ErrBadResponse , fmt .Sprintf ("bad response code %d" , resp .StatusCode )
822
838
}
823
839
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 )
826
846
if err != nil {
827
- return resp , body , warnings , err
847
+ return resp , body , nil , err
828
848
}
829
849
830
850
code := resp .StatusCode
831
851
832
852
if code / 100 != 2 && ! apiError (code ) {
833
853
errorType , errorMsg := errorTypeAndMsgFor (resp )
834
- return resp , body , warnings , & Error {
854
+ return resp , body , nil , & Error {
835
855
Type : errorType ,
836
856
Msg : errorMsg ,
837
857
Detail : string (body ),
@@ -842,7 +862,7 @@ func (h *httpAPI) Do(ctx context.Context, req *http.Request) (*http.Response, []
842
862
843
863
if http .StatusNoContent != code {
844
864
if jsonErr := json .Unmarshal (body , & result ); jsonErr != nil {
845
- return resp , body , warnings , & Error {
865
+ return resp , body , nil , & Error {
846
866
Type : ErrBadResponse ,
847
867
Msg : jsonErr .Error (),
848
868
}
@@ -863,12 +883,12 @@ func (h *httpAPI) Do(ctx context.Context, req *http.Request) (*http.Response, []
863
883
}
864
884
}
865
885
866
- return resp , []byte (result .Data ), warnings , err
886
+ return resp , []byte (result .Data ), result . Warnings , err
867
887
868
888
}
869
889
870
890
// 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 ) {
872
892
req , err := http .NewRequest (http .MethodPost , u .String (), strings .NewReader (args .Encode ()))
873
893
if err != nil {
874
894
return nil , nil , nil , err
0 commit comments