-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
63 lines (56 loc) · 1.37 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
53
54
55
56
57
58
59
60
61
62
63
// jumpcloud is a package to interact with the JumpCloud API
//
// # Create a new client
// c, err := jumpcloud.NewClient(os.Getenv("JC_API_KEY"))
//
// # Use the Client
// groups, err := c.GetAllUserGroups()
//
package jumpcloud
import (
"io"
"net/http"
"net/url"
"time"
)
const HostURL = "https://console.jumpcloud.com"
// Client is a struct to hold the client data and http client
type Client struct {
HostURL *url.URL
HTTPClient *http.Client
Headers http.Header
}
// NewClient factory returns a prepared client
// token is the JumpCloud API key
func NewClient(token string) (*Client, error) {
parsedUrl, err := url.Parse(HostURL)
c := Client{
HostURL: parsedUrl,
HTTPClient: &http.Client{Timeout: 10 * time.Second},
Headers: http.Header{
"Accept": {"application/json"},
"Content-Type": {"application/json"},
"x-api-key": {token},
},
}
if err != nil {
return nil, err
}
return &c, nil
}
// doRequest is a helper function to prepare http requests
// This is not implemented yet. Some methods need access to the request object still.
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
// Prepare and send request
req.Header = c.Headers
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, err
}