forked from deploymenttheory/go-api-sdk-jamfpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_helpers.go
90 lines (79 loc) · 2.59 KB
/
http_helpers.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
// http_helpers.go
package http_client
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
)
// ParseISO8601Date attempts to parse a string date in ISO 8601 format.
func ParseISO8601Date(dateStr string) (time.Time, error) {
return time.Parse(time.RFC3339, dateStr)
}
// EnsureHTTPScheme prefixes a URL with "http://" it defaults to "https://" doesn't already have an "https://".
func EnsureHTTPScheme(url string) string {
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
return fmt.Sprintf("https://%s", url)
}
return url
}
// CheckDeprecationHeader checks the response headers for the Deprecation header and logs a warning if present.
func CheckDeprecationHeader(resp *http.Response, logger Logger) {
deprecationHeader := resp.Header.Get("Deprecation")
if deprecationHeader != "" {
logger.Warn("API endpoint is deprecated as of", "Date", deprecationHeader)
}
}
// SetAuthenticationCredentials interprets and sets the credentials for the Client.
func (c *Client) SetAuthenticationCredentials(creds map[string]string) {
// Check for OAuth credentials
if clientID, ok := creds["clientID"]; ok {
if clientSecret, ok := creds["clientSecret"]; ok {
c.OAuthCredentials = OAuthCredentials{
ClientID: clientID,
ClientSecret: clientSecret,
}
c.AuthMethod = "oauth"
return
}
}
// Check for Bearer Token credentials
if username, ok := creds["username"]; ok {
if password, ok := creds["password"]; ok {
c.BearerTokenAuthCredentials = BearerTokenAuthCredentials{
Username: username,
Password: password,
}
c.AuthMethod = "bearer"
return
}
}
}
// GetOAuthCredentials retrieves the current OAuth credentials (Client ID and Client Secret)
// set for the client instance. Used for test cases.
func (c *Client) GetOAuthCredentials() OAuthCredentials {
return c.OAuthCredentials
}
// GetBearerAuthCredentials retrieves the current bearer auth credentials (Username and Password)
// set for the client instance. Used for test cases.
func (c *Client) GetBearerAuthCredentials() BearerTokenAuthCredentials {
return c.BearerTokenAuthCredentials
}
// LoadAuthConfig reads a JSON configuration file and decodes it into a ClientAuthConfig struct.
// It is used to retrieve authentication details like BaseURL, Username, and Password for the client.
func LoadAuthConfig(filename string) (*AuthConfig, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
config := &AuthConfig{}
decoder := json.NewDecoder(file)
err = decoder.Decode(config)
if err != nil {
return nil, err
}
return config, nil
}