-
Notifications
You must be signed in to change notification settings - Fork 5
/
api_client.go
73 lines (61 loc) · 2.52 KB
/
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
package jamfpro
import (
"fmt"
"github.com/deploymenttheory/go-api-http-client/httpclient"
)
type Client struct {
HTTP *httpclient.Client
}
// ClientConfig combines authentication and environment settings for the client.
type ClientConfig struct {
Auth httpclient.AuthConfig
Environment httpclient.EnvironmentConfig
ClientOptions httpclient.ClientOptions
}
// BuildClient initializes a new Jamf Pro client with the given configuration.
// This is typically used when you want to manually specify the configuration.
// e.g by another caller application such as terraform or a custom application.
func BuildClient(config httpclient.ClientConfig) (*Client, error) {
httpClient, err := httpclient.BuildClient(config)
if err != nil {
return nil, err
}
return &Client{HTTP: httpClient}, nil
}
// BuildClientWithEnv initializes a new Jamf Pro client using configurations
// loaded from environment variables. This is typically used when by a user to
// use environment variables to configure the client locally or when running
// in a container or a CI/CD pipeline.
func BuildClientWithEnv() (*Client, error) {
// Create a new empty ClientConfig
config := &httpclient.ClientConfig{}
// Load configurations from environment variables
loadedConfig, err := httpclient.LoadConfigFromEnv(config)
if err != nil {
return nil, fmt.Errorf("failed to load HTTP client configuration from environment variables: %w", err)
}
// Build the HTTP client with the loaded configuration
httpClient, err := httpclient.BuildClient(*loadedConfig)
if err != nil {
return nil, fmt.Errorf("failed to build HTTP client: %w", err)
}
// Create and return the Jamf Pro client with the HTTP client
return &Client{HTTP: httpClient}, nil
}
// BuildClientWithConfigFile initializes a new Jamf Pro client using a
// configuration file for the HTTP client. This is typically used when a user
// wants to use a configuration file to configure the client locally.
func BuildClientWithConfigFile(configFilePath string) (*Client, error) {
// Load the HTTP client configuration from the specified file
loadedConfig, err := httpclient.LoadConfigFromFile(configFilePath)
if err != nil {
return nil, fmt.Errorf("failed to load HTTP client configuration from file: %w", err)
}
// Build the HTTP client with the loaded configuration
httpClient, err := httpclient.BuildClient(*loadedConfig)
if err != nil {
return nil, fmt.Errorf("failed to build HTTP client: %w", err)
}
// Create and return the Jamf Pro client with the HTTP client
return &Client{HTTP: httpClient}, nil
}