forked from Teamwork/nylas-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
191 lines (164 loc) · 4.33 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package nylas
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const apiURL = "https://api.nylas.com"
// View constants for more info, see:
// https://docs.nylas.com/reference#views
const (
ViewCount = "count"
ViewExpanded = "expanded"
ViewIDs = "ids"
)
// Label/Folder mailbox name constants, for more info see:
// https://docs.nylas.com/reference#get-labels
// https://docs.nylas.com/reference#get-folders
// https://tools.ietf.org/html/rfc6154
const (
MailboxInbox = "inbox"
MailboxAll = "all"
MailboxTrash = "trash"
MailboxArchive = "archive"
MailboxDrafts = "drafts"
MailboxSent = "sent"
MailboxSpam = "spam"
MailboxImportant = "important"
)
// ErrAccessTokenNotSet is returned when Client methods are called that require
// an access token to be set.
var ErrAccessTokenNotSet = errors.New("access token not set on client")
// Client for working with the Nylas API.
type Client struct {
clientID, clientSecret string
accessToken string
baseURL string
c *http.Client
errorHandler func(e error) error
}
// Option sets an optional setting on the Client.
type Option func(*Client)
// NewClient returns a new client for working with the Nylas API.
func NewClient(clientID, clientSecret string, opts ...Option) *Client {
client := &Client{
clientID: clientID,
clientSecret: clientSecret,
baseURL: apiURL,
c: http.DefaultClient,
}
for _, opt := range opts {
opt(client)
}
return client
}
// WithHTTPClient returns an Option to set the http.Client to be used.
func WithHTTPClient(httpClient *http.Client) Option {
return func(c *Client) {
c.c = httpClient
}
}
// WithBaseURL returns an Option to set the base URL to be used.
func WithBaseURL(baseURL string) Option {
return func(c *Client) {
c.baseURL = baseURL
}
}
// WithErrorHandler returns an Option to set the error handler to be used.
func WithErrorHandler(f func(e error) error) Option {
return func(c *Client) {
c.errorHandler = f
}
}
// WithAccessToken returns an option to set the access token to be used.
// This token is used for user mailbox specific methods.
func WithAccessToken(token string) Option {
return func(c *Client) {
c.accessToken = token
}
}
// As returns a copy of the Client with the given access token set.
func (c *Client) As(accessToken string) *Client {
as := *c
WithAccessToken(accessToken)(&as)
return &as
}
func (c *Client) newUserRequest(
ctx context.Context, method, endpoint string, body interface{},
) (*http.Request, error) {
if c.accessToken == "" {
return nil, ErrAccessTokenNotSet
}
req, err := c.newRequest(ctx, method, endpoint, body)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.accessToken, "")
return req, nil
}
func (c *Client) newAccountRequest(
ctx context.Context, method, endpoint string, body interface{},
) (*http.Request, error) {
req, err := c.newRequest(ctx, method, endpoint, body)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.clientSecret, "")
return req, nil
}
func (c *Client) newRequest(
ctx context.Context, method, endpoint string, body interface{},
) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+endpoint, nil)
if err != nil {
return nil, err
}
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal body: %w", err)
}
req.Body = ioutil.NopCloser(bytes.NewBuffer(data))
req.Header.Add("Content-Type", "application/json; charset=utf")
}
return req, nil
}
func (c *Client) do(req *http.Request, v interface{}) error {
resp, err := c.c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() // nolint: errcheck
if resp.StatusCode >= 299 {
e := NewError(resp)
if c.errorHandler != nil {
return c.errorHandler(e)
}
return e
}
if v != nil {
return json.NewDecoder(resp.Body).Decode(v)
}
return nil
}
func appendQueryValues(req *http.Request, values url.Values) {
q := req.URL.Query()
for k, vs := range values {
for _, v := range vs {
q.Add(k, v)
}
}
req.URL.RawQuery = q.Encode()
}
type countResponse struct {
Count int `json:"count"`
}
// Bool returns a pointer to the given bool value.
func Bool(v bool) *bool { return &v }
// String returns a pointer to the given string value.
func String(v string) *string { return &v }