cleaner options with custom http client

This commit is contained in:
Pedro Costa
2025-05-12 16:25:40 +01:00
parent d455f66564
commit 927be2b7d2
5 changed files with 49 additions and 35 deletions

View File

@@ -14,7 +14,7 @@ import (
type Client struct {
managementURL string
authHeader string
httpClient *http.Client
httpClient HttpClient
// Accounts NetBird account APIs
// see more: https://docs.netbird.io/api/resources/accounts
@@ -70,27 +70,24 @@ type Client struct {
}
// New initialize new Client instance using PAT token
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
func New(managementURL, token string) *Client {
return NewWithOptions(
WithManagementURL(managementURL),
WithPAT(token),
)
}
// NewWithBearerToken initialize new Client instance using Bearer token type
func NewWithBearerToken(managementURL, token string, opts ...option) *Client {
func NewWithBearerToken(managementURL, token string) *Client {
return NewWithOptions(
WithManagementURL(managementURL),
WithBearerToken(token),
)
}
func NewWithOptions(opts ...option) *Client {
client := &Client{
managementURL: managementURL,
authHeader: "Bearer " + token,
httpClient: http.DefaultClient,
httpClient: http.DefaultClient,
}
for _, option := range opts {
@@ -129,7 +126,7 @@ func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Re
req.Header.Add("Content-Type", "application/json")
}
resp, err := http.DefaultClient.Do(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}

View File

@@ -4,8 +4,32 @@ import "net/http"
type option func(*Client)
func WithHttpClient(client *http.Client) option {
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
func WithHttpClient(client HttpClient) option {
return func(c *Client) {
c.httpClient = client
}
}
func WithBearerToken(token string) option {
return WithAuthHeader("Bearer " + token)
}
func WithPAT(token string) option {
return WithAuthHeader("Token " + token)
}
func WithManagementURL(url string) option {
return func(c *Client) {
c.managementURL = url
}
}
func WithAuthHeader(value string) option {
return func(c *Client) {
c.authHeader = value
}
}

View File

@@ -15,11 +15,9 @@ type PoliciesAPI struct {
// List list all policies
// See more: https://docs.netbird.io/api/resources/policies#list-all-policies
func (a *PoliciesAPI) List(ctx context.Context, accountID string) ([]api.Policy, error) {
func (a *PoliciesAPI) List(ctx context.Context) ([]api.Policy, error) {
path := "/api/policies"
if accountID != "" {
path += "?account=" + accountID
}
resp, err := a.c.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
@@ -65,11 +63,9 @@ func (a *PoliciesAPI) Create(ctx context.Context, request api.PostApiPoliciesJSO
// Update update policy info
// See more: https://docs.netbird.io/api/resources/policies#update-a-policy
func (a *PoliciesAPI) Update(ctx context.Context, policyID string, request api.PutApiPoliciesPolicyIdJSONRequestBody, accountID string) (*api.Policy, error) {
func (a *PoliciesAPI) Update(ctx context.Context, policyID string, request api.PutApiPoliciesPolicyIdJSONRequestBody) (*api.Policy, error) {
path := "/api/policies/" + policyID
if accountID != "" {
path += "?account=" + accountID
}
requestBytes, err := json.Marshal(request)
if err != nil {
return nil, err

View File

@@ -43,11 +43,8 @@ func (a *SetupKeysAPI) Get(ctx context.Context, setupKeyID string) (*api.SetupKe
// Create generate new Setup Key
// See more: https://docs.netbird.io/api/resources/setup-keys#create-a-setup-key
func (a *SetupKeysAPI) Create(ctx context.Context, request api.PostApiSetupKeysJSONRequestBody, accountID string) (*api.SetupKeyClear, error) {
func (a *SetupKeysAPI) Create(ctx context.Context, request api.PostApiSetupKeysJSONRequestBody) (*api.SetupKeyClear, error) {
path := "/api/setup-keys"
if accountID != "" {
path += "?account=" + accountID
}
requestBytes, err := json.Marshal(request)
if err != nil {

View File

@@ -108,7 +108,7 @@ func TestSetupKeys_Create_200(t *testing.T) {
})
ret, err := c.SetupKeys.Create(context.Background(), api.PostApiSetupKeysJSONRequestBody{
ExpiresIn: 5,
}, "")
})
require.NoError(t, err)
assert.Equal(t, testSteupKeyGenerated, *ret)
})
@@ -124,7 +124,7 @@ func TestSetupKeys_Create_Err(t *testing.T) {
})
ret, err := c.SetupKeys.Create(context.Background(), api.PostApiSetupKeysJSONRequestBody{
ExpiresIn: 5,
}, "")
})
assert.Error(t, err)
assert.Equal(t, "No", err.Error())
assert.Nil(t, ret)
@@ -207,7 +207,7 @@ func TestSetupKeys_Integration(t *testing.T) {
Ephemeral: ptr(false),
Name: "test",
Type: "reusable",
}, "")
})
require.NoError(t, err)
assert.Equal(t, true, skClear.Valid)