-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_request.go
128 lines (120 loc) · 2.99 KB
/
admin_request.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
package vcapool
/*
type AdminRequest struct {
URL string
}
func NewAdminRequest() *AdminRequest {
return &AdminRequest{
URL: vcago.Config.GetEnvString("ADMIN_URL", "n", "http://172.4.5.3"),
}
}
func (i *AdminRequest) Get(path string) (r *vcago.Response, err error) {
url := i.URL + path
request := new(http.Request)
request, err = http.NewRequest("GET", url, nil)
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
response := new(http.Response)
response, err = client.Do(request)
if err != nil {
return
}
defer response.Body.Close()
var bodyBytes []byte
if response.StatusCode != 200 {
if bodyBytes, err = ioutil.ReadAll(response.Body); err != nil {
return
}
body := new(interface{})
if err = json.Unmarshal(bodyBytes, body); err != nil {
return
}
return nil, errors.New(response.Status)
}
r = new(vcago.Response)
if bodyBytes, err = ioutil.ReadAll(response.Body); err != nil {
return
}
if err = json.Unmarshal(bodyBytes, r); err != nil {
return
}
return
}
func (i *AdminRequest) Post(path string, data interface{}) (r *vcago.Response, err error) {
var jsonData []byte
if jsonData, err = json.Marshal(data); err != nil {
return
}
url := i.URL + path
request := new(http.Request)
request, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
response := new(http.Response)
response, err = client.Do(request)
if err != nil {
return
}
defer response.Body.Close()
var bodyBytes []byte
if response.StatusCode != 201 {
if bodyBytes, err = ioutil.ReadAll(response.Body); err != nil {
return
}
body := new(interface{})
if err = json.Unmarshal(bodyBytes, body); err != nil {
return
}
return nil, errors.New(response.Status)
}
r = new(vcago.Response)
if bodyBytes, err = ioutil.ReadAll(response.Body); err != nil {
return
}
if err = json.Unmarshal(bodyBytes, r); err != nil {
return
}
return
}
func (i *AdminRequest) GetUser(query *UserQuery) (r *UserList, err error) {
uRL := "/admin/users"
if query != nil {
encoder := qs.NewEncoder()
var values url.Values
if values, err = encoder.Values(query); err != nil {
return
}
uRL = uRL + "?" + values.Encode()
}
response := new(vcago.Response)
if response, err = i.Get(uRL); err != nil {
return
}
if response.Payload != nil {
bytes, _ := json.Marshal(&response.Payload)
r = new(UserList)
_ = json.Unmarshal(bytes, &r)
}
return
}
func (i *AdminRequest) GetCrew(query *CrewQuery) (r *CrewList, err error) {
uRL := "/admin/crews"
if query != nil {
encoder := qs.NewEncoder()
var values url.Values
if values, err = encoder.Values(query); err != nil {
return
}
uRL = uRL + "?" + values.Encode()
}
response := new(vcago.Response)
if response, err = i.Get(uRL); err != nil {
return
}
if response.Payload != nil {
bytes, _ := json.Marshal(&response.Payload)
r = new(CrewList)
_ = json.Unmarshal(bytes, &r)
}
return
}*/