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
125 lines (111 loc) · 3.23 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
/*
Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
*/
package valuelists
import "github.com/huaweicloud/golangsdk"
var requestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
// CreateOpts the options to create reference table.
type CreateOpts struct {
Name string `json:"name" required:"true"`
Type string `json:"type" required:"true"`
Values []string `json:"values,omitempty"`
Description string `json:"description,omitempty"`
}
// Create a reference table.
func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*WafValueList, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var rst golangsdk.Result
_, err = c.Post(rootURL(c), b, &rst.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r WafValueList
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
//UpdateValueListOpts the options to update reference table.
type UpdateValueListOpts struct {
Name string `json:"name" required:"true"`
Values []string `json:"values,omitempty"`
Type string `json:"type,omitempty"`
Description *string `json:"description,omitempty"`
}
// Update reference table according options and id.
func Update(c *golangsdk.ServiceClient, id string, opts UpdateValueListOpts) (*WafValueList, error) {
b, err := golangsdk.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
var rst golangsdk.Result
_, err = c.Put(resourceURL(c, id), b, &rst.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r WafValueList
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
// Get a reference table by id.
func Get(c *golangsdk.ServiceClient, id string) (*WafValueList, error) {
var rst golangsdk.Result
_, err := c.Get(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r WafValueList
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
// ListValueListOpts the options to query a list of reference tables.
type ListValueListOpts struct {
Page int `q:"page"`
Pagesize int `q:"pagesize"`
}
// List : Query a list of reference tables according to the options.
func List(c *golangsdk.ServiceClient, opts ListValueListOpts) (*ListValueListRst, error) {
url := rootURL(c)
query, err := golangsdk.BuildQueryString(opts)
if err != nil {
return nil, err
}
url += query.String()
var rst golangsdk.Result
_, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r ListValueListRst
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}
// Delete reference table by id.
func Delete(c *golangsdk.ServiceClient, id string) (*WafValueList, error) {
var rst golangsdk.Result
_, err := c.DeleteWithResponse(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: requestOpts.MoreHeaders,
})
if err == nil {
var r WafValueList
rst.ExtractInto(&r)
return &r, nil
}
return nil, err
}