-
Notifications
You must be signed in to change notification settings - Fork 5
/
shared_api_client.go
117 lines (99 loc) · 3.72 KB
/
shared_api_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// shared_api_client.go
// The jamfpro package offers a client for interacting with the Jamf Pro API.
// This client extends the foundational capabilities of the http_client package,
// adding methods specifically tailored for Jamf Pro's API endpoints.
// By embedding the http_client's Client, it leverages core HTTP methods,
// authentication mechanisms, and other utilities, while also enabling
// Jamf Pro-specific functionalities.
package jamfpro
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/http_client"
)
const (
maxConcurrentRequestsAllowed = 5 // Maximum allowed concurrent requests.
defaultTokenLifespan = 30 * time.Minute // Set default token lifespan
defaultTokenBufferPeriod = 5 * time.Minute // Set default token buffer period before attempting a token refresh
)
type Client struct {
HTTP *http_client.Client
}
type Config struct {
InstanceName string
DebugMode bool
Logger http_client.Logger
MaxConcurrentRequests int
TokenLifespan time.Duration
TokenRefreshBufferPeriod time.Duration
ClientID string
ClientSecret string
}
func NewClient(config Config) (*Client, error) {
// If not provided, use the default values from constants
if config.MaxConcurrentRequests == 0 {
config.MaxConcurrentRequests = maxConcurrentRequestsAllowed
}
if config.TokenLifespan == 0 {
config.TokenLifespan = defaultTokenLifespan
}
if config.TokenRefreshBufferPeriod == 0 {
config.TokenRefreshBufferPeriod = defaultTokenBufferPeriod
}
// Initialise http client
httpConfig := http_client.Config{
DebugMode: config.DebugMode,
Logger: config.Logger,
MaxConcurrentRequests: config.MaxConcurrentRequests,
TokenLifespan: config.TokenLifespan,
TokenRefreshBufferPeriod: config.TokenRefreshBufferPeriod,
}
httpCli, err := http_client.NewClient(config.InstanceName, httpConfig, nil)
if err != nil {
return nil, err // Return the error if HTTP client initialization fails
}
client := &Client{
HTTP: httpCli,
}
// Set auth credential configuration
creds := http_client.OAuthCredentials{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
}
client.SetClientOAuthCredentials(creds)
// validate credentials oauth credentials exist
if client.HTTP.GetOAuthCredentials().ClientID == "" || client.HTTP.GetOAuthCredentials().ClientSecret == "" {
return nil, fmt.Errorf("OAuth credentials (ClientID and ClientSecret) must be provided")
}
// return complete initialised client with auth credentials added.
return client, nil
}
// SetClientOAuthCredentials sets the http clients OAuth Credentials once a client has been initialised
func (c *Client) SetClientOAuthCredentials(creds http_client.OAuthCredentials) {
c.HTTP.SetOAuthCredentials(creds)
}
// SetAuthenticationCredentials sets the http clients bearer auth Credentials once a client has been initialised
func (c *Client) SetAuthenticationCredentials(creds map[string]string) {
c.HTTP.SetAuthenticationCredentials(creds)
}
// GetPerformanceMetrics returns the http client performance metrics from the client
func (c *Client) GetPerformanceMetrics() *http_client.ClientPerformanceMetrics {
return c.HTTP.GetPerformanceMetrics()
}
// LoadClientAuthConfig reads a JSON configuration file and decodes it into a ClientAuthConfig struct.
func LoadClientAuthConfig(filename string) (*http_client.ClientAuthConfig, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
config := &http_client.ClientAuthConfig{}
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
return nil, err
}
return config, nil
}