-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.go
235 lines (195 loc) · 5.5 KB
/
base.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
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
// Copyright 2023 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package casvisorsdk
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
// client is a shared http Client.
var client HttpClient = &http.Client{}
// SetHttpClient sets custom http Client.
func SetHttpClient(httpClient HttpClient) {
client = httpClient
}
// HttpClient interface has the method required to use a type as custom http client.
// The net/*http.Client type satisfies this interface.
type HttpClient interface {
Do(*http.Request) (*http.Response, error)
}
type Response struct {
Status string `json:"status"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
Data2 interface{} `json:"data2"`
}
// DoGetResponse is a general function to get response from param url through HTTP Get method.
func (c *Client) DoGetResponse(url string) (*Response, error) {
respBytes, err := c.doGetBytesRawWithoutCheck(url)
if err != nil {
return nil, err
}
var response Response
err = json.Unmarshal(respBytes, &response)
if err != nil {
return nil, err
}
if response.Status != "ok" {
return nil, fmt.Errorf(response.Msg)
}
return &response, nil
}
// DoGetBytes is a general function to get response data in bytes from param url through HTTP Get method.
func (c *Client) DoGetBytes(url string) ([]byte, error) {
response, err := c.DoGetResponse(url)
if err != nil {
return nil, err
}
res, err := json.Marshal(response.Data)
if err != nil {
return nil, err
}
return res, nil
}
// DoGetBytesRaw is a general function to get response from param url through HTTP Get method.
func (c *Client) DoGetBytesRaw(url string) ([]byte, error) {
respBytes, err := c.doGetBytesRawWithoutCheck(url)
if err != nil {
return nil, err
}
var response Response
err = json.Unmarshal(respBytes, &response)
if err == nil && response.Status == "error" {
return nil, errors.New(response.Msg)
}
return respBytes, nil
}
func (c *Client) DoPost(action string, queryMap map[string]string, postBytes []byte, isForm, isFile bool) (*Response, error) {
url := c.GetUrl(action, queryMap)
var err error
var contentType string
var body io.Reader
if isForm {
if isFile {
contentType, body, err = createFormFile(map[string][]byte{"file": postBytes})
if err != nil {
return nil, err
}
} else {
var params map[string]string
err = json.Unmarshal(postBytes, ¶ms)
if err != nil {
return nil, err
}
contentType, body, err = createForm(params)
if err != nil {
return nil, err
}
}
} else {
contentType = "text/plain;charset=UTF-8"
body = bytes.NewReader(postBytes)
}
respBytes, err := c.DoPostBytesRaw(url, contentType, body)
if err != nil {
return nil, err
}
var response Response
err = json.Unmarshal(respBytes, &response)
if err != nil {
return nil, err
}
if response.Status != "ok" {
return nil, fmt.Errorf(response.Msg)
}
return &response, nil
}
// DoPostBytesRaw is a general function to post a request from url, body through HTTP Post method.
func (c *Client) DoPostBytesRaw(url string, contentType string, body io.Reader) ([]byte, error) {
if contentType == "" {
contentType = "text/plain;charset=UTF-8"
}
var resp *http.Response
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.ClientId, c.ClientSecret)
req.Header.Set("Content-Type", contentType)
resp, err = client.Do(req)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respByte, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respByte, nil
}
// doGetBytesRawWithoutCheck is a general function to get response from param url through HTTP Get method without checking response status
func (c *Client) doGetBytesRawWithoutCheck(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.ClientId, c.ClientSecret)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(resp.Body)
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBytes, nil
}
// modifyRecord is an encapsulation of permission CUD(Create, Update, Delete) operations.
// possible actions are `add-record`, `update-record`, `delete-record`,
func (c *Client) modifyRecord(action string, record *Record, columns []string) (*Response, bool, error) {
queryMap := map[string]string{
"id": fmt.Sprintf("%s/%s", record.Owner, record.Name),
}
if len(columns) != 0 {
queryMap["columns"] = strings.Join(columns, ",")
}
if record.Owner == "" {
record.Owner = "admin"
}
postBytes, err := json.Marshal(record)
if err != nil {
return nil, false, err
}
resp, err := c.DoPost(action, queryMap, postBytes, false, false)
if err != nil {
return nil, false, err
}
return resp, resp.Data == "Affected", nil
}