-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
58 lines (49 loc) · 1.08 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
package mdmdirector
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
// HostURL - Default MdmDirector URL
const HostURL string = "http://localhost:8000"
// Client -
type Client struct {
HostURL string
HTTPClient *http.Client
Token string
Username string
Password string
}
// NewClient -
func NewClient(host, username, password *string) (*Client, error) {
c := Client{
HTTPClient: &http.Client{Timeout: 10 * time.Second},
HostURL: HostURL,
Username: *username,
Password: *password,
}
if host != nil {
c.HostURL = *host
}
if c.Username == "" || c.Password == "" {
return nil, fmt.Errorf("define username and password")
}
return &c, nil
}
func (c *Client) doRequest(req *http.Request) ([]byte, error) {
req.SetBasicAuth(c.Username, c.Password)
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("status: %d, body: %s", res.StatusCode, body)
}
return body, err
}