generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathbatch_request.go
More file actions
52 lines (40 loc) · 1.24 KB
/
batch_request.go
File metadata and controls
52 lines (40 loc) · 1.24 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
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package msgraph
import (
"net/http"
)
const maxNumRequestsPerBatch = 20
type singleRequest struct {
Body interface{} `json:"body"`
Headers map[string]string `json:"headers"`
ID string `json:"id"`
URL string `json:"url"`
Method string `json:"method"`
}
type fullBatchRequest struct {
Requests []*singleRequest `json:"requests"`
}
func (c *client) batchRequest(req fullBatchRequest, out interface{}) error {
u := MSGraphEndpoint(c.conf.OAuth2TenantType) + "/$batch"
_, err := c.CallJSON(http.MethodPost, u, req, out)
return err
}
func prepareBatchRequests(requests []*singleRequest) []fullBatchRequest {
numFullRequests := len(requests) / maxNumRequestsPerBatch
if len(requests)%maxNumRequestsPerBatch != 0 {
numFullRequests++
}
result := []fullBatchRequest{}
for i := 0; i < numFullRequests; i++ {
startIdx := i * maxNumRequestsPerBatch
endIdx := startIdx + maxNumRequestsPerBatch
if i == numFullRequests-1 {
endIdx = len(requests)
}
slice := requests[startIdx:endIdx]
batchReq := fullBatchRequest{Requests: slice}
result = append(result, batchReq)
}
return result
}