[management] rest client with options

This commit is contained in:
Pedro Costa
2025-05-09 08:47:22 +01:00
parent fcd2c15a37
commit b32c5522a2
2 changed files with 26 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ import (
type Client struct {
managementURL string
authHeader string
httpClient *http.Client
// Accounts NetBird account APIs
// see more: https://docs.netbird.io/api/resources/accounts
@@ -69,21 +70,33 @@ type Client struct {
}
// New initialize new Client instance using PAT token
func New(managementURL, token string) *Client {
func New(managementURL, token string, opts ...option) *Client {
client := &Client{
managementURL: managementURL,
authHeader: "Token " + token,
httpClient: http.DefaultClient,
}
for _, option := range opts {
option(client)
}
client.initialize()
return client
}
// NewWithBearerToken initialize new Client instance using Bearer token type
func NewWithBearerToken(managementURL, token string) *Client {
func NewWithBearerToken(managementURL, token string, opts ...option) *Client {
client := &Client{
managementURL: managementURL,
authHeader: "Bearer " + token,
httpClient: http.DefaultClient,
}
for _, option := range opts {
option(client)
}
client.initialize()
return client
}

View File

@@ -0,0 +1,11 @@
package rest
import "net/http"
type option func(*Client)
func WithHttpClient(client *http.Client) option {
return func(c *Client) {
c.httpClient = client
}
}