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
213 lines (185 loc) · 7.21 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
package responses
import (
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/pagination"
)
// ResponseOpts allows to create a new custom response or update the existing custom response using given parameters.
type ResponseOpts struct {
// APIG group name, which can contain 1 to 64 characters, only letters, digits, hyphens (-) and
// underscores (_) are allowed.
Name string `json:"name" required:"true"`
// Response type definition, which includes a key and value. Options of the key:
// AUTH_FAILURE: Authentication failed.
// AUTH_HEADER_MISSING: The identity source is missing.
// AUTHORIZER_FAILURE: Custom authentication failed.
// AUTHORIZER_CONF_FAILURE: There has been a custom authorizer error.
// AUTHORIZER_IDENTITIES_FAILURE: The identity source of the custom authorizer is invalid.
// BACKEND_UNAVAILABLE: The backend service is unavailable.
// BACKEND_TIMEOUT: Communication with the backend service timed out.
// THROTTLED: The request was rejected due to request throttling.
// UNAUTHORIZED: The app you are using has not been authorized to call the API.
// ACCESS_DENIED: Access denied.
// NOT_FOUND: No API is found.
// REQUEST_PARAMETERS_FAILURE: The request parameters are incorrect.
// DEFAULT_4XX: Another 4XX error occurred.
// DEFAULT_5XX: Another 5XX error occurred.
// Each error type is in JSON format.
Responses map[string]ResponseInfo `json:"responses,omitempty"`
// APIG dedicated instance ID.
InstanceId string `json:"-"`
// APIG group ID.
GroupId string `json:"-"`
}
type ResponseInfo struct {
// Response body template.
Body string `json:"body" required:"true"`
// HTTP status code of the response. If omitted, the status will be cancelled.
Status int `json:"status,omitempty"`
// Indicates whether the response is the default response.
// Only the response of the API call is supported.
IsDefault bool `json:"default,omitempty"`
}
type ResponseOptsBuilder interface {
ToResponseOptsMap() (map[string]interface{}, error)
GetInstanceId() string
GetGroupId() string
}
func (opts ResponseOpts) ToResponseOptsMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
func (opts ResponseOpts) GetInstanceId() string {
return opts.InstanceId
}
func (opts ResponseOpts) GetGroupId() string {
return opts.GroupId
}
// Create is a method by which to create function that create a new custom response.
func Create(client *golangsdk.ServiceClient, opts ResponseOptsBuilder) (r CreateResult) {
reqBody, err := opts.ToResponseOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId())),
reqBody, &r.Body, nil)
return
}
// Update is a method by which to create function that udpate the existing custom response.
func Update(client *golangsdk.ServiceClient, respId string, opts ResponseOptsBuilder) (r UpdateResult) {
reqBody, err := opts.ToResponseOptsMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(resourceURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId()), respId),
reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// Get is a method to obtain the specified custom response according to the instanceId, appId and respId.
func Get(client *golangsdk.ServiceClient, instanceId, groupId, respId string) (r GetResult) {
_, r.Err = client.Get(resourceURL(client, buildResponsesPath(instanceId, groupId), respId), &r.Body, nil)
return
}
// ListOpts allows to filter list data using given parameters.
type ListOpts 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. The valid values are range form 1 to 500, default to 20.
Limit int `q:"limit"`
// APIG dedicated instance ID.
InstanceId string `json:"-"`
// APIG group ID.
GroupId string `json:"-"`
}
type ListOptsBuilder interface {
ToListQuery() (string, error)
GetInstanceId() string
GetGroupId() string
}
func (opts ListOpts) GetInstanceId() string {
return opts.InstanceId
}
func (opts ListOpts) GetGroupId() string {
return opts.GroupId
}
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 custom reponses according to the query parameters.
func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := rootURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId()))
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 ResponsePage{pagination.SinglePageBase(r)}
})
}
// Delete is a method to delete the existing custom response.
func Delete(client *golangsdk.ServiceClient, instanceId, groupId, respId string) (r DeleteResult) {
_, r.Err = client.Delete(resourceURL(client, buildResponsesPath(instanceId, groupId), respId), nil)
return
}
// SpecRespOpts is used to build the APIG response url. All parameters are required.
type SpecRespOpts struct {
InstanceId string
GroupId string
RespId string
}
type SpecRespOptsBuilder interface {
GetInstanceId() string
GetGroupId() string
GetResponseId() string
}
func (opts SpecRespOpts) GetInstanceId() string {
return opts.InstanceId
}
func (opts SpecRespOpts) GetGroupId() string {
return opts.GroupId
}
func (opts SpecRespOpts) GetResponseId() string {
return opts.RespId
}
// GetSpecResp is a method to get the specifies custom response configuration from an group.
func GetSpecResp(client *golangsdk.ServiceClient, respType string, opts SpecRespOptsBuilder) (r GetSpecRespResult) {
_, r.Err = client.Get(specResponsesURL(client, buildResponsesPath(opts.GetInstanceId(), opts.GetGroupId()),
opts.GetResponseId(), respType), &r.Body, nil)
return
}
type UpdateSpecRespBuilder interface {
ToSpecRespUpdateMap() (map[string]interface{}, error)
}
func (opts ResponseInfo) ToSpecRespUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "")
}
// UpdateSpecResp is a method to update an existing custom response configuration from an group.
func UpdateSpecResp(client *golangsdk.ServiceClient, respType string, specOpts SpecRespOptsBuilder,
respOpts UpdateSpecRespBuilder) (r UpdateSpecRespResult) {
reqBody, err := respOpts.ToSpecRespUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(specResponsesURL(client, buildResponsesPath(specOpts.GetInstanceId(), specOpts.GetGroupId()),
specOpts.GetResponseId(), respType), reqBody, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
return
}
// DeleteSpecResp is a method to delete an existing custom response configuration from an group.
func DeleteSpecResp(client *golangsdk.ServiceClient, respType string, specOpts SpecRespOptsBuilder) (r DeleteResult) {
_, r.Err = client.Delete(specResponsesURL(client, buildResponsesPath(specOpts.GetInstanceId(),
specOpts.GetGroupId()), specOpts.GetResponseId(), respType), nil)
return
}