@@ -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.
@@ -494,35 +497,66 @@ type HTTPLimitConnections map[string]LimitConnection
494
497
// StreamLimitConnections represents limit connections related stats
495
498
type StreamLimitConnections map [string ]LimitConnection
496
499
497
- // NewNginxClient creates an NginxClient with the latest supported version.
498
- func NewNginxClient (httpClient * http.Client , apiEndpoint string ) (* NginxClient , error ) {
499
- return NewNginxClientWithVersion (httpClient , apiEndpoint , APIVersion )
500
+ // WithHTTPClient sets the HTTP client to use for accessing the API.
501
+ func WithHTTPClient (httpClient * http.Client ) Option {
502
+ return func (o * NginxClient ) {
503
+ o .httpClient = httpClient
504
+ }
500
505
}
501
506
502
- // NewNginxClientWithVersion creates an NginxClient with the given version of NGINX Plus API.
503
- func NewNginxClientWithVersion ( httpClient * http. Client , apiEndpoint string , version int ) ( * NginxClient , error ) {
504
- if ! versionSupported ( version ) {
505
- return nil , fmt . Errorf ( "API version %v is not supported by the client" , version )
507
+ // WithAPIVersion sets the API version to use for accessing the API.
508
+ func WithAPIVersion ( apiVersion int ) Option {
509
+ return func ( o * NginxClient ) {
510
+ o . apiVersion = apiVersion
506
511
}
507
- versions , err := getAPIVersions (httpClient , apiEndpoint )
508
- if err != nil {
509
- return nil , fmt .Errorf ("error accessing the API: %w" , err )
512
+ }
513
+
514
+ // WithCheckAPI sets the flag to check the API version of the server.
515
+ func WithCheckAPI () Option {
516
+ return func (o * NginxClient ) {
517
+ o .checkAPI = true
510
518
}
511
- found := false
512
- for _ , v := range * versions {
513
- if v == version {
514
- found = true
515
- break
516
- }
519
+ }
520
+
521
+ // NewNginxClient creates a new NginxClient.
522
+ func NewNginxClient (apiEndpoint string , opts ... Option ) (* NginxClient , error ) {
523
+ c := & NginxClient {
524
+ httpClient : http .DefaultClient ,
525
+ apiEndpoint : apiEndpoint ,
526
+ apiVersion : APIVersion ,
527
+ checkAPI : false ,
517
528
}
518
- if ! found {
519
- return nil , ErrUnsupportedVer
529
+
530
+ for _ , opt := range opts {
531
+ opt (c )
520
532
}
521
- return & NginxClient {
522
- apiEndpoint : apiEndpoint ,
523
- httpClient : httpClient ,
524
- version : version ,
525
- }, nil
533
+
534
+ if c .httpClient == nil {
535
+ return nil , fmt .Errorf ("http client is not set" )
536
+ }
537
+
538
+ if ! versionSupported (c .apiVersion ) {
539
+ return nil , fmt .Errorf ("API version %v is not supported by the client" , c .apiVersion )
540
+ }
541
+
542
+ if c .checkAPI {
543
+ versions , err := getAPIVersions (c .httpClient , apiEndpoint )
544
+ if err != nil {
545
+ return nil , fmt .Errorf ("error accessing the API: %w" , err )
546
+ }
547
+ found := false
548
+ for _ , v := range * versions {
549
+ if v == c .apiVersion {
550
+ found = true
551
+ break
552
+ }
553
+ }
554
+ if ! found {
555
+ return nil , fmt .Errorf ("API version %v is not supported by the server" , c .apiVersion )
556
+ }
557
+ }
558
+
559
+ return c , nil
526
560
}
527
561
528
562
func versionSupported (n int ) bool {
@@ -793,7 +827,7 @@ func (client *NginxClient) get(path string, data interface{}) error {
793
827
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
794
828
defer cancel ()
795
829
796
- url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .version , path )
830
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .apiVersion , path )
797
831
798
832
req , err := http .NewRequestWithContext (ctx , http .MethodGet , url , nil )
799
833
if err != nil {
@@ -827,7 +861,7 @@ func (client *NginxClient) post(path string, input interface{}) error {
827
861
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
828
862
defer cancel ()
829
863
830
- url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .version , path )
864
+ url := fmt .Sprintf ("%v/%v/%v" , client .apiEndpoint , client .apiVersion , path )
831
865
832
866
jsonInput , err := json .Marshal (input )
833
867
if err != nil {
@@ -859,7 +893,7 @@ func (client *NginxClient) delete(path string, expectedStatusCode int) error {
859
893
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
860
894
defer cancel ()
861
895
862
- path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .version , path )
896
+ path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .apiVersion , path )
863
897
864
898
req , err := http .NewRequestWithContext (ctx , http .MethodDelete , path , nil )
865
899
if err != nil {
@@ -884,7 +918,7 @@ func (client *NginxClient) patch(path string, input interface{}, expectedStatusC
884
918
ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
885
919
defer cancel ()
886
920
887
- path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .version , path )
921
+ path = fmt .Sprintf ("%v/%v/%v/" , client .apiEndpoint , client .apiVersion , path )
888
922
889
923
jsonInput , err := json .Marshal (input )
890
924
if err != nil {
@@ -1339,7 +1373,7 @@ func (client *NginxClient) GetStreamZoneSync() (*StreamZoneSync, error) {
1339
1373
// GetLocationZones returns http/location_zones stats.
1340
1374
func (client * NginxClient ) GetLocationZones () (* LocationZones , error ) {
1341
1375
var locationZones LocationZones
1342
- if client .version < 5 {
1376
+ if client .apiVersion < 5 {
1343
1377
return & locationZones , nil
1344
1378
}
1345
1379
err := client .get ("http/location_zones" , & locationZones )
@@ -1353,7 +1387,7 @@ func (client *NginxClient) GetLocationZones() (*LocationZones, error) {
1353
1387
// GetResolvers returns Resolvers stats.
1354
1388
func (client * NginxClient ) GetResolvers () (* Resolvers , error ) {
1355
1389
var resolvers Resolvers
1356
- if client .version < 5 {
1390
+ if client .apiVersion < 5 {
1357
1391
return & resolvers , nil
1358
1392
}
1359
1393
err := client .get ("resolvers" , & resolvers )
@@ -1576,7 +1610,7 @@ func (client *NginxClient) UpdateStreamServer(upstream string, server StreamUpst
1576
1610
1577
1611
// Version returns client's current N+ API version.
1578
1612
func (client * NginxClient ) Version () int {
1579
- return client .version
1613
+ return client .apiVersion
1580
1614
}
1581
1615
1582
1616
func addPortToServer (server string ) string {
@@ -1598,7 +1632,7 @@ func addPortToServer(server string) string {
1598
1632
// GetHTTPLimitReqs returns http/limit_reqs stats.
1599
1633
func (client * NginxClient ) GetHTTPLimitReqs () (* HTTPLimitRequests , error ) {
1600
1634
var limitReqs HTTPLimitRequests
1601
- if client .version < 6 {
1635
+ if client .apiVersion < 6 {
1602
1636
return & limitReqs , nil
1603
1637
}
1604
1638
err := client .get ("http/limit_reqs" , & limitReqs )
@@ -1611,7 +1645,7 @@ func (client *NginxClient) GetHTTPLimitReqs() (*HTTPLimitRequests, error) {
1611
1645
// GetHTTPConnectionsLimit returns http/limit_conns stats.
1612
1646
func (client * NginxClient ) GetHTTPConnectionsLimit () (* HTTPLimitConnections , error ) {
1613
1647
var limitConns HTTPLimitConnections
1614
- if client .version < 6 {
1648
+ if client .apiVersion < 6 {
1615
1649
return & limitConns , nil
1616
1650
}
1617
1651
err := client .get ("http/limit_conns" , & limitConns )
@@ -1624,7 +1658,7 @@ func (client *NginxClient) GetHTTPConnectionsLimit() (*HTTPLimitConnections, err
1624
1658
// GetStreamConnectionsLimit returns stream/limit_conns stats.
1625
1659
func (client * NginxClient ) GetStreamConnectionsLimit () (* StreamLimitConnections , error ) {
1626
1660
var limitConns StreamLimitConnections
1627
- if client .version < 6 {
1661
+ if client .apiVersion < 6 {
1628
1662
return & limitConns , nil
1629
1663
}
1630
1664
err := client .get ("stream/limit_conns" , & limitConns )
0 commit comments