/
client.go
169 lines (143 loc) · 5.25 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
package client
import (
"context"
pb "github.com/textileio/textile/v2/api/hubd/pb"
"google.golang.org/grpc"
)
// Client provides the client api.
type Client struct {
c pb.APIServiceClient
conn *grpc.ClientConn
}
// NewClient starts the client.
func NewClient(target string, opts ...grpc.DialOption) (*Client, error) {
conn, err := grpc.Dial(target, opts...)
if err != nil {
return nil, err
}
return &Client{
c: pb.NewAPIServiceClient(conn),
conn: conn,
}, nil
}
// Close closes the client's grpc connection and cancels any active requests.
func (c *Client) Close() error {
return c.conn.Close()
}
// BuildInfo provides API build information.
func (c *Client) BuildInfo(ctx context.Context) (*pb.BuildInfoResponse, error) {
return c.c.BuildInfo(ctx, &pb.BuildInfoRequest{})
}
// Signup creates a new user and returns a session.
// This method will block and wait for email-based verification.
func (c *Client) Signup(ctx context.Context, username, email string) (*pb.SignupResponse, error) {
return c.c.Signup(ctx, &pb.SignupRequest{
Username: username,
Email: email,
})
}
// Signin returns a session for an existing username or email.
// This method will block and wait for email-based verification.
func (c *Client) Signin(ctx context.Context, usernameOrEmail string) (*pb.SigninResponse, error) {
return c.c.Signin(ctx, &pb.SigninRequest{
UsernameOrEmail: usernameOrEmail,
})
}
// Signout deletes a session.
func (c *Client) Signout(ctx context.Context) error {
_, err := c.c.Signout(ctx, &pb.SignoutRequest{})
return err
}
// GetSessionInfo returns session info.
func (c *Client) GetSessionInfo(ctx context.Context) (*pb.GetSessionInfoResponse, error) {
return c.c.GetSessionInfo(ctx, &pb.GetSessionInfoRequest{})
}
// GetIdentity returns the identity of the current session.
func (c *Client) GetIdentity(ctx context.Context) (*pb.GetIdentityResponse, error) {
return c.c.GetIdentity(ctx, &pb.GetIdentityRequest{})
}
// CreateKey creates a new key for the current session.
func (c *Client) CreateKey(ctx context.Context, keyType pb.KeyType, secure bool) (*pb.CreateKeyResponse, error) {
return c.c.CreateKey(ctx, &pb.CreateKeyRequest{
Type: keyType,
Secure: secure,
})
}
// InvalidateKey marks a key as invalid.
// New threads cannot be created with an invalid key.
func (c *Client) InvalidateKey(ctx context.Context, key string) error {
_, err := c.c.InvalidateKey(ctx, &pb.InvalidateKeyRequest{Key: key})
return err
}
// ListKeys returns a list of keys for the current session.
func (c *Client) ListKeys(ctx context.Context) (*pb.ListKeysResponse, error) {
return c.c.ListKeys(ctx, &pb.ListKeysRequest{})
}
// CreateOrg creates a new org by name.
func (c *Client) CreateOrg(ctx context.Context, name string) (*pb.CreateOrgResponse, error) {
return c.c.CreateOrg(ctx, &pb.CreateOrgRequest{Name: name})
}
// GetOrg returns an org.
func (c *Client) GetOrg(ctx context.Context) (*pb.GetOrgResponse, error) {
return c.c.GetOrg(ctx, &pb.GetOrgRequest{})
}
// ListOrgs returns a list of orgs for the current session.
func (c *Client) ListOrgs(ctx context.Context) (*pb.ListOrgsResponse, error) {
return c.c.ListOrgs(ctx, &pb.ListOrgsRequest{})
}
// RemoveOrg removes an org.
func (c *Client) RemoveOrg(ctx context.Context) error {
_, err := c.c.RemoveOrg(ctx, &pb.RemoveOrgRequest{})
return err
}
// InviteToOrg invites the given email to an org.
func (c *Client) InviteToOrg(ctx context.Context, email string) (*pb.InviteToOrgResponse, error) {
return c.c.InviteToOrg(ctx, &pb.InviteToOrgRequest{
Email: email,
})
}
// LeaveOrg removes the current session dev from an org.
func (c *Client) LeaveOrg(ctx context.Context) error {
_, err := c.c.LeaveOrg(ctx, &pb.LeaveOrgRequest{})
return err
}
// SetupBilling (re-)enables billing for an account, enabling
// usage beyond the free quotas.
func (c *Client) SetupBilling(ctx context.Context) error {
_, err := c.c.SetupBilling(ctx, &pb.SetupBillingRequest{})
return err
}
// GetBillingSession returns a billing portal session url.
func (c *Client) GetBillingSession(ctx context.Context) (*pb.GetBillingSessionResponse, error) {
return c.c.GetBillingSession(ctx, &pb.GetBillingSessionRequest{})
}
// ListBillingUsers returns a list of users the account is responsible for.
func (c *Client) ListBillingUsers(ctx context.Context, opts ...ListOption) (
*pb.ListBillingUsersResponse, error) {
args := &listOptions{}
for _, opt := range opts {
opt(args)
}
return c.c.ListBillingUsers(ctx, &pb.ListBillingUsersRequest{
Offset: args.offset,
Limit: args.limit,
})
}
// IsUsernameAvailable returns a nil error if the username is valid and available.
func (c *Client) IsUsernameAvailable(ctx context.Context, username string) error {
_, err := c.c.IsUsernameAvailable(ctx, &pb.IsUsernameAvailableRequest{
Username: username,
})
return err
}
// IsOrgNameAvailable returns a nil error if the name is valid and available.
func (c *Client) IsOrgNameAvailable(ctx context.Context, name string) (*pb.IsOrgNameAvailableResponse, error) {
return c.c.IsOrgNameAvailable(ctx, &pb.IsOrgNameAvailableRequest{
Name: name,
})
}
// DestroyAccount completely deletes an account and all associated data.
func (c *Client) DestroyAccount(ctx context.Context) error {
_, err := c.c.DestroyAccount(ctx, &pb.DestroyAccountRequest{})
return err
}