This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
requests.go
228 lines (199 loc) · 7.63 KB
/
requests.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
package applications
import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
// AppOpts allows to create or update an application using given parameters.
type AppOpts struct {
// Application name, which can contain 3 to 64 characters, starting with a letter.
// Only letters, digits and underscores (_) are allowed.
Name string `json:"name" required:"true"`
// Description of the application, which can contain a maximum of 255 characters.
// Chinese characters must be in UTF-8 or Unicode format.
Description string `json:"remark,omitempty"`
// Application key, which can contain 8 to 64 characters, starting with a letter or digit.
// Only letters, digits, hyphens (-) and underscores (_) are allowed.
AppKey string `json:"app_key,omitempty"`
// Application secret, which can contain 8 to 64 characters, starting with a letter or digit.
// Only letters, digits and the following special characters are allowed: _-!@#$%
AppSecret string `json:"app_secret,omitempty"`
}
type AppOptsBuilder interface {
ToAppOptsMap() (map[string]interface{}, error)
}
func (opts AppOpts) ToAppOptsMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create is a method by which to create function that create a APIG application.
func Create(client *golangsdk.ServiceClient, instanceId string, opts AppOptsBuilder) (r CreateResult) {
reqBody, err := opts.ToAppOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
return
}
func Update(client *golangsdk.ServiceClient, instanceId, appId string, opts AppOptsBuilder) (r UpdateResult) {
reqBody, err := opts.ToAppOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(resourceURL(client, instanceId, appId), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
//Get is a method to obtain the specified application according to the instanceId and appId.
func Get(client *golangsdk.ServiceClient, instanceId, appId string) (r GetResult) {
_, r.Err = client.Get(resourceURL(client, instanceId, appId), &r.Body, nil)
return
}
// ListOpts allows to filter list data using given parameters.
type ListOpts struct {
// App ID.
Id string `q:"id"`
// App name.
Name string `q:"name"`
// App status.
Status string `q:"status"`
// App key.
AppKey string `q:"app_key"`
// Creator of the application.
// USER: The app is created by the API user.
// MARKET: The app is allocated by the marketplace.
Creator string `q:"creator"`
// Offset from which the query starts.
// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
Offset int `q:"offset"`
// Number of items displayed on each page.
Limit int `q:"limit"`
// Parameter name (name) for exact matching.
PreciseSearch string `q:"precise_search"`
}
type ListOptsBuilder interface {
ToAppListQuery() (string, error)
}
func (opts ListOpts) ToAppListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
// List is a method to obtain an array of one or more APIG applications according to the query parameters.
func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
url := rootURL(client, instanceId)
if opts != nil {
query, err := opts.ToAppListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ApplicationPage{pagination.SinglePageBase(r)}
})
}
// SecretResetOpts allows to reset application secret value using given parameters.
type SecretResetOpts struct {
AppSecret string `json:"app_secret"`
}
type SecretResetOptsBuilder interface {
ToSecretResetOptsMap() (map[string]interface{}, error)
}
func (opts SecretResetOpts) ToSecretResetOptsMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func ResetAppSecret(client *golangsdk.ServiceClient, instanceId, appId string,
opts SecretResetOptsBuilder) (r ResetSecretResult) {
reqBody, err := opts.ToSecretResetOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(resetSecretURL(client, instanceId, appId), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// Delete is a method to delete an existing application.
func Delete(client *golangsdk.ServiceClient, instanceId, appId string) (r DeleteResult) {
_, r.Err = client.Delete(resourceURL(client, instanceId, appId), nil)
return
}
// AppCodeOpts allows to create an application code using given parameters.
type AppCodeOpts struct {
// AppCode value, which contains 64 to 180 characters, starting with a letter, plus sign (+) or slash (/).
// Only letters and the following special characters are allowed: +-_!@#$%/=
AppCode string `json:"app_code" required:"true"`
}
type AppCodeOptsBuilder interface {
ToAppCodeOptsMap() (map[string]interface{}, error)
}
func (opts AppCodeOpts) ToAppCodeOptsMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// CreateAppCode is a method by which to create function that create a code at sepcified APIG application using
// instanceId, appId and AppCodeOpts (code value).
func CreateAppCode(client *golangsdk.ServiceClient, instanceId, appId string,
opts AppCodeOptsBuilder) (r CreateCodeResult) {
reqBody, err := opts.ToAppCodeOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(codeURL(client, instanceId, appId), reqBody, &r.Body, nil)
return
}
// AutoGenerateAppCode is a method used to automatically create code in a specified application.
func AutoGenerateAppCode(client *golangsdk.ServiceClient, instanceId, appId string) (r AutoGenerateCodeResult) {
_, r.Err = client.Put(codeURL(client, instanceId, appId), nil, &r.Body, nil)
return
}
// GetAppCode is a method to obtain the specified code of the specified application of the specified instance using
// instanceId, appId and codeId.
func GetAppCode(client *golangsdk.ServiceClient, instanceId, appId, codeId string) (r GetCodeResult) {
_, r.Err = client.Get(codeResourceURL(client, instanceId, appId, codeId), &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 201},
})
return
}
// ListCodeOpts allows to filter application code list data using given parameters.
type ListCodeOpts struct {
// Offset from which the query starts.
// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
Offset int `q:"offset"`
// Number of items displayed on each page.
Limit int `q:"limit"`
}
type ListCodeOptsBuilder interface {
ToAppCodeListQuery() (string, error)
}
func (opts ListCodeOpts) ToAppCodeListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
// ListAppCode is a method to obtain the application code list of the specified application of the specified instance.
func ListAppCode(client *golangsdk.ServiceClient, instanceId, appId string, opts ListCodeOptsBuilder) pagination.Pager {
url := codeURL(client, instanceId, appId)
if opts != nil {
query, err := opts.ToAppCodeListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return AppCodePage{pagination.SinglePageBase(r)}
})
}
//RemoveAppCode is a method to delete an existing code from a specified application.
func RemoveAppCode(client *golangsdk.ServiceClient, instanceId, appId, codeId string) (r DeleteResult) {
_, r.Err = client.Delete(codeResourceURL(client, instanceId, appId, codeId), nil)
return
}