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
253 lines (226 loc) · 9.87 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package throttles
import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
type ThrottlingPolicyOpts struct {
// Maximum number of times an API can be accessed within a specified period.
// The value of this parameter cannot exceed the default limit 200 TPS.
// This value must be a positive integer and cannot exceed 2,147,483,647.
ApiCallLimits int `json:"api_call_limits" required:"true"`
// Request throttling policy name, which can contain 3 to 64 characters, starting with a letter.
// Only letters, digits, and underscores (_) are allowed.
// Chinese characters must be in UTF-8 or Unicode format.
Name string `json:"name" required:"true"`
// Period of time for limiting the number of API calls.
// This parameter applies with each of the preceding three API call limits.
// This value must be a positive integer and cannot exceed 2,147,483,647.
TimeInterval int `json:"time_interval" required:"true"`
// Time unit for limiting the number of API calls.
// The valid values are as following:
// SECOND
// MINUTE
// HOUR
// DAY
TimeUnit string `json:"time_unit" required:"true"`
// Maximum number of times the API can be accessed by an app within the same period.
// The value of this parameter must be less than that of user_call_limits.
// This value must be a positive integer and cannot exceed 2,147,483,647.
AppCallLimits int `json:"app_call_limits,omitempty"`
// Description of the request throttling policy, which can contain a maximum of 255 characters.
// Chinese characters must be in UTF-8 or Unicode format.
Description string `json:"remark,omitempty"`
// Type of the request throttling policy.
// 1: exclusive, limiting the maximum number of times a single API bound to the policy can be called within
// the specified period.
// 2: shared, limiting the maximum number of times all APIs bound to the policy can be called within the
// specified period.
Type int `json:"type,omitempty"`
// Maximum number of times the API can be accessed by a user within the same period.
// The value of this parameter must be less than that of api_call_limits.
// This value must be a positive integer and cannot exceed 2,147,483,647.
UserCallLimits int `json:"user_call_limits,omitempty"`
// Maximum number of times the API can be accessed by an IP address within the same period.
// The value of this parameter must be less than that of api_call_limits.
// This value must be a positive integer and cannot exceed 2,147,483,647.
IpCallLimits int `json:"ip_call_limits,omitempty"`
}
type ThrottlingPolicyOptsBuilder interface {
ToThrottlingPolicyOptsMap() (map[string]interface{}, error)
}
func (opts ThrottlingPolicyOpts) ToThrottlingPolicyOptsMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// Create is a method by which to create function that create a new throttling policy.
func Create(client *golangsdk.ServiceClient, instanceId string, opts ThrottlingPolicyOptsBuilder) (r CreateResult) {
reqBody, err := opts.ToThrottlingPolicyOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
return
}
// Update is a method by which to udpate an existing throttle policy.
func Update(client *golangsdk.ServiceClient, instanceId, policyId string,
opts ThrottlingPolicyOptsBuilder) (r UpdateResult) {
reqBody, err := opts.ToThrottlingPolicyOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(resourceURL(client, instanceId, policyId), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// Get is a method to obtain an existing APIG throttling policy by policy ID.
func Get(client *golangsdk.ServiceClient, instanceId, policyId string) (r GetResult) {
_, r.Err = client.Get(resourceURL(client, instanceId, policyId), &r.Body, nil)
return
}
type ListOpts struct {
// Request throttling policy ID.
Id string `q:"id"`
// Request throttling policy name.
Name string `q:"name"`
// 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. The valid values are range form 1 to 500, default to 20.
Limit int `q:"limit"`
// Parameter name (name) for exact matching.
PreciseSearch string `q:"precise_search"`
}
type ListOptsBuilder interface {
ToListQuery() (string, error)
}
func (opts ListOpts) ToListQuery() (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 throttling policies 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.ToListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ThorttlePage{pagination.SinglePageBase(r)}
})
}
// Delete is a method to delete an existing throttling policy.
func Delete(client *golangsdk.ServiceClient, instanceId, policyId string) (r DeleteResult) {
_, r.Err = client.Delete(resourceURL(client, instanceId, policyId), nil)
return
}
// SpecThrottleCreateOpts is a struct which will be used to create a new special throttling policy.
type SpecThrottleCreateOpts struct {
// Maximum number of times the excluded object can access an API within the throttling period.
CallLimits int `json:"call_limits" required:"true"`
// Excluded app ID or excluded account ID.
ObjectId string `json:"object_id" required:"true"`
// Excluded object type, which supports APP and USER.
ObjectType string `json:"object_type" required:"true"`
}
// SpecThrottleCreateOptsBuilder is an interface which to support request body build of
// the special throttling policy creation.
type SpecThrottleCreateOptsBuilder interface {
ToSpecThrottleCreateOptsMap() (map[string]interface{}, error)
}
// ToSpecThrottleCreateOptsMap is a method which to build a request body by the SpecThrottleCreateOpts.
func (opts SpecThrottleCreateOpts) ToSpecThrottleCreateOptsMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// CreateSpecThrottle is a method by which to create a new special throttling policy.
func CreateSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId string,
opts SpecThrottleCreateOptsBuilder) (r SpecThrottleResult) {
reqBody, err := opts.ToSpecThrottleCreateOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(specRootURL(client, instanceId, policyId), reqBody, &r.Body, nil)
return
}
// SpecThrottleUpdateOpts is a struct which will be used to update an existing special throttling policy.
type SpecThrottleUpdateOpts struct {
// Maximum number of times the excluded object can access an API within the throttling period.
CallLimits int `json:"call_limits" required:"true"`
}
// SpecThrottleUpdateOptsBuilder is an interface which to support request body build of
// the special throttling policy updation.
type SpecThrottleUpdateOptsBuilder interface {
ToSpecThrottleUpdateOptsMap() (map[string]interface{}, error)
}
// ToSpecThrottleUpdateOptsMap is a method which to build a request body by the SpecThrottleUpdateOpts.
func (opts SpecThrottleUpdateOpts) ToSpecThrottleUpdateOptsMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// UpdateSpecThrottle is a method by which to update an existing special throttle policy.
func UpdateSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId, strategyId string,
opts SpecThrottleUpdateOptsBuilder) (r SpecThrottleResult) {
reqBody, err := opts.ToSpecThrottleUpdateOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(specResourceURL(client, instanceId, policyId, strategyId), reqBody, &r.Body,
&golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// SpecThrottlesListOpts allows to filter list data using given parameters.
type SpecThrottlesListOpts struct {
// Object type, which can be APP or USER.
ObjectType string `q:"object_type"`
// Name of an excluded app.
AppName string `q:"app_name"`
// 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. The valid values are range form 1 to 500, default to 20.
Limit int `q:"limit"`
}
// SpecThrottlesListOptsBuilder is an interface which to support request query build of
// the special throttling policies search.
type SpecThrottlesListOptsBuilder interface {
ToSpecThrottlesListQuery() (string, error)
}
// ToSpecThrottlesListQuery is a method which to build a request query by the SpecThrottlesListOpts.
func (opts SpecThrottlesListOpts) ToSpecThrottlesListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
// ListSpecThrottles is a method to obtain an array of one or more special throttling policies
// according to the query parameters.
func ListSpecThrottles(client *golangsdk.ServiceClient, instanceId, policyId string,
opts SpecThrottlesListOptsBuilder) pagination.Pager {
url := specRootURL(client, instanceId, policyId)
if opts != nil {
query, err := opts.ToSpecThrottlesListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return SpecThrottlePage{pagination.SinglePageBase(r)}
})
}
// DeleteSpecThrottle is a method to delete an existing special throttling policy.
func DeleteSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId, strategyId string) (r DeleteResult) {
_, r.Err = client.Delete(specResourceURL(client, instanceId, policyId, strategyId), nil)
return
}