-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
61 lines (51 loc) · 1.17 KB
/
utils.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func postJSON(url string, obj interface{}, token string) (*http.Response, error) {
tr := &http.Transport{
MaxIdleConnsPerHost: 10,
}
client := &http.Client{
Transport: tr,
Timeout: 5 * time.Second,
}
b := new(bytes.Buffer)
if err := json.NewEncoder(b).Encode(obj); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, b)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
if token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
return client.Do(req)
}
func clientLogin(baseURL, username, password string) (*loginResp, error) {
url := baseURL + "/login"
r, err := postJSON(url, map[string]string{"username": username, "password": password}, "")
if err != nil {
return nil, err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
var lresp loginResp
if err = json.Unmarshal(body, &lresp); err != nil {
return nil, err
}
if r.StatusCode >= 400 {
err = fmt.Errorf("Login Error: %s", lresp.Msg)
}
return &lresp, err
}