forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwebhook_call.go
105 lines (86 loc) · 2.55 KB
/
webhook_call.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
package contentful
import (
"fmt"
"net/http"
"net/url"
)
// WebhookCallsService service
type WebhookCallsService service
// WebhookCall model
type WebhookCall struct {
Sys *Sys `json:"sys"`
Request Request `json:"request,omitempty"`
Response Response `json:"response,omitempty"`
StatusCode int `json:"statusCode"`
Errors []string `json:"errors"`
EventType string `json:"eventType"`
URL string `json:"url"`
RequestAt string `json:"requestAt"`
ResponseAt string `json:"responseAt"`
}
// Request model
type Request struct {
URL string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
// Response model
type Response struct {
URL string `json:"url"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
StatusCode int `json:"statusCode"`
}
// WebhookHealth model
type WebhookHealth struct {
Sys *Sys `json:"sys"`
Calls HealthDetails `json:"calls"`
}
// HealthDetails model
type HealthDetails struct {
Total int `json:"total"`
Healthy int `json:"healthy"`
}
// List returns a webhook calls collection
func (service *WebhookCallsService) List(spaceID, webhookID string) *Collection {
path := fmt.Sprintf("/spaces/%s/webhooks/%s/calls", spaceID, webhookID)
req, err := service.c.newRequest(http.MethodGet, path, nil, nil)
if err != nil {
return &Collection{}
}
col := NewCollection(&CollectionOptions{})
col.c = service.c
col.req = req
return col
}
// Get returns details of a single webhook call
func (service *WebhookCallsService) Get(spaceID, webhookID, callID string) (*WebhookCall, error) {
path := fmt.Sprintf("/spaces/%s/webhooks/%s/calls/%s", spaceID, webhookID, callID)
query := url.Values{}
method := "GET"
req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &WebhookCall{}, err
}
var webHook WebhookCall
if ok := service.c.do(req, &webHook); ok != nil {
return nil, err
}
return &webHook, err
}
// Health returns the health of a webhook
func (service *WebhookCallsService) Health(spaceID, webhookID string) (*WebhookHealth, error) {
path := fmt.Sprintf("/spaces/%s/webhooks/%s/health", spaceID, webhookID)
query := url.Values{}
method := "GET"
req, err := service.c.newRequest(method, path, query, nil)
if err != nil {
return &WebhookHealth{}, err
}
var health WebhookHealth
if ok := service.c.do(req, &health); ok != nil {
return nil, err
}
return &health, err
}