forked from SparkPost/gosparkpost
/
event_docs.go
78 lines (66 loc) · 1.94 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
69
70
71
72
73
74
75
76
77
78
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) (g 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 nil, nil, err
}
if err = res.AssertJson(); err != nil {
return nil, res, err
}
if res.HTTP.StatusCode == 200 {
var body []byte
var ok bool
body, err = res.ReadBody()
if err != nil {
return nil, res, err
}
var results map[string]map[string]*EventGroup
var groups map[string]*EventGroup
if err = json.Unmarshal(body, &results); err != nil {
return nil, res, err
} else if groups, ok = results["results"]; ok {
return groups, res, err
}
return nil, res, errors.New("Unexpected response format")
} else {
err = res.ParseResponse()
if err != nil {
return nil, res, err
}
if len(res.Errors) > 0 {
err = res.PrettyError("EventDocumentation", "retrieve")
if err != nil {
return nil, res, err
}
}
return nil, res, errors.Errorf("%d: %s", res.HTTP.StatusCode, string(res.Body))
}
return nil, res, err
}