@@ -41,11 +41,14 @@ var ErrUnsupportedVer = errors.New("API version of the client is not supported b
41
41
42
42
// NginxClient lets you access NGINX Plus API.
43
43
type NginxClient struct {
44
- version int
44
+ apiVersion int
45
45
apiEndpoint string
46
46
httpClient * http.Client
47
+ checkAPI bool
47
48
}
48
49
50
+ type Option func (* NginxClient )
51
+
49
52
type versions []int
50
53
51
54
// UpstreamServer lets you configure HTTP upstreams.
@@ -508,35 +511,66 @@ type WorkersHTTP struct {
508
511
HTTPRequests HTTPRequests `json:"requests"`
509
512
}
510
513
511
- // NewNginxClient creates an NginxClient with the latest supported version.
512
- func NewNginxClient (httpClient * http.Client , apiEndpoint string ) (* NginxClient , error ) {
513
- return NewNginxClientWithVersion (httpClient , apiEndpoint , APIVersion )
514
+ // WithHTTPClient sets the HTTP client to use for accessing the API.
515
+ func WithHTTPClient (httpClient * http.Client ) Option {
516
+ return func (o * NginxClient ) {
517
+ o .httpClient = httpClient
518
+ }
514
519
}
515
520
516
- // NewNginxClientWithVersion creates an NginxClient with the given version of NGINX Plus API.
517
- func NewNginxClientWithVersion ( httpClient * http. Client , apiEndpoint string , version int ) ( * NginxClient , error ) {
518
- if ! versionSupported ( version ) {
519
- return nil , fmt . Errorf ( "API version %v is not supported by the client" , version )
521
+ // WithAPIVersion sets the API version to use for accessing the API.
522
+ func WithAPIVersion ( apiVersion int ) Option {
523
+ return func ( o * NginxClient ) {
524
+ o . apiVersion = apiVersion
520
525
}
521
- versions , err := getAPIVersions (httpClient , apiEndpoint )
522
- if err != nil {
523
- return nil , fmt .Errorf ("error accessing the API: %w" , err )
526
+ }
527
+
528
+ // WithCheckAPI sets the flag to check the API version of the server.
529
+ func WithCheckAPI () Option {
530
+ return func (o * NginxClient ) {
531
+ o .checkAPI = true
524
532
}
525
- found := false
526
- for _ , v := range * versions {
527
- if v == version {
528
- found = true
529
- break
530
- }
533
+ }
534
+
535
+ // NewNginxClient creates a new NginxClient.
536
+ func NewNginxClient (apiEndpoint string , opts ... Option ) (* NginxClient , error ) {
537
+ c := & NginxClient {
538
+ httpClient : http .DefaultClient ,
539
+ apiEndpoint : apiEndpoint ,
540
+ apiVersion : APIVersion ,
541
+ checkAPI : false ,
531
542
}
532
- if ! found {
533
- return nil , ErrUnsupportedVer
543
+
544
+ for _ , opt := range opts {
545
+ opt (c )
534
546
}
535
- return & NginxClient {
536
- apiEndpoint : apiEndpoint ,
537
- httpClient : httpClient ,
538
- version : version ,
539
- }, nil
547
+
548
+ if c .httpClient == nil {
549
+ return nil , fmt .Errorf ("http client is not set" )
550
+ }
551
+
552
+ if ! versionSupported (c .apiVersion ) {
553
+ return nil , fmt .Errorf ("API version %v is not supported by the client" , c .apiVersion )
554
+ }
555
+
556
+ if c .checkAPI {
557
+ versions , err := getAPIVersions (c .httpClient , apiEndpoint )
558
+ if err != nil {
559
+ return nil , fmt .Errorf ("error accessing the API: %w" , err )
560
+ }
561
+ found := false
562
+ for _ , v := range * versions {
563
+ if v == c .apiVersion {
564
+ found = true
565
+ break
566
+ }
567
+ }
568
+ if ! found {
569
+ return nil , fmt .Errorf ("API version %v is not supported by the server" , c .apiVersion )
570
+ }
571
+ }
572
+
573
+ return c , nil
540
574
}
541
575
542
576
func versionSupported (n int ) bool {
@@ -807,7 +841,7 @@ func (client *NginxClient) get(path string, data interface{}) error {
807
841
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
808
842
defer cancel ()
809
843
810
- url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .version , path )
844
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .apiVersion , path )
811
845
812
846
req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
813
847
if err != nil {
@@ -841,7 +875,7 @@ func (client *NginxClient) post(path string, input interface{}) error {
841
875
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
842
876
defer cancel ()
843
877
844
- url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .version , path )
878
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .apiVersion , path )
845
879
846
880
jsonInput , err := json .Marshal (input )
847
881
if err != nil {
@@ -873,7 +907,7 @@ func (client *NginxClient) delete(path string, expectedStatusCode int) error {
873
907
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
874
908
defer cancel ()
875
909
876
- path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .version , path )
910
+ path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .apiVersion , path )
877
911
878
912
req , err := http .NewRequestWithContext (ctx , http .MethodDelete , path , nil )
879
913
if err != nil {
@@ -898,7 +932,7 @@ func (client *NginxClient) patch(path string, input interface{}, expectedStatusC
898
932
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
899
933
defer cancel ()
900
934
901
- path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .version , path )
935
+ path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .apiVersion , path )
902
936
903
937
jsonInput , err := json .Marshal (input )
904
938
if err != nil {
@@ -1359,7 +1393,7 @@ func (client *NginxClient) GetStreamZoneSync() (*StreamZoneSync, error) {
1359
1393
// GetLocationZones returns http/location_zones stats.
1360
1394
func (client * NginxClient ) GetLocationZones () (* LocationZones , error ) {
1361
1395
var locationZones LocationZones
1362
- if client .version < 5 {
1396
+ if client .apiVersion < 5 {
1363
1397
return & locationZones , nil
1364
1398
}
1365
1399
err := client .get ("http/location_zones" , & locationZones )
@@ -1373,7 +1407,7 @@ func (client *NginxClient) GetLocationZones() (*LocationZones, error) {
1373
1407
// GetResolvers returns Resolvers stats.
1374
1408
func (client * NginxClient ) GetResolvers () (* Resolvers , error ) {
1375
1409
var resolvers Resolvers
1376
- if client .version < 5 {
1410
+ if client .apiVersion < 5 {
1377
1411
return & resolvers , nil
1378
1412
}
1379
1413
err := client .get ("resolvers" , & resolvers )
@@ -1596,7 +1630,7 @@ func (client *NginxClient) UpdateStreamServer(upstream string, server StreamUpst
1596
1630
1597
1631
// Version returns client's current N+ API version.
1598
1632
func (client * NginxClient ) Version () int {
1599
- return client .version
1633
+ return client .apiVersion
1600
1634
}
1601
1635
1602
1636
func addPortToServer (server string ) string {
@@ -1618,7 +1652,7 @@ func addPortToServer(server string) string {
1618
1652
// GetHTTPLimitReqs returns http/limit_reqs stats.
1619
1653
func (client * NginxClient ) GetHTTPLimitReqs () (* HTTPLimitRequests , error ) {
1620
1654
var limitReqs HTTPLimitRequests
1621
- if client .version < 6 {
1655
+ if client .apiVersion < 6 {
1622
1656
return & limitReqs , nil
1623
1657
}
1624
1658
err := client .get ("http/limit_reqs" , & limitReqs )
@@ -1631,7 +1665,7 @@ func (client *NginxClient) GetHTTPLimitReqs() (*HTTPLimitRequests, error) {
1631
1665
// GetHTTPConnectionsLimit returns http/limit_conns stats.
1632
1666
func (client * NginxClient ) GetHTTPConnectionsLimit () (* HTTPLimitConnections , error ) {
1633
1667
var limitConns HTTPLimitConnections
1634
- if client .version < 6 {
1668
+ if client .apiVersion < 6 {
1635
1669
return & limitConns , nil
1636
1670
}
1637
1671
err := client .get ("http/limit_conns" , & limitConns )
@@ -1644,7 +1678,7 @@ func (client *NginxClient) GetHTTPConnectionsLimit() (*HTTPLimitConnections, err
1644
1678
// GetStreamConnectionsLimit returns stream/limit_conns stats.
1645
1679
func (client * NginxClient ) GetStreamConnectionsLimit () (* StreamLimitConnections , error ) {
1646
1680
var limitConns StreamLimitConnections
1647
- if client .version < 6 {
1681
+ if client .apiVersion < 6 {
1648
1682
return & limitConns , nil
1649
1683
}
1650
1684
err := client .get ("stream/limit_conns" , & limitConns )
@@ -1663,7 +1697,7 @@ func (client *NginxClient) GetStreamConnectionsLimit() (*StreamLimitConnections,
1663
1697
// GetWorkers returns workers stats.
1664
1698
func (client * NginxClient ) GetWorkers () ([]* Workers , error ) {
1665
1699
var workers []* Workers
1666
- if client .version < 9 {
1700
+ if client .apiVersion < 9 {
1667
1701
return workers , nil
1668
1702
}
1669
1703
err := client .get ("workers" , & workers )
0 commit comments