-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
227 lines (200 loc) · 5.65 KB
/
client.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Code generated by apic. DO NOT EDIT.
package ssh_host_certificates
import (
"bytes"
"context"
"net/url"
"text/template"
"github.com/ngrok/ngrok-api-go/v3"
"github.com/ngrok/ngrok-api-go/v3/internal/api"
)
type Client struct {
apiClient *api.Client
}
func NewClient(cfg *ngrok.ClientConfig) *Client {
return &Client{apiClient: api.NewClient(cfg)}
}
// Create a new SSH Host Certificate
//
// https://ngrok.com/docs/api#api-ssh-host-certificates-create
func (c *Client) Create(ctx context.Context, arg *ngrok.SSHHostCertificateCreate) (*ngrok.SSHHostCertificate, error) {
var res ngrok.SSHHostCertificate
var path bytes.Buffer
if err := template.Must(template.New("create_path").Parse("/ssh_host_certificates")).Execute(&path, arg); err != nil {
panic(err)
}
var (
apiURL = &url.URL{Path: path.String()}
bodyArg interface{}
)
apiURL.Path = path.String()
bodyArg = arg
if err := c.apiClient.Do(ctx, "POST", apiURL, bodyArg, &res); err != nil {
return nil, err
}
return &res, nil
}
// Delete an SSH Host Certificate
//
// https://ngrok.com/docs/api#api-ssh-host-certificates-delete
func (c *Client) Delete(ctx context.Context, id string) error {
arg := &ngrok.Item{ID: id}
var path bytes.Buffer
if err := template.Must(template.New("delete_path").Parse("/ssh_host_certificates/{{ .ID }}")).Execute(&path, arg); err != nil {
panic(err)
}
arg.ID = ""
var (
apiURL = &url.URL{Path: path.String()}
bodyArg interface{}
)
apiURL.Path = path.String()
if err := c.apiClient.Do(ctx, "DELETE", apiURL, bodyArg, nil); err != nil {
return err
}
return nil
}
// Get detailed information about an SSH Host Certficate
//
// https://ngrok.com/docs/api#api-ssh-host-certificates-get
func (c *Client) Get(ctx context.Context, id string) (*ngrok.SSHHostCertificate, error) {
arg := &ngrok.Item{ID: id}
var res ngrok.SSHHostCertificate
var path bytes.Buffer
if err := template.Must(template.New("get_path").Parse("/ssh_host_certificates/{{ .ID }}")).Execute(&path, arg); err != nil {
panic(err)
}
arg.ID = ""
var (
apiURL = &url.URL{Path: path.String()}
bodyArg interface{}
)
apiURL.Path = path.String()
if err := c.apiClient.Do(ctx, "GET", apiURL, bodyArg, &res); err != nil {
return nil, err
}
return &res, nil
}
// List all SSH Host Certificates issued on this account
//
// https://ngrok.com/docs/api#api-ssh-host-certificates-list
func (c *Client) list(ctx context.Context, arg *ngrok.Paging) (*ngrok.SSHHostCertificateList, error) {
if arg == nil {
arg = new(ngrok.Paging)
}
var res ngrok.SSHHostCertificateList
var path bytes.Buffer
if err := template.Must(template.New("list_path").Parse("/ssh_host_certificates")).Execute(&path, arg); err != nil {
panic(err)
}
var (
apiURL = &url.URL{Path: path.String()}
bodyArg interface{}
)
apiURL.Path = path.String()
queryVals := make(url.Values)
if arg.BeforeID != nil {
queryVals.Set("before_id", *arg.BeforeID)
}
if arg.Limit != nil {
queryVals.Set("limit", *arg.Limit)
}
apiURL.RawQuery = queryVals.Encode()
if err := c.apiClient.Do(ctx, "GET", apiURL, bodyArg, &res); err != nil {
return nil, err
}
return &res, nil
}
// List all SSH Host Certificates issued on this account
//
// https://ngrok.com/docs/api#api-ssh-host-certificates-list
func (c *Client) List(paging *ngrok.Paging) *Iter {
if paging == nil {
paging = new(ngrok.Paging)
}
if paging.Limit == nil {
paging.Limit = ngrok.String("100")
}
return &Iter{
client: c,
limit: paging.Limit,
lastItemID: paging.BeforeID,
n: -1,
}
}
// Iter allows the caller to iterate through a list of values while
// automatically fetching new pages worth of values from the API.
type Iter struct {
client *Client
n int
items []ngrok.SSHHostCertificate
err error
limit *string
lastItemID *string
}
// Next returns true if there is another value available in the iterator. If it
// returs true it also advances the iterator to that next available item.
func (it *Iter) Next(ctx context.Context) bool {
// no more if there is an error
if it.err != nil {
return false
}
// advance the iterator
it.n += 1
// is there an available item?
if it.n < len(it.items) {
it.lastItemID = ngrok.String(it.Item().ID)
return true
}
// fetch the next page
resp, err := it.client.list(ctx, &ngrok.Paging{
BeforeID: it.lastItemID,
Limit: it.limit,
})
if err != nil {
it.err = err
return false
}
// page with zero items means there are no more
if len(resp.SSHHostCertificates) == 0 {
return false
}
it.n = -1
it.items = resp.SSHHostCertificates
return it.Next(ctx)
}
// Item() returns the SSHHostCertificate currently
// pointed to by the iterator.
func (it *Iter) Item() *ngrok.SSHHostCertificate {
return &it.items[it.n]
}
// If Next() returned false because an error was encountered while fetching the
// next value Err() will return that error. A caller should always check Err()
// after Next() returns false.
func (it *Iter) Err() error {
return it.err
}
// Update an SSH Host Certificate
//
// https://ngrok.com/docs/api#api-ssh-host-certificates-update
func (c *Client) Update(ctx context.Context, arg *ngrok.SSHHostCertificateUpdate) (*ngrok.SSHHostCertificate, error) {
if arg == nil {
arg = new(ngrok.SSHHostCertificateUpdate)
}
var res ngrok.SSHHostCertificate
var path bytes.Buffer
if err := template.Must(template.New("update_path").Parse("/ssh_host_certificates/{{ .ID }}")).Execute(&path, arg); err != nil {
panic(err)
}
arg.ID = ""
var (
apiURL = &url.URL{Path: path.String()}
bodyArg interface{}
)
apiURL.Path = path.String()
bodyArg = arg
if err := c.apiClient.Do(ctx, "PATCH", apiURL, bodyArg, &res); err != nil {
return nil, err
}
return &res, nil
}