forked from SparkPost/gosparkpost
-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_docs.go
68 lines (57 loc) · 1.62 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
64
65
66
67
68
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
}
if err = res.AssertJson(); err != nil {
return
}
if Is2XX(res.HTTP.StatusCode) {
var body []byte
var ok bool
body, err = res.ReadBody()
if err != nil {
return
}
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
}