forked from pdbogen/circle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
55 lines (49 loc) · 1.32 KB
/
session.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
package circle
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
)
func NewSession(jwt string) (*Session, error) {
return &Session{Jwt: jwt}, nil
}
func (s *Session) Get(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("NewRequest: %v", err)
}
s.addHeaders(req)
res, err := http.DefaultClient.Do(req)
if err == nil {
log.Debugf("GET %q -> %d", url, res.StatusCode)
} else {
log.Debugf("GET %q -> %v", url, err)
}
return res, err
}
func (s *Session) Post(url string, body interface{}) (*http.Response, error) {
bodyBytes, err := json.Marshal(body)
if err != nil {
log.Fatalf("could not encode body %v: %v", body, err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(bodyBytes))
if err != nil {
return nil, fmt.Errorf("NewRequest: %v", err)
}
s.addHeaders(req)
res, err := http.DefaultClient.Do(req)
if err == nil {
log.Debugf("POST %q -> %d", url, res.StatusCode)
} else {
log.Debugf("POST %q -> %v", url, err)
}
return res, err
}
func (s *Session) addHeaders(req *http.Request) {
req.Header.Add("Accept", "application/json, text/plain, */*")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Origin", "https://circle.logi.com")
req.Header.Add("authorization", s.Jwt)
}