-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.go
More file actions
722 lines (600 loc) · 20 KB
/
Copy pathapi.go
File metadata and controls
722 lines (600 loc) · 20 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
package pyrus
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/subtle"
"encoding/hex"
"encoding/json"
"errors"
"io"
"mime"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"go.uber.org/zap"
)
const (
baseURL = "https://api.pyrus.com/v4"
userAgent = "Pyrus API golang client v0.0.1"
)
type Client struct {
baseURL string
login string
securityKey string
accessToken string
mu sync.RWMutex
logger Logger
httpClient *http.Client
eventBufferSize int
}
// IClient is the main interface. Provided to implement dummy implementations useful for testing.
type IClient interface {
Auth(login, securityKey string) (string, error)
Forms() (*FormsResponse, error)
Form(formID int) (*FormResponse, error)
Registry(formID int, req *RegistryRequest) (*FormRegisterResponse, error)
Task(taskID int) (*TaskResponse, error)
CreateTask(req *TaskRequest) (*TaskResponse, error)
CommentTask(taskID int, req *TaskCommentRequest) (*TaskResponse, error)
Announcement(announcementID int) (*AnnouncementResponse, error)
CreateAnnouncement(req *AnnouncementRequest) (*AnnouncementResponse, error)
CommentAnnouncement(announcementID int, req *AnnouncementCommentRequest) (*AnnouncementResponse, error)
UploadFile(name string, file io.Reader) (*UploadResponse, error)
DownloadFile(fileID int) (*DownloadResponse, error)
Catalogs() (*CatalogsResponse, error)
Catalog(catalogID int) (*CatalogResponse, error)
CreateCatalog(name string, headers []string, items []*CatalogItem) (*CatalogResponse, error)
SyncCatalog(catalogID int, apply bool, headers []string, items []*CatalogItem) (*SyncCatalogResponse, error)
Contacts(includeInactive bool) (*ContactsResponse, error)
Members() (*MembersResponse, error)
CreateMember(req *MemberRequest) (*Member, error)
UpdateMember(memberID int, req *MemberRequest) (*Member, error)
BlockMember(memberID int) (*Member, error)
Roles() (*RolesResponse, error)
CreateRole(name string, members []int) (*Role, error)
UpdateRole(roleID int, name string, add, remove []int, banned bool) (*Role, error)
Profile() (*ProfileResponse, error)
Lists() (*ListsResponses, error)
TaskList(listID, itemCount int, includeArchived bool) (*TaskListResponse, error)
Inbox(itemCount int) (*TaskListResponse, error)
RegisterCall(req *RegisterCallRequest) (*RegisterCallResponse, error)
AddCallDetails(callGUID string, req *AddCallDetailsRequest) error
RegisterCallEvent(callGUID string, eventType CallEventType, extension string) error
WebhookHandler() (http.HandlerFunc, <-chan Event)
}
// Option helps to create an option for Client.
type Option func(*Client)
// WithLogger allows to log errors with own logger.
func WithLogger(l Logger) Option {
return func(c *Client) {
c.logger = l
}
}
// WithZapLogger allows to pass ready *zap.Logger instance for error logging.
func WithZapLogger(l *zap.Logger) Option {
return func(c *Client) {
c.logger = &zapLogger{logger: l}
}
}
// WithHTTPClient allows to override http.DefaultClient and use your own.
func WithHTTPClient(hc *http.Client) Option {
return func(c *Client) {
c.httpClient = hc
}
}
// WithEventBufferSize allows to override default buffer size of Event chan used by Webhook engine.
func WithEventBufferSize(size int) Option {
return func(c *Client) {
c.eventBufferSize = size
}
}
func WithBaseURL(baseURL string) Option {
return func(c *Client) {
c.baseURL = baseURL
}
}
// NewClient returns an instance of Client.
func NewClient(login, securityKey string, opts ...Option) (*Client, error) {
c := &Client{
baseURL: baseURL,
login: login,
securityKey: securityKey,
logger: &noopLogger{},
httpClient: http.DefaultClient,
eventBufferSize: 100,
}
// Apply optional opts
for _, opt := range opts {
opt(c)
}
return c, nil
}
func (c *Client) getAndSetAccessToken() error {
accessToken, err := c.Auth(c.login, c.securityKey)
if err != nil {
return err
}
c.mu.Lock()
c.accessToken = accessToken
c.mu.Unlock()
return nil
}
func (c *Client) performRequest(method, path string, q *url.Values, reqBody, respBody interface{}) error {
auth := false
if path == "/auth" {
auth = true
}
u, err := url.Parse(c.baseURL + path)
if err != nil {
c.logger.Error("Error while parsing a URL!", err)
return err
}
if q != nil {
u.RawQuery = q.Encode()
}
multipartRequest := false
if _, ok := reqBody.(*fileRequest); ok {
multipartRequest = true
}
var (
req *http.Request
reqErr error
)
contentTypeHeader := "application/json"
if multipartRequest {
buf := bytes.NewBuffer(nil)
w := multipart.NewWriter(buf)
fw, err := w.CreateFormFile("file", reqBody.(*fileRequest).Filename)
if err != nil {
c.logger.Error("Error while creating a new form file!", err)
return err
}
if _, err := io.Copy(fw, reqBody.(*fileRequest).Reader); err != nil {
c.logger.Error("Error while writing a file!", err)
return err
}
if err := w.Close(); err != nil {
c.logger.Error("Error while trying to close multipart writer!", err)
return err
}
req, reqErr = http.NewRequest(method, u.String(), buf)
contentTypeHeader = w.FormDataContentType()
} else if reqBody != nil {
buf := bytes.NewBuffer(nil)
if err := json.NewEncoder(buf).Encode(reqBody); err != nil {
c.logger.Error("Error while encoding JSON!", err)
return err
}
req, reqErr = http.NewRequest(method, u.String(), buf)
} else {
req, reqErr = http.NewRequest(method, u.String(), nil)
}
if reqErr != nil {
c.logger.Error("Error while creating a request!", err)
return err
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", contentTypeHeader)
// It's wise to get first token without unnecessary request
c.mu.RLock()
ok := c.accessToken != "" || auth
c.mu.RUnlock()
if !ok {
if err := c.getAndSetAccessToken(); err != nil {
return err
}
}
c.mu.RLock()
if c.accessToken != "" && !auth {
req.Header.Set("Authorization", "Bearer "+c.accessToken)
}
c.mu.RUnlock()
resp, err := c.httpClient.Do(req)
if err != nil {
c.logger.Error("Error while doing a request!", err)
return err
}
defer resp.Body.Close() //nolint:errcheck
// Get new access_token in case of old session
if resp.StatusCode == 401 && !auth {
if err := c.getAndSetAccessToken(); err != nil {
return err
}
return c.performRequest(method, path, q, reqBody, respBody)
}
// Don't read if there is no need in response body at all
if respBody == nil && !auth {
return nil
}
// File downloading
if mt, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type")); err == nil && mt != "application/json" {
mt, params, err := mime.ParseMediaType(resp.Header.Get("Content-Disposition"))
if err != nil {
c.logger.Error("Error while parsing media type!", err)
return err
}
if mt != "attachment" {
return errors.New("attachment was expected")
}
filename, ok := params["filename"]
if !ok {
return errors.New("file doesn't have a name")
}
if _, ok := respBody.(*string); ok {
*respBody.(*string) = filename
}
w, ok := reqBody.(io.Writer)
if !ok {
return errors.New("writer was expected")
}
if _, err := io.Copy(w, resp.Body); err != nil {
c.logger.Error("Error while trying to download file!", err)
return err
}
return nil
}
decoder := json.NewDecoder(resp.Body)
if resp.StatusCode != 200 {
var pe Error
if err := decoder.Decode(&pe); err != nil {
c.logger.Error("Error while decoding a response body!", err)
return err
}
return pe
}
if err := decoder.Decode(&respBody); err != nil {
c.logger.Error("Error while decoding a response body!", err)
return err
}
return nil
}
// Auth performs authorization and returns access_token.
func (c *Client) Auth(login, securityKey string) (string, error) {
var respBody AuthResponse
if err := c.performRequest(http.MethodPost, "/auth", nil, &authRequest{
Login: login,
SecurityKey: securityKey,
}, &respBody); err != nil {
return "", err
}
return respBody.AccessToken, nil
}
// Forms returns a description of all the forms in which the current user is a manager or a member.
func (c *Client) Forms() (*FormsResponse, error) {
var forms FormsResponse
if err := c.performRequest(http.MethodGet, "/forms", nil, nil, &forms); err != nil {
return nil, err
}
return &forms, nil
}
// Form returns a description of form with inputted id.
func (c *Client) Form(formID int) (*FormResponse, error) {
var form FormResponse
if err := c.performRequest(http.MethodGet, "/forms/"+strconv.Itoa(formID), nil, nil, &form); err != nil {
return nil, err
}
return &form, nil
}
// Registry returns the list of tasks that were created based on the specified form.
// The response only contains general information about the task, like the list of filled form fields and its workflow.
// You can use Task method to get all task comments.
func (c *Client) Registry(formID int, req *RegistryRequest) (*FormRegisterResponse, error) {
var tasks FormRegisterResponse
if err := c.performRequest(http.MethodPost, "/forms/"+strconv.Itoa(formID)+"/register", nil, req, &tasks); err != nil {
return nil, err
}
return &tasks, nil
}
// Task returns a task with all comments.
func (c *Client) Task(taskID int) (*TaskResponse, error) {
var task TaskResponse
if err := c.performRequest(http.MethodGet, "/tasks/"+strconv.Itoa(taskID), nil, nil, &task); err != nil {
return nil, err
}
return &task, nil
}
// CreateTask creates a task and returns it with a comment.
func (c *Client) CreateTask(req *TaskRequest) (*TaskResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
var task TaskResponse
if err := c.performRequest(http.MethodPost, "/tasks", nil, req, &task); err != nil {
return nil, err
}
return &task, nil
}
// CommentTask comments a task and returns it with all comments, including the added one.
func (c *Client) CommentTask(taskID int, req *TaskCommentRequest) (*TaskResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
var task TaskResponse
if err := c.performRequest(http.MethodPost, "/tasks/"+strconv.Itoa(taskID)+"/comments", nil, req, &task); err != nil {
return nil, err
}
return &task, nil
}
// Announcement returns an announcement with all comments.
func (c *Client) Announcement(announcementID int) (*AnnouncementResponse, error) {
var announcement AnnouncementResponse
if err := c.performRequest(http.MethodGet, "/announcements/"+strconv.Itoa(announcementID), nil, nil, &announcement); err != nil {
return nil, err
}
return &announcement, nil
}
// CreateAnnouncement creates an announcement and returns it with a comment.
func (c *Client) CreateAnnouncement(req *AnnouncementRequest) (*AnnouncementResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
var announcement AnnouncementResponse
if err := c.performRequest(http.MethodPost, "/announcements", nil, req, &announcement); err != nil {
return nil, err
}
return &announcement, nil
}
// CommentAnnouncement comments a task and returns it with all comments, including the added one.
func (c *Client) CommentAnnouncement(announcementID int, req *AnnouncementCommentRequest) (*AnnouncementResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
var announcement AnnouncementResponse
if err := c.performRequest(http.MethodPost, "/announcements/"+strconv.Itoa(announcementID)+"/comments", nil, req, &announcement); err != nil {
return nil, err
}
return &announcement, nil
}
// UploadFile uploads files for subsequent attachment to tasks.
// Files that are not referenced by any task are removed after a while.
func (c *Client) UploadFile(name string, file io.Reader) (*UploadResponse, error) {
var upload UploadResponse
if err := c.performRequest(http.MethodPost, "/files/upload", nil, &fileRequest{
Filename: name,
Reader: file,
}, &upload); err != nil {
return nil, err
}
return &upload, nil
}
// DownloadFile downloads file from Pyrus.
func (c *Client) DownloadFile(fileID int) (*DownloadResponse, error) {
buf := bytes.NewBuffer(nil)
var filename string
if err := c.performRequest(http.MethodGet, "/files/download/"+strconv.Itoa(fileID), nil, buf, &filename); err != nil {
return nil, err
}
return &DownloadResponse{
Filename: filename,
RawFile: buf.Bytes(),
}, nil
}
// Catalogs returns a list of available catalogs.
func (c *Client) Catalogs() (*CatalogsResponse, error) {
var catalogs CatalogsResponse
if err := c.performRequest(http.MethodGet, "/catalogs", nil, nil, &catalogs); err != nil {
return nil, err
}
return &catalogs, nil
}
// Catalog returns a catalog with all its elements.
func (c *Client) Catalog(catalogID int) (*CatalogResponse, error) {
var catalog CatalogResponse
if err := c.performRequest(http.MethodGet, "/catalogs/"+strconv.Itoa(catalogID), nil, nil, &catalog); err != nil {
return nil, err
}
return &catalog, nil
}
// CreateCatalog creates a catalog and returns it with all its elements.
func (c *Client) CreateCatalog(name string, headers []string, items []*CatalogItem) (*CatalogResponse, error) {
var catalog CatalogResponse
if err := c.performRequest(http.MethodPut, "/catalogs", nil, &catalogRequest{
Name: name,
CatalogHeaders: headers,
Items: items,
}, &catalog); err != nil {
return nil, err
}
return &catalog, nil
}
// SyncCatalog updates catalog header and items and returns a list of items that have been added, modified, or deleted.
func (c *Client) SyncCatalog(catalogID int, apply bool, headers []string, items []*CatalogItem) (*SyncCatalogResponse, error) {
var syncCatalog SyncCatalogResponse
if err := c.performRequest(http.MethodPost, "/catalogs/"+strconv.Itoa(catalogID), nil, &syncCatalogRequest{
Apply: apply,
CatalogHeaders: headers,
Items: items,
}, &syncCatalog); err != nil {
return nil, err
}
return &syncCatalog, nil
}
// Contacts returns a list of contacts available to the current user and grouped by organization.
func (c *Client) Contacts(includeInactive bool) (*ContactsResponse, error) {
q := &url.Values{}
if includeInactive {
q.Set("include_inactive", "y")
}
var contacts ContactsResponse
if err := c.performRequest(http.MethodGet, "/contacts", q, nil, &contacts); err != nil {
return nil, err
}
return &contacts, nil
}
// Members returns a list of all organization participants.
func (c *Client) Members() (*MembersResponse, error) {
var members MembersResponse
if err := c.performRequest(http.MethodGet, "/members", nil, nil, &members); err != nil {
return nil, err
}
return &members, nil
}
// CreateMember creates a user and returns it.
func (c *Client) CreateMember(req *MemberRequest) (*Member, error) {
var member Member
if err := c.performRequest(http.MethodPost, "/members", nil, req, &member); err != nil {
return nil, err
}
return &member, nil
}
// UpdateMember updates a user and returns it.
func (c *Client) UpdateMember(memberID int, req *MemberRequest) (*Member, error) {
var member Member
if err := c.performRequest(http.MethodPut, "/members/"+strconv.Itoa(memberID), nil, req, &member); err != nil {
return nil, err
}
return &member, nil
}
// BlockMember blocks a user and returns it.
func (c *Client) BlockMember(memberID int) (*Member, error) {
var member Member
if err := c.performRequest(http.MethodDelete, "/members/"+strconv.Itoa(memberID), nil, nil, &member); err != nil {
return nil, err
}
return &member, nil
}
// Roles returns a list of roles.
func (c *Client) Roles() (*RolesResponse, error) {
var roles RolesResponse
if err := c.performRequest(http.MethodGet, "/roles", nil, nil, &roles); err != nil {
return nil, err
}
return &roles, nil
}
// CreateRole creates a role and returns it.
func (c *Client) CreateRole(name string, members []int) (*Role, error) {
var role Role
if err := c.performRequest(http.MethodPost, "/roles", nil, &roleRequest{
Name: name,
MemberAdd: members,
}, &role); err != nil {
return nil, err
}
return &role, nil
}
// UpdateRole updates a role and returns it.
func (c *Client) UpdateRole(roleID int, name string, add, remove []int, banned bool) (*Role, error) {
var role Role
if err := c.performRequest(http.MethodPut, "/roles/"+strconv.Itoa(roleID), nil, &roleUpdateRequest{
Name: name,
MemberAdd: add,
MemberRemove: remove,
Banned: banned,
}, &role); err != nil {
return nil, err
}
return &role, nil
}
// Profile returns a profile of the calling user.
func (c *Client) Profile() (*ProfileResponse, error) {
var profile ProfileResponse
if err := c.performRequest(http.MethodGet, "/profile", nil, nil, &profile); err != nil {
return nil, err
}
return &profile, nil
}
// Lists returns all the lists that are available to the user.
func (c *Client) Lists() (*ListsResponses, error) {
var lists ListsResponses
if err := c.performRequest(http.MethodGet, "/lists", nil, nil, &lists); err != nil {
return nil, err
}
return &lists, nil
}
// TaskList returns all the tasks in the specified list.
func (c *Client) TaskList(listID, itemCount int, includeArchived bool) (*TaskListResponse, error) {
q := &url.Values{}
if itemCount != 0 {
q.Set("item_count", strconv.Itoa(itemCount))
}
if includeArchived {
q.Set("include_archived", "y")
}
var taskList TaskListResponse
if err := c.performRequest(http.MethodGet, "/lists/"+strconv.Itoa(listID)+"/tasks", q, nil, &taskList); err != nil {
return nil, err
}
return &taskList, nil
}
// Inbox returns all inbox tasks.
func (c *Client) Inbox(itemCount int) (*TaskListResponse, error) {
q := &url.Values{}
if itemCount != 0 {
q.Set("item_count", strconv.Itoa(itemCount))
}
var taskList TaskListResponse
if err := c.performRequest(http.MethodGet, "/inbox", q, nil, &taskList); err != nil {
return nil, err
}
return &taskList, nil
}
// RegisterCall returns the GUID of the incoming call, and the id of the generated request.
func (c *Client) RegisterCall(req *RegisterCallRequest) (*RegisterCallResponse, error) {
if err := req.Validate(); err != nil {
return nil, err
}
var call RegisterCallResponse
if err := c.performRequest(http.MethodPost, "/calls", nil, req, &call); err != nil {
return nil, err
}
return &call, nil
}
// AddCallDetails adds call details by call_guid.
func (c *Client) AddCallDetails(callGUID string, req *AddCallDetailsRequest) error {
if err := c.performRequest(http.MethodPut, "/calls/"+callGUID, nil, req, nil); err != nil {
return err
}
return nil
}
// RegisterCallEvent registers call event by call_guid.
func (c *Client) RegisterCallEvent(callGUID string, eventType CallEventType, extension string) error {
if err := c.performRequest(http.MethodPost, "/calls/"+callGUID+"/event", nil, ®isterCallEventRequest{
EventType: eventType,
Extension: extension,
}, nil); err != nil {
return err
}
return nil
}
// WebhookHandler returns HTTP handler and channel with Event's.
// Handler automatically checks X-Pyrus-Sig, parses Event and sends it over channel..
func (c *Client) WebhookHandler() (http.HandlerFunc, <-chan Event) {
eventChan := make(chan Event, c.eventBufferSize)
writeError := func(w http.ResponseWriter, code int, err error) {
respBody, _ := json.Marshal(map[string]string{"error": err.Error()})
w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(respBody); err != nil {
c.logger.Error("Error while writing a response!", err)
}
}
return func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
c.logger.Error("Error while reading a request body!", err)
writeError(w, http.StatusInternalServerError, err)
return
}
hasher := hmac.New(sha1.New, []byte(c.securityKey))
hasher.Write(b)
hash := hex.EncodeToString(hasher.Sum(nil))
if subtle.ConstantTimeCompare([]byte(hash), []byte(strings.ToLower(r.Header.Get("X-Pyrus-Sig")))) != 1 {
err := errors.New("invalid signature")
c.logger.Error("Invalid signature!", err)
writeError(w, http.StatusUnauthorized, err)
return
}
var event Event
if err := json.Unmarshal(b, &event); err != nil {
c.logger.Error("Error while decoding a request body!", err)
writeError(w, http.StatusBadRequest, err)
return
}
eventChan <- event
w.WriteHeader(http.StatusOK)
}, eventChan
}