-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.go
More file actions
245 lines (183 loc) · 5.37 KB
/
methods.go
File metadata and controls
245 lines (183 loc) · 5.37 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package msgraph
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
const baseUrl = "https://graph.microsoft.com/v1.0"
// Params serves to map data for post in the request
type Params map[string]interface{}
// Headers serves to add extra request headers
type Headers map[string]string
// Make request and return the response
func (c *Client) execute(method string, path string, params interface{}, headers Headers, model interface{}) error {
token, err := c.accessToken()
if err != nil {
return err
}
var request *http.Request
endpoint := baseUrl + path
// check for params
if params != nil {
// marshal params
b, err := json.Marshal(params)
if err != nil {
return err
}
// send as body
if method != http.MethodGet {
// set payload with params
payload := strings.NewReader(string(b))
// set request with payload
request, _ = http.NewRequest(method, endpoint, payload)
} else {
var values Params
// convert any type to params
if err = json.Unmarshal(b, &values); err != nil {
return err
}
// init request
request, _ = http.NewRequest(method, endpoint, nil)
// init query string
query := request.URL.Query()
// add params
for key, value := range values {
query.Add(key, AnyToString(value))
}
// set query string
request.URL.RawQuery = query.Encode()
}
} else {
// set request without payload
request, _ = http.NewRequest(method, endpoint, nil)
}
request.Header.Add("Authorization", "Bearer "+token)
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-type", "application/json")
// add extra headers
if headers != nil {
for key, value := range headers {
request.Header.Add(key, value)
}
}
if c.Debug {
fmt.Printf("[DEBUG] %s %s\n", request.Method, request.URL.String())
for k, v := range request.Header {
if k == "Authorization" {
fmt.Printf("[DEBUG] Header %s: %s...]\n", k, v[0][:30])
} else {
fmt.Printf("[DEBUG] Header %s: %s\n", k, v)
}
}
}
response, err := http.DefaultClient.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
// read response
data, err := io.ReadAll(response.Body)
if err != nil {
return err
}
if c.Debug {
fmt.Printf("[DEBUG] Response Status: %d\n", response.StatusCode)
body := string(data)
if len(body) > 500 {
body = body[:500] + "..."
}
fmt.Printf("[DEBUG] Response Body: %s\n", body)
}
// init error response
msg := &ErrMessage{}
if len(data) > 0 {
// check for error message
if err = json.Unmarshal(data, msg); err == nil && msg.ErrorMessage != "" {
return msg
}
if err != nil {
return err
}
err = json.Unmarshal(data, model)
if err != nil {
return err
}
}
// verify status code
if NotIn(response.StatusCode, http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent) {
// return body as error message
if len(data) > 0 {
return errors.New(string(data))
}
// return http status as error
return errors.New(response.Status)
}
// parse data
return nil
}
// executeRaw makes a request and returns the raw response bytes without JSON parsing.
// Used for endpoints that return binary content (e.g. attachment downloads).
func (c *Client) executeRaw(method string, path string, headers Headers) ([]byte, error) {
token, err := c.accessToken()
if err != nil {
return nil, err
}
endpoint := baseUrl + path
request, _ := http.NewRequest(method, endpoint, nil)
request.Header.Add("Authorization", "Bearer "+token)
if headers != nil {
for key, value := range headers {
request.Header.Add(key, value)
}
}
if c.Debug {
fmt.Printf("[DEBUG] %s %s\n", request.Method, request.URL.String())
}
response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
if c.Debug {
fmt.Printf("[DEBUG] Response Status: %d\n", response.StatusCode)
fmt.Printf("[DEBUG] Response Size: %d bytes\n", len(data))
}
if NotIn(response.StatusCode, http.StatusOK, http.StatusCreated, http.StatusAccepted) {
if len(data) > 0 {
return nil, errors.New(string(data))
}
return nil, errors.New(response.Status)
}
return data, nil
}
// GetRaw executes GET requests and returns raw bytes
func (c *Client) GetRaw(path string, headers Headers) ([]byte, error) {
return c.executeRaw(http.MethodGet, path, headers)
}
// Get executes GET requests
func (c *Client) Get(path string, params interface{}, headers Headers, model interface{}) error {
return c.execute(http.MethodGet, path, params, headers, model)
}
// Post executes POST requests
func (c *Client) Post(path string, params interface{}, headers Headers, model interface{}) error {
return c.execute(http.MethodPost, path, params, headers, model)
}
// Put executes PUT requests
func (c *Client) Put(path string, params interface{}, headers Headers, model interface{}) error {
return c.execute(http.MethodPut, path, params, headers, model)
}
// Patch executes PATCH requests
func (c *Client) Patch(path string, params interface{}, headers Headers, model interface{}) error {
return c.execute(http.MethodPatch, path, params, headers, model)
}
// Delete executes DELETE requests
func (c *Client) Delete(path string, params interface{}, headers Headers, model interface{}) error {
return c.execute(http.MethodDelete, path, params, headers, model)
}