-
Notifications
You must be signed in to change notification settings - Fork 40
/
event_docs.go
63 lines (53 loc) · 1.56 KB
/
event_docs.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
package gosparkpost
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
var EventDocumentationFormat = "/api/v%d/webhooks/events/documentation"
type EventGroup struct {
Name string
Events map[string]EventMeta `json:"events"`
Description string `json:"description"`
DisplayName string `json:"display_name"`
}
type EventMeta struct {
Name string
Fields map[string]EventField `json:"event"`
Description string `json:"description"`
DisplayName string `json:"display_name"`
}
type EventField struct {
Description string `json:"description"`
SampleValue interface{} `json:"sampleValue"`
}
func (c *Client) EventDocumentation() (g map[string]*EventGroup, res *Response, err error) {
return c.EventDocumentationContext(context.Background())
}
func (c *Client) EventDocumentationContext(ctx context.Context) (groups map[string]*EventGroup, res *Response, err error) {
path := fmt.Sprintf(EventDocumentationFormat, c.Config.ApiVersion)
res, err = c.HttpGet(ctx, c.Config.BaseUrl+path)
if err != nil {
return
}
var body []byte
if body, err = res.AssertJson(); err != nil {
return
}
if Is2XX(res.HTTP.StatusCode) {
var ok bool
var results map[string]map[string]*EventGroup
if err = json.Unmarshal(body, &results); err != nil {
} else if groups, ok = results["results"]; ok {
// Success!
} else {
err = errors.New("Unexpected response format (results)")
}
} else {
if err = res.ParseResponse(); err == nil {
err = res.HTTPError()
}
}
return
}