-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
52 lines (41 loc) · 1.13 KB
/
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
package jamf
import (
"fmt"
"net/http"
"time"
)
const (
uriAuthToken = "/uapi/auth/tokens"
)
// Client ... stores an object to talk with Jamf API
type Client struct {
username, password, url string
token *string
// The Http Client that is used to make requests
HttpClient *http.Client
HttpRetryTimeout time.Duration
// Option to specify extra headers like User-Agent
ExtraHeader map[string]string
}
type ResponseAuthToken struct {
Token *string `json:"token,omitempty"`
Expires *int `json:"expires,omitempty"`
}
// NewClient ... returns a new jamf.Client which can be used to access the API
func NewClient(username, password, url string) (*Client, error) {
c := &Client{
username: username,
password: password,
url: url,
token: nil,
HttpClient: http.DefaultClient,
HttpRetryTimeout: 60 * time.Second,
ExtraHeader: make(map[string]string),
}
var out *ResponseAuthToken
if err := c.doRequest("POST", uriAuthToken, nil, &out); err != nil {
return nil, fmt.Errorf("cannot get a token: %v", err)
}
c.token = out.Token
return c, nil
}