-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
151 lines (139 loc) · 3.3 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package GraphClient
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"time"
)
type Client struct {
HomeAccountId string
TokenCache ITokenCache
AuthClient *Auth
}
type GraphResponse struct {
Body string
RawBody []byte
Headers http.Header
RawResponse *http.Response
}
const baseUrl = "https://%s/%s"
func newHttpClient() *http.Client {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
Timeout: 2 * time.Minute,
Transport: transport,
}
return client
}
//GetClient
//
//Get a *Client
//
//HomeAccountId can be set to "",but should be set by WithHomeAccountId()
func GetClient(auth *Auth, HomeAccountId string, tokenCache ...ITokenCache) *Client {
c := &Client{
HomeAccountId: HomeAccountId,
AuthClient: auth,
}
if len(tokenCache) == 0 {
c.TokenCache = auth.TokenCache
} else {
c.TokenCache = tokenCache[0]
}
return c
}
//WithHomeAccountId
//
//Set HomeAccountId.
//
//If you set it when create Client, previous will be override.
func (t *Client) WithHomeAccountId(HomeAccountId string) *Client {
t.HomeAccountId = HomeAccountId
return t
}
// Request
//
//method:HTTP method used to request
//
//path: e.g.:https://graph.microsoft.com/v1.0/me => /me
//
//body: Set "" when method is GET, otherwise you will get an error
//
//header:Optional.Set custom headers by it.
func (t *Client) Request(method string, path string, body []byte, header *map[string][]string, query *map[string][]string) (*GraphResponse, error) {
if t.HomeAccountId == "" {
return nil, errors.New("HomeAccountId is not specific")
}
token := t.TokenCache.Get(t.HomeAccountId)
if token == nil {
return nil, errors.New("cannot get token from token cache")
}
validateToken, err, changed := t.AuthClient.GetValidateToken(*token)
if err != nil {
return nil, err
}
if changed {
err := t.TokenCache.Set(t.HomeAccountId, validateToken)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, fmt.Sprintf(baseUrl, t.AuthClient.Endpoint, t.AuthClient.ApiVersion)+path, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header["Authorization"] = []string{"Bearer " + validateToken.AccessToken}
if header != nil {
for k, v := range *header {
req.Header[k] = v
}
}
if query != nil {
q := req.URL.Query()
for k, v := range *query {
for _, i := range v {
q.Add(k, i)
}
}
req.URL.RawQuery = q.Encode()
}
client := newHttpClient()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
rec := &GraphResponse{}
rec.Body = string(respBody)
rec.RawBody = respBody
rec.RawResponse = resp
rec.Headers = resp.Header
return rec, nil
}
func (t *GraphResponse) ToJson() (map[string]interface{}, error) {
m := make(map[string]interface{})
err := json.Unmarshal(t.RawBody, &m)
if err != nil {
return nil, err
}
return m, nil
}
func (t *GraphResponse) GetJson() gjson.Result {
return gjson.Parse(t.Body)
}