-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
73 lines (67 loc) · 1.97 KB
/
users.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
package hop
import (
"context"
"net/url"
"go.hop.io/sdk/types"
)
// Get is used to get the current user.
func (c ClientCategoryUsersMe) Get(ctx context.Context, opts ...ClientOption) (*types.UserMeInfo, error) {
if c.c.getTokenType() == "ptk" {
return nil, types.InvalidToken("cannot get user with project token")
}
var u types.UserMeInfo
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/users/@me",
Result: &u,
}, opts)
if err != nil {
return nil, err
}
return &u, nil
}
// CreatePat is used to create a personal access token for the current user.
func (c ClientCategoryUsersMe) CreatePat(ctx context.Context, name string, opts ...ClientOption) (*types.UserPat, error) {
if c.c.getTokenType() == "ptk" {
return nil, types.InvalidToken("cannot create users tokens with project token")
}
var pat types.UserPat
err := c.c.do(ctx, ClientArgs{
Method: "POST",
Path: "/users/@me/pats",
ResultKey: "pat",
Body: map[string]string{"name": name},
Result: &pat,
}, opts)
if err != nil {
return nil, err
}
return &pat, nil
}
// GetAllPats is used to get all personal access tokens for the current user.
func (c ClientCategoryUsersMe) GetAllPats(ctx context.Context, opts ...ClientOption) ([]*types.UserPat, error) {
if c.c.getTokenType() == "ptk" {
return nil, types.InvalidToken("cannot get users tokens with project token")
}
var pats []*types.UserPat
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/users/@me/pats",
ResultKey: "pats",
Result: &pats,
}, opts)
if err != nil {
return nil, err
}
return pats, nil
}
// DeletePat is used to delete a personal access token for the current user.
func (c ClientCategoryUsersMe) DeletePat(ctx context.Context, id string, opts ...ClientOption) error {
if c.c.getTokenType() == "ptk" {
return types.InvalidToken("cannot delete users tokens with project token")
}
return c.c.do(ctx, ClientArgs{
Method: "DELETE",
Path: "/users/@me/pats/" + url.PathEscape(id),
}, opts)
}