Files
ngrok-api-go/credentials/client.go
T
ngrok-release-bot 0c9865fd44 cut new release
2021-05-16 04:22:14 +00:00

236 lines
5.3 KiB
Go

// Code generated by apic. DO NOT EDIT.
package credentials
import (
"bytes"
"context"
"net/url"
"text/template"
"github.com/ngrok/ngrok-api-go"
)
type Client struct {
apiClient *ngrok.Client
}
func NewClient(apiClient *ngrok.Client) *Client {
return &Client{apiClient: apiClient}
}
// Create a new tunnel authtoken credential. This authtoken credential can be used
// to start a new tunnel session. The response to this API call is the only time
// the generated token is available. If you need it for future use, you must save
// it securely yourself.
func (c *Client) Create(
ctx context.Context,
arg *ngrok.CredentialCreate,
) (*ngrok.Credential, error) {
if arg == nil {
arg = new(ngrok.CredentialCreate)
}
var res ngrok.Credential
var path bytes.Buffer
if err := template.Must(template.New("create_path").Parse("/credentials")).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 a tunnel authtoken credential by ID
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("/credentials/{{ .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 a tunnel authtoken credential
func (c *Client) Get(
ctx context.Context,
id string,
) (*ngrok.Credential, error) {
arg := &ngrok.Item{ID: id}
var res ngrok.Credential
var path bytes.Buffer
if err := template.Must(template.New("get_path").Parse("/credentials/{{ .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 tunnel authtoken credentials on this account
func (c *Client) list(
ctx context.Context,
arg *ngrok.Paging,
) (*ngrok.CredentialList, error) {
if arg == nil {
arg = new(ngrok.Paging)
}
var res ngrok.CredentialList
var path bytes.Buffer
if err := template.Must(template.New("list_path").Parse("/credentials")).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 tunnel authtoken credentials on this account
func (c *Client) List(ctx context.Context, paging *ngrok.Paging) *Iter {
if paging == nil {
paging = new(ngrok.Paging)
}
if paging.Limit == nil {
paging.Limit = ngrok.String("100")
}
return &Iter{
client: c,
ctx: ctx,
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
ctx context.Context
n int
items []ngrok.Credential
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() bool {
// no more if there is an error
if it.err != nil {
return false
}
// are there items remaining?
if it.n < len(it.items)-1 {
it.n += 1
it.lastItemID = ngrok.String(it.Item().ID)
return true
}
// fetch the next page
resp, err := it.client.list(it.ctx, &ngrok.Paging{
BeforeID: it.lastItemID,
Limit: it.limit,
})
if err != nil {
it.err = err
return false
}
it.n = 0
it.items = resp.Credentials
return len(it.items) > 0
}
// Item() returns the Credential currently
// pointed to by the iterator.
func (it *Iter) Item() *ngrok.Credential {
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 attributes of an tunnel authtoken credential by ID
func (c *Client) Update(
ctx context.Context,
arg *ngrok.CredentialUpdate,
) (*ngrok.Credential, error) {
if arg == nil {
arg = new(ngrok.CredentialUpdate)
}
var res ngrok.Credential
var path bytes.Buffer
if err := template.Must(template.New("update_path").Parse("/credentials/{{ .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
}