/
webhooks.go
120 lines (98 loc) · 3.44 KB
/
webhooks.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
package pipedrive
import (
"context"
"fmt"
"net/http"
"time"
)
// WebhooksService handles webhooks related
// methods of the Pipedrive API.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Webhooks
type WebhooksService service
// Webhook represents a Pipedrive webhook.
type Webhook struct {
ID int `json:"id"`
CompanyID int `json:"company_id"`
OwnerID int `json:"owner_id"`
UserID int `json:"user_id"`
EventAction string `json:"event_action"`
EventObject string `json:"event_object"`
SubscriptionURL string `json:"subscription_url"`
IsActive int `json:"is_active"`
AddTime time.Time `json:"add_time"`
RemoveTime interface{} `json:"remove_time"`
Type string `json:"type"`
HTTPAuthUser interface{} `json:"http_auth_user"`
HTTPAuthPassword interface{} `json:"http_auth_password"`
AdditionalData struct{} `json:"additional_data"`
LastDeliveryTime time.Time `json:"last_delivery_time"`
LastHTTPStatus int `json:"last_http_status"`
AdminID int `json:"admin_id"`
}
func (w Webhook) String() string {
return Stringify(w)
}
// WebhooksResponse represents multiple webhooks response.
type WebhooksResponse struct {
Status string `json:"status,omitempty"`
Success bool `json:"success,omitempty"`
Data []Webhook `json:"data,omitempty"`
}
// WebhookResponse represents single webhook response.
type WebhookResponse struct {
Status string `json:"status,omitempty"`
Success bool `json:"success,omitempty"`
Data Webhook `json:"data,omitempty"`
}
// List all webhooks.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Webhooks/get_webhooks
func (s *WebhooksService) List(ctx context.Context) (*WebhooksResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "/webhooks", nil, nil)
if err != nil {
return nil, nil, err
}
var record *WebhooksResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// WebhooksCreateOptions specifices the optional parameters to the
// WebhooksService.Create method.
type WebhooksCreateOptions struct {
SubscriptionURL string `url:"subscription_url"`
EventAction EventAction `url:"event_action"`
DealProbability EventObject `url:"event_object"`
UserID uint `url:"user_id"`
HTTPAuthUser string `url:"http_auth_user"`
HTTPAuthPassword string `url:"http_auth_password"`
}
// Create a webhook.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Webhooks/post_webhooks
func (s *WebhooksService) Create(ctx context.Context, opt *WebhooksCreateOptions) (*WebhookResponse, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "/webhooks", nil, opt)
if err != nil {
return nil, nil, err
}
var record *WebhookResponse
resp, err := s.client.Do(ctx, req, &record)
if err != nil {
return nil, resp, err
}
return record, resp, nil
}
// Delete a webhook.
//
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Webhooks/delete_webhooks_id
func (s *WebhooksService) Delete(ctx context.Context, id int) (*Response, error) {
uri := fmt.Sprintf("/webhooks/%v", id)
req, err := s.client.NewRequest(http.MethodDelete, uri, nil, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}