-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
56 lines (48 loc) · 1.93 KB
/
token.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
package client
import (
"context"
"net/http"
"net/url"
"strconv"
"github.com/calyptia/api/types"
)
// CreateToken within a project.
// These tokens are to authorize other applications to access the project.
// For example:
// - an agent might use it to register itself to the project.
// - you might create a new core instance in the project using the Calyptia CLI.
// - you might use it within the Calyptia CLI to grant access to your project.
func (c *Client) CreateToken(ctx context.Context, projectID string, payload types.CreateToken) (types.Token, error) {
var out types.Token
return out, c.do(ctx, http.MethodPost, "/v1/projects/"+url.PathEscape(projectID)+"/tokens", payload, &out)
}
// Tokens from a project.
func (c *Client) Tokens(ctx context.Context, projectID string, params types.TokensParams) (types.Tokens, error) {
q := url.Values{}
if params.Last != nil {
q.Set("last", strconv.FormatUint(uint64(*params.Last), uintBase))
}
if params.Before != nil {
q.Set("before", *params.Before)
}
if params.Name != nil {
q.Set("name", *params.Name)
}
var out types.Tokens
path := "/v1/projects/" + url.PathEscape(projectID) + "/tokens?" + q.Encode()
return out, c.do(ctx, http.MethodGet, path, nil, &out.Items, withCursor(&out.EndCursor))
}
// Token by ID.
func (c *Client) Token(ctx context.Context, tokenID string) (types.Token, error) {
var out types.Token
return out, c.do(ctx, http.MethodGet, "/v1/project_tokens/"+url.PathEscape(tokenID), nil, &out)
}
// UpdateToken by its ID.
func (c *Client) UpdateToken(ctx context.Context, in types.UpdateToken) error {
return c.do(ctx, http.MethodPatch, "/v1/project_tokens/"+url.PathEscape(in.TokenID), in, nil)
}
// DeleteToken by its ID.
// Once deleted, any application that might has been using it, will stop working.
func (c *Client) DeleteToken(ctx context.Context, tokenID string) error {
return c.do(ctx, http.MethodDelete, "/v1/project_tokens/"+url.PathEscape(tokenID), nil, nil)
}