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
103 lines (84 loc) · 2.76 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
package topics
import (
"github.com/huaweicloud/golangsdk"
)
var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
}
//CreateOpsBuilder is used for creating topic parameters.
//any struct providing the parameters should implement this interface
type CreateOpsBuilder interface {
ToTopicCreateMap() (map[string]interface{}, error)
}
//CreateOps is a struct that contains all the parameters.
type CreateOps struct {
//Name of the topic to be created
Name string `json:"name" required:"true"`
//Topic display name
DisplayName string `json:"display_name,omitempty"`
EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
}
func (ops CreateOps) ToTopicCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(ops, "")
}
//CreateOpsBuilder is used for updating topic parameters.
//any struct providing the parameters should implement this interface
type UpdateOpsBuilder interface {
ToTopicUpdateMap() (map[string]interface{}, error)
}
//UpdateOps is a struct that contains all the parameters.
type UpdateOps struct {
//Topic display name
DisplayName string `json:"display_name,omitempty"`
}
func (ops UpdateOps) ToTopicUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(ops, "")
}
//Create a topic with given parameters.
func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
b, err := ops.ToTopicCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{201, 200},
MoreHeaders: RequestOpts.MoreHeaders,
})
return
}
//Update a topic with given parameters.
func Update(client *golangsdk.ServiceClient, ops UpdateOpsBuilder, id string) (r UpdateResult) {
b, err := ops.ToTopicUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders,
})
return
}
//delete a topic via id
func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
_, r.Err = client.Delete(deleteURL(client, id), &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: RequestOpts.MoreHeaders,
})
return
}
//get a topic with detailed information by id
func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
_, r.Err = client.Get(getURL(client, id), &r.Body, &golangsdk.RequestOpts{
MoreHeaders: RequestOpts.MoreHeaders,
})
return
}
//list all the topics
func List(client *golangsdk.ServiceClient) (r ListResult) {
_, r.Err = client.Get(listURL(client), &r.Body, &golangsdk.RequestOpts{
MoreHeaders: RequestOpts.MoreHeaders,
})
return
}