forked from kevholditch/gokong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apis.go
170 lines (135 loc) · 5.18 KB
/
apis.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
package gokong
import (
"encoding/json"
"fmt"
)
type ApiClient struct {
config *Config
}
type ApiRequest struct {
Name *string `json:"name"`
Hosts []*string `json:"hosts,omitempty"`
Uris []*string `json:"uris,omitempty"`
Methods []*string `json:"methods,omitempty"`
UpstreamUrl *string `json:"upstream_url"`
StripUri *bool `json:"strip_uri"`
PreserveHost *bool `json:"preserve_host"`
Retries *int `json:"retries,omitempty"`
UpstreamConnectTimeout *int `json:"upstream_connect_timeout,omitempty"`
UpstreamSendTimeout *int `json:"upstream_send_timeout,omitempty"`
UpstreamReadTimeout *int `json:"upstream_read_timeout,omitempty"`
HttpsOnly *bool `json:"https_only"`
HttpIfTerminated *bool `json:"http_if_terminated"`
}
type Api struct {
Id *string `json:"id"`
CreatedAt *int `json:"created_at"`
Name *string `json:"name"`
Hosts []*string `json:"hosts,omitempty"`
Uris []*string `json:"uris,omitempty"`
Methods []*string `json:"methods,omitempty"`
UpstreamUrl *string `json:"upstream_url"`
StripUri *bool `json:"strip_uri,omitempty"`
PreserveHost *bool `json:"preserve_host,omitempty"`
Retries *int `json:"retries,omitempty"`
UpstreamConnectTimeout *int `json:"upstream_connect_timeout,omitempty"`
UpstreamSendTimeout *int `json:"upstream_send_timeout,omitempty"`
UpstreamReadTimeout *int `json:"upstream_read_timeout,omitempty"`
HttpsOnly *bool `json:"https_only,omitempty"`
HttpIfTerminated *bool `json:"http_if_terminated,omitempty"`
}
type Apis struct {
Results []*Api `json:"data,omitempty"`
Total int `json:"total,omitempty"`
Next string `json:"next,omitempty"`
Offset string `json:"offset,omitempty"`
}
type ApiFilter struct {
Id string `url:"id,omitempty"`
Name string `url:"name,omitempty"`
UpstreamUrl string `url:"upstream_url,omitempty"`
Retries int `url:"retries,omitempty"`
Size int `url:"size,omitempty"`
Offset int `url:"offset,omitempty"`
}
const ApisPath = "/apis/"
func (apiClient *ApiClient) GetByName(name string) (*Api, error) {
return apiClient.GetById(name)
}
func (apiClient *ApiClient) GetById(id string) (*Api, error) {
_, body, errs := newGet(apiClient.config, apiClient.config.HostAddress+ApisPath+id).End()
if errs != nil {
return nil, fmt.Errorf("could not get api, error: %v", errs)
}
api := &Api{}
err := json.Unmarshal([]byte(body), api)
if err != nil {
return nil, fmt.Errorf("could not parse api get response, error: %v", err)
}
if api.Id == nil {
return nil, nil
}
return api, nil
}
func (apiClient *ApiClient) List() (*Apis, error) {
return apiClient.ListFiltered(nil)
}
func (apiClient *ApiClient) ListFiltered(filter *ApiFilter) (*Apis, error) {
address, err := addQueryString(apiClient.config.HostAddress+ApisPath, filter)
if err != nil {
return nil, fmt.Errorf("could not build query string for apis filter, error: %v", err)
}
_, body, errs := newGet(apiClient.config, address).End()
if errs != nil {
return nil, fmt.Errorf("could not get apis, error: %v", errs)
}
apis := &Apis{}
err = json.Unmarshal([]byte(body), apis)
if err != nil {
return nil, fmt.Errorf("could not parse apis list response, error: %v", err)
}
return apis, nil
}
func (apiClient *ApiClient) Create(newApi *ApiRequest) (*Api, error) {
_, body, errs := newPost(apiClient.config, apiClient.config.HostAddress+ApisPath).Send(newApi).End()
if errs != nil {
return nil, fmt.Errorf("could not create new api, error: %v", errs)
}
createdApi := &Api{}
err := json.Unmarshal([]byte(body), createdApi)
if err != nil {
return nil, fmt.Errorf("could not parse api creation response, error: %v %s", err, body)
}
if createdApi.Id == nil {
return nil, fmt.Errorf("could not create api, error: %v", body)
}
return createdApi, nil
}
func (apiClient *ApiClient) DeleteByName(name string) error {
return apiClient.DeleteById(name)
}
func (apiClient *ApiClient) DeleteById(id string) error {
res, _, errs := newDelete(apiClient.config, apiClient.config.HostAddress+ApisPath+id).End()
if errs != nil {
return fmt.Errorf("could not delete api, result: %v error: %v", res, errs)
}
return nil
}
func (apiClient *ApiClient) UpdateByName(name string, apiRequest *ApiRequest) (*Api, error) {
return apiClient.UpdateById(name, apiRequest)
}
func (apiClient *ApiClient) UpdateById(id string, apiRequest *ApiRequest) (*Api, error) {
_, body, errs := newPatch(apiClient.config, apiClient.config.HostAddress+ApisPath+id).Send(apiRequest).End()
if errs != nil {
return nil, fmt.Errorf("could not update api, error: %v", errs)
}
updatedApi := &Api{}
err := json.Unmarshal([]byte(body), updatedApi)
if err != nil {
return nil, fmt.Errorf("could not parse api update response, error: %v", err)
}
if updatedApi.Id == nil {
return nil, fmt.Errorf("could not update certificate, error: %v", body)
}
return updatedApi, nil
}