[management] Add Bearer token support (#3534)

This commit is contained in:
M. Essam 2025-03-18 22:48:36 +02:00 committed by GitHub
parent 919fe94fd5
commit 939419a0ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -68,28 +68,42 @@ type Client struct {
Events *EventsAPI Events *EventsAPI
} }
// New initialize new Client instance // New initialize new Client instance using PAT token
func New(managementURL, token string) *Client { func New(managementURL, token string) *Client {
client := &Client{ client := &Client{
managementURL: managementURL, managementURL: managementURL,
authHeader: "Token " + token, authHeader: "Token " + token,
} }
client.Accounts = &AccountsAPI{client} client.initialize()
client.Users = &UsersAPI{client}
client.Tokens = &TokensAPI{client}
client.Peers = &PeersAPI{client}
client.SetupKeys = &SetupKeysAPI{client}
client.Groups = &GroupsAPI{client}
client.Policies = &PoliciesAPI{client}
client.PostureChecks = &PostureChecksAPI{client}
client.Networks = &NetworksAPI{client}
client.Routes = &RoutesAPI{client}
client.DNS = &DNSAPI{client}
client.GeoLocation = &GeoLocationAPI{client}
client.Events = &EventsAPI{client}
return client return client
} }
// NewWithBearerToken initialize new Client instance using Bearer token type
func NewWithBearerToken(managementURL, token string) *Client {
client := &Client{
managementURL: managementURL,
authHeader: "Bearer " + token,
}
client.initialize()
return client
}
func (c *Client) initialize() {
c.Accounts = &AccountsAPI{c}
c.Users = &UsersAPI{c}
c.Tokens = &TokensAPI{c}
c.Peers = &PeersAPI{c}
c.SetupKeys = &SetupKeysAPI{c}
c.Groups = &GroupsAPI{c}
c.Policies = &PoliciesAPI{c}
c.PostureChecks = &PostureChecksAPI{c}
c.Networks = &NetworksAPI{c}
c.Routes = &RoutesAPI{c}
c.DNS = &DNSAPI{c}
c.GeoLocation = &GeoLocationAPI{c}
c.Events = &EventsAPI{c}
}
func (c *Client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) { func (c *Client) newRequest(ctx context.Context, method, path string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, c.managementURL+path, body) req, err := http.NewRequestWithContext(ctx, method, c.managementURL+path, body)
if err != nil { if err != nil {