-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinvitations.go
More file actions
77 lines (63 loc) · 2.24 KB
/
invitations.go
File metadata and controls
77 lines (63 loc) · 2.24 KB
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
// Package invitations provides methods for managing workspace invitations.
package invitations
import (
"encoding/json"
"fmt"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// List lists pending invitations in a workspace.
func List(c *drycc.Client, workspace string, results int) (api.WorkspaceInvitations, int, error) {
u := fmt.Sprintf("/v2/workspaces/%s/invitations", workspace)
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.WorkspaceInvitation{}, -1, reqErr
}
var invitations []api.WorkspaceInvitation
if err := json.Unmarshal([]byte(body), &invitations); err != nil {
return []api.WorkspaceInvitation{}, -1, err
}
return invitations, count, reqErr
}
// Create creates a workspace invitation.
func Create(c *drycc.Client, workspace, email string) (api.WorkspaceInvitation, error) {
u := fmt.Sprintf("/v2/workspaces/%s/invitations", workspace)
req := api.WorkspaceInvitationCreateRequest{Email: email}
body, err := json.Marshal(req)
if err != nil {
return api.WorkspaceInvitation{}, err
}
res, reqErr := c.Request("POST", u, body)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.WorkspaceInvitation{}, reqErr
}
defer res.Body.Close()
invitation := api.WorkspaceInvitation{}
if err = json.NewDecoder(res.Body).Decode(&invitation); err != nil {
return api.WorkspaceInvitation{}, err
}
return invitation, reqErr
}
// Get fetches an invitation by uid token.
func Get(c *drycc.Client, workspace, uid string) (api.WorkspaceInvitation, error) {
u := fmt.Sprintf("/v2/workspaces/%s/invitations/%s", workspace, uid)
res, reqErr := c.Request("GET", u, nil)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return api.WorkspaceInvitation{}, reqErr
}
defer res.Body.Close()
invitation := api.WorkspaceInvitation{}
if err := json.NewDecoder(res.Body).Decode(&invitation); err != nil {
return api.WorkspaceInvitation{}, err
}
return invitation, reqErr
}
// Delete revokes an invitation.
func Delete(c *drycc.Client, workspace, uid string) error {
u := fmt.Sprintf("/v2/workspaces/%s/invitations/%s", workspace, uid)
res, err := c.Request("DELETE", u, nil)
if err == nil {
res.Body.Close()
}
return err
}