-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_admin.go
80 lines (58 loc) · 1.64 KB
/
api_admin.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
package gong
import (
goreq "github.com/parnurzeal/gorequest"
"github.com/pkg/errors"
)
type apiAdmin struct {
n *node
}
func (aa *apiAdmin) url() string { return aa.n.url + "/apis" }
func (aa *apiAdmin) Add(def *ApiDefinition) (*ApiObject, error) {
obj := ApiObject{}
resp, _, errs := goreq.New().Post(aa.url() + "/").
Send(def).EndStruct(&obj)
err := combineRequestErrors(resp, errs)
if err != nil {
return nil, errors.Wrap(err, "http request add api")
}
return &obj, nil
}
func (aa *apiAdmin) Retrieve(name_or_id string) (*ApiObject, error) {
obj := ApiObject{}
resp, _, errs := goreq.New().Get(aa.url() + "/" + name_or_id).
EndStruct(&obj)
err := combineRequestErrors(resp, errs)
if err != nil {
return nil, errors.Wrapf(err,
"http request to get api of %v", name_or_id)
}
return &obj, nil
}
func (aa *apiAdmin) List(filters ...string) ([]*ApiObject, error) {
// TODO
return nil, nil
}
func (aa *apiAdmin) Update(
name_or_id string, def *ApiDefinition) (*ApiObject, error) {
obj := ApiObject{}
resp, _, errs := goreq.New().Patch(aa.url() + "/" + name_or_id).
Send(def).EndStruct(&obj)
err := combineRequestErrors(resp, errs)
if err != nil {
return nil, errors.Wrapf(err,
"http request to update api of %v", name_or_id)
}
return &obj, nil
}
func (aa *apiAdmin) Replace(def *ApiDefinition) (*ApiObject, error) {
// TODO
return nil, nil
}
func (aa *apiAdmin) Delete(name_or_id string) error {
resp, _, errs := goreq.New().Delete(aa.url() + "/" + name_or_id).End()
err := combineRequestErrors(resp, errs)
if err != nil {
return errors.Wrapf(err, "http request delete api %v", name_or_id)
}
return nil
}