/
pipedrive.go
361 lines (283 loc) · 8.08 KB
/
pipedrive.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package pipedrive
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
"time"
"github.com/google/go-querystring/query"
)
const (
defaultBaseUrl = "api.pipedrive.com/"
libraryVersion = "1"
hostProtocol = "https"
// The amount of requests current API token can perform for the 10 seconds window.
headerRateLimit = "X-RateLimit-Limit"
// The amount of requests left for the 10 seconds window.
headerRateRemaining = "X-RateLimit-Remaining"
// The amount of seconds before the limit resets.
headerRateReset = "X-RateLimit-Reset"
)
type Client struct {
// HTTP client used to communicate with the API.
client *http.Client
// Base URL for API requests. Defaults to the public Pipedrive API, but can be
// set to a domain endpoint to use. BaseURL should
// always be specified with a trailing slash.
BaseURL *url.URL
apiKey string
rateMutex sync.Mutex
currentRate Rate
// Reuse a single struct instead of allocating one for each service.
common service
Deals *DealService
Currencies *CurrenciesService
NoteFields *NoteFieldsService
Notes *NotesService
Recents *RecentsService
SearchResults *SearchResultsService
Users *UsersService
Filters *FiltersService
Activities *ActivitiesService
ActivityFields *ActivityFieldsService
ActivityTypes *ActivityTypesService
Authorizations *AuthorizationsService
Stages *StagesService
Webhooks *WebhooksService
UserConnections *UserConnectionsService
GoalsService *GoalsService
PipelinesService *PipelinesService
UserSettings *UserSettingsService
Files *FilesService
ProductFields *ProductFieldsService
Products *ProductsService
PersonFields *PersonFieldsService
OrganizationField *OrganizationFieldsService
DealFields *DealFieldsService
Persons *PersonsService
Organizations *OrganizationsService
}
type service struct {
client *Client
}
type Config struct {
APIKey string
CompanyDomain string
}
type Rate struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset Timestamp `json:"reset"`
}
func (r Rate) String() string {
return Stringify(r)
}
type Response struct {
*http.Response
Rate
}
// Parse the rate from response headers.
func parseRateFromResponse(r *http.Response) Rate {
var rate Rate
if limit := r.Header.Get(headerRateLimit); limit != "" {
rate.Limit, _ = strconv.Atoi(limit)
}
if remaining := r.Header.Get(headerRateRemaining); remaining != "" {
rate.Remaining, _ = strconv.Atoi(remaining)
}
if reset := r.Header.Get(headerRateReset); reset != "" {
if value, _ := strconv.ParseInt(reset, 10, 64); value != 0 {
rate.Reset = Timestamp{time.Unix(value, 0)}
}
}
return rate
}
func (c *Client) NewRequest(method, url string, opt interface{}, body interface{}) (*http.Request, error) {
if !strings.HasSuffix(c.BaseURL.Path, "/") {
return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", c.BaseURL)
}
u, err := c.createRequestUrl(url, opt)
if err != nil {
return nil, err
}
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(body)
if err != nil {
return nil, err
}
}
request, err := http.NewRequest(method, u, buf)
if err != nil {
return nil, err
}
if body != nil {
request.Header.Set("Content-Type", "application/json")
}
return request, nil
}
func (c *Client) checkRateLimitBeforeDo(req *http.Request) *RateLimitError {
c.rateMutex.Lock()
rate := c.currentRate
c.rateMutex.Unlock()
if !rate.Reset.Time.IsZero() && rate.Remaining == 0 {
resp := &http.Response{
Status: http.StatusText(http.StatusForbidden),
StatusCode: http.StatusForbidden,
Request: req,
Header: make(http.Header, 0),
Body: ioutil.NopCloser(bytes.NewBufferString("")),
}
return &RateLimitError{
Rate: rate,
Response: resp,
Message: fmt.Sprintf("API rate limit of %v exceeded.", rate.Limit),
}
}
return nil
}
func (c *Client) checkResponse(r *http.Response) error {
if code := r.StatusCode; 200 <= code && code <= 299 {
return nil
}
data, err := ioutil.ReadAll(r.Body)
errorResponse := &ErrorResponse{Response: r}
if err == nil && data != nil {
json.Unmarshal(data, errorResponse)
}
switch {
case r.StatusCode == http.StatusForbidden && r.Header.Get(headerRateRemaining) == "0":
return &RateLimitError{
Rate: parseRateFromResponse(r),
Response: errorResponse.Response,
}
default:
return errorResponse
}
}
// Do sends an API request and returns the API response. T
//
// The provided ctx must be non-nil. If it is canceled or times out,
// ctx.Err() will be returned.
func (c *Client) Do(ctx context.Context, request *http.Request, v interface{}) (*Response, error) {
if err := c.checkRateLimitBeforeDo(request); err != nil {
return &Response{
Response: err.Response,
}, err
}
resp, err := c.client.Do(request)
if err != nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return nil, err
}
defer func() {
io.CopyN(ioutil.Discard, resp.Body, 512)
resp.Body.Close()
}()
response := newResponse(resp)
c.rateMutex.Lock()
c.currentRate = response.Rate
c.rateMutex.Unlock()
err = c.checkResponse(response.Response)
if err != nil {
return response, err
}
err = json.NewDecoder(resp.Body).Decode(v)
if err == io.EOF {
return response, nil
}
return response, nil
}
func (c *Client) createRequestUrl(path string, opt interface{}) (string, error) {
uri, err := c.BaseURL.Parse(hostProtocol + "://" + defaultBaseUrl + "v" + libraryVersion)
if err != nil {
return path, err
}
uri.Path += path
v := reflect.ValueOf(opt)
if v.Kind() == reflect.Ptr && v.IsNil() {
parameters := url.Values{}
parameters.Add("api_token", c.apiKey)
uri.RawQuery = parameters.Encode()
return uri.String(), nil
}
qs, err := query.Values(opt)
if err != nil {
return path, err
}
qs.Add("api_token", c.apiKey)
uri.RawQuery = qs.Encode()
return uri.String(), nil
}
func (c *Client) SetOptions(options ...func(*Client) error) error {
for _, opt := range options {
if err := opt(c); err != nil {
return err
}
}
return nil
}
func NewClient(options *Config) *Client {
baseURL, _ := url.Parse(defaultBaseUrl)
c := &Client{
client: http.DefaultClient,
BaseURL: baseURL,
apiKey: options.APIKey,
}
c.common.client = c
c.Deals = (*DealService)(&c.common)
c.Currencies = (*CurrenciesService)(&c.common)
c.NoteFields = (*NoteFieldsService)(&c.common)
c.Notes = (*NotesService)(&c.common)
c.Recents = (*RecentsService)(&c.common)
c.SearchResults = (*SearchResultsService)(&c.common)
c.Users = (*UsersService)(&c.common)
c.Filters = (*FiltersService)(&c.common)
c.Activities = (*ActivitiesService)(&c.common)
c.ActivityFields = (*ActivityFieldsService)(&c.common)
c.ActivityTypes = (*ActivityTypesService)(&c.common)
c.Authorizations = (*AuthorizationsService)(&c.common)
c.Stages = (*StagesService)(&c.common)
c.Webhooks = (*WebhooksService)(&c.common)
c.UserConnections = (*UserConnectionsService)(&c.common)
c.GoalsService = (*GoalsService)(&c.common)
c.PipelinesService = (*PipelinesService)(&c.common)
c.UserSettings = (*UserSettingsService)(&c.common)
c.Files = (*FilesService)(&c.common)
c.ProductFields = (*ProductFieldsService)(&c.common)
c.Products = (*ProductsService)(&c.common)
c.PersonFields = (*PersonFieldsService)(&c.common)
c.OrganizationField = (*OrganizationFieldsService)(&c.common)
c.DealFields = (*DealFieldsService)(&c.common)
c.Persons = (*PersonsService)(&c.common)
c.Organizations = (*OrganizationsService)(&c.common)
return c
}
func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
response.Rate = parseRateFromResponse(r)
return response
}
// Converts []int to string by separator.
func arrayToString(a []int, sep string) string {
b := make([]string, len(a))
for i, v := range a {
b[i] = strconv.Itoa(v)
}
return strings.Join(b, sep)
}