-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaccount.go
130 lines (114 loc) · 4.2 KB
/
account.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
package nylas
import (
"context"
"fmt"
"net/http"
)
// BillingState constants, for more info see:
// https://docs.nylas.com/reference#aclient_idaccounts
const (
BillingStateCancelled = "cancelled"
BillingStatePaid = "paid"
BillingStateDeleted = "deleted"
)
// OrganizationUnit constants specify either "label" or "folder", depending on the provider capabilities.
const (
OrganizationUnitFolder = "folder"
OrganizationUnitLabel = "label"
)
// Account contains the details of an account which corresponds to an email
// address, mailbox, and optionally a calendar.
type Account struct {
ID string `json:"id"`
Object string `json:"object"`
AccountID string `json:"account_id"`
Name string `json:"name"`
EmailAddress string `json:"email_address"`
Provider string `json:"provider"`
OrganizationUnit string `json:"organization_unit"`
SyncState string `json:"sync_state"`
LinkedAt int `json:"linked_at"`
// Only populated after a call to ConnectAccount
AccessToken string `json:"access_token"`
BillingState string `json:"billing_state"`
}
// ManagementAccount contains the details of an account and is used when working
// with the Account Management endpoints.
// See: https://docs.nylas.com/reference#account-management
type ManagementAccount struct {
ID string `json:"id"`
AccountID string `json:"account_id"`
BillingState string `json:"billing_state"`
Email string `json:"email"`
Provider string `json:"provider"`
SyncState string `json:"sync_state"`
Trial bool `json:"trial"`
}
// Account returns the account information for the user the client is
// authenticated as.
// See: https://docs.nylas.com/reference#account
func (c *Client) Account(ctx context.Context) (Account, error) {
req, err := c.newUserRequest(ctx, http.MethodGet, "/account", nil)
if err != nil {
return Account{}, err
}
var resp Account
return resp, c.do(req, &resp)
}
// Accounts returns the account information for all accounts.
// See: https://docs.nylas.com/reference#aclient_idaccounts
func (c *Client) Accounts(ctx context.Context) ([]ManagementAccount, error) {
endpoint := fmt.Sprintf("/a/%s/accounts", c.clientID)
req, err := c.newAccountRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var resp []ManagementAccount
return resp, c.do(req, &resp)
}
// DeleteAccount deletes an account. Accounts deleted using this method are immediately unavailable.
// See: https://developer.nylas.com/docs/api/#delete/a/client_id/accounts/id
func (c *Client) DeleteAccount(ctx context.Context, id string) error {
endpoint := fmt.Sprintf("/a/%s/accounts/%s", c.clientID, id)
req, err := c.newAccountRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
return c.do(req, nil)
}
// CancelAccount cancels a paid account. Accounts that are cancelled instead of deleted, can be recovered within 3 days.
// See: https://developer.nylas.com/docs/api/#post/a/client_id/accounts/id/downgrade
func (c *Client) CancelAccount(ctx context.Context, id string) error {
endpoint := fmt.Sprintf("/a/%s/accounts/%s/downgrade", c.clientID, id)
req, err := c.newAccountRequest(ctx, http.MethodPost, endpoint, nil)
if err != nil {
return err
}
return c.do(req, nil)
}
// ReactivateAccount re-enables a cancelled account to make it activate again.
// See: https://docs.nylas.com/reference#re-activate-an-account.
func (c *Client) ReactivateAccount(ctx context.Context, id string) error {
endpoint := fmt.Sprintf("/a/%s/accounts/%s/upgrade", c.clientID, id)
req, err := c.newAccountRequest(ctx, http.MethodPost, endpoint, nil)
if err != nil {
return err
}
return c.do(req, nil)
}
// RevokeAccountTokens revokes all account tokens, optionally excluding one.
// See: https://docs.nylas.com/reference#revoke-all
func (c *Client) RevokeAccountTokens(ctx context.Context, id string, keepToken *string) error {
endpoint := fmt.Sprintf("/a/%s/accounts/%s/revoke-all", c.clientID, id)
var body map[string]interface{}
if keepToken != nil {
body = map[string]interface{}{
"keep_access_token": *keepToken,
}
}
req, err := c.newAccountRequest(ctx, http.MethodPost, endpoint, body)
if err != nil {
return err
}
return c.do(req, nil)
}