| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"io/ioutil" |
| 9 |
"net/http" |
| 10 |
"reflect" |
| 11 |
"strconv" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/google/jsonapi" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
type Event struct { |
| 19 |
ID string `jsonapi:"primary,event"` |
| 20 |
CustomerID string `jsonapi:"attr,customer_id"` |
| 21 |
Description string `jsonapi:"attr,description"` |
| 22 |
EventType string `jsonapi:"attr,event_type"` |
| 23 |
IP string `jsonapi:"attr,ip"` |
| 24 |
Metadata map[string]interface{} `jsonapi:"attr,metadata,omitempty"` |
| 25 |
ServiceID string `jsonapi:"attr,service_id"` |
| 26 |
UserID string `jsonapi:"attr,user_id"` |
| 27 |
CreatedAt *time.Time `jsonapi:"attr,created_at,iso8601"` |
| 28 |
Admin bool `jsonapi:"attr,admin"` |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
type GetAPIEventsFilterInput struct { |
| 33 |
|
| 34 |
CustomerID string |
| 35 |
|
| 36 |
|
| 37 |
ServiceID string |
| 38 |
|
| 39 |
|
| 40 |
EventType string |
| 41 |
|
| 42 |
|
| 43 |
UserID string |
| 44 |
|
| 45 |
|
| 46 |
PageNumber int |
| 47 |
|
| 48 |
|
| 49 |
MaxResults int |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
type EventsPaginationInfo struct { |
| 62 |
First string `json:"first,omitempty"` |
| 63 |
Last string `json:"last,omitempty"` |
| 64 |
Next string `json:"next,omitempty"` |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
type GetAPIEventsResponse struct { |
| 69 |
Events []*Event |
| 70 |
Links EventsPaginationInfo `json:"links"` |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (c *Client) GetAPIEvents(i *GetAPIEventsFilterInput) (GetAPIEventsResponse, error) { |
| 75 |
eventsResponse := GetAPIEventsResponse{ |
| 76 |
Events: []*Event{}, |
| 77 |
Links: EventsPaginationInfo{}, |
| 78 |
} |
| 79 |
|
| 80 |
path := fmt.Sprintf("/events") |
| 81 |
|
| 82 |
filters := &RequestOptions{Params: i.formatEventFilters()} |
| 83 |
|
| 84 |
resp, err := c.Get(path, filters) |
| 85 |
|
| 86 |
if err != nil { |
| 87 |
return eventsResponse, err |
| 88 |
} |
| 89 |
|
| 90 |
err = c.interpretAPIEventsPage(&eventsResponse, i.PageNumber, resp) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
return eventsResponse, err |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
type GetAPIEventInput struct { |
| 100 |
|
| 101 |
EventID string |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (c *Client) GetAPIEvent(i *GetAPIEventInput) (*Event, error) { |
| 106 |
if i.EventID == "" { |
| 107 |
return nil, ErrMissingEventID |
| 108 |
} |
| 109 |
|
| 110 |
path := fmt.Sprintf("/events/%s", i.EventID) |
| 111 |
resp, err := c.Get(path, nil) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
|
| 116 |
var event Event |
| 117 |
if err := jsonapi.UnmarshalPayload(resp.Body, &event); err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
return &event, nil |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func (c *Client) interpretAPIEventsPage(answer *GetAPIEventsResponse, pageNum int, received *http.Response) error { |
| 128 |
|
| 129 |
|
| 130 |
pages, body, err := getEventsPages(received.Body) |
| 131 |
if err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
answer.Links = pages |
| 135 |
data, err := jsonapi.UnmarshalManyPayload(body, reflect.TypeOf(new(Event))) |
| 136 |
if err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
|
| 140 |
for i := range data { |
| 141 |
typed, ok := data[i].(*Event) |
| 142 |
if !ok { |
| 143 |
return fmt.Errorf("got back response of unexpected type") |
| 144 |
} |
| 145 |
answer.Events = append(answer.Events, typed) |
| 146 |
} |
| 147 |
if pageNum == 0 { |
| 148 |
if pages.Next != "" { |
| 149 |
|
| 150 |
resp, err := c.SimpleGet(pages.Next) |
| 151 |
if err != nil { |
| 152 |
return err |
| 153 |
} |
| 154 |
c.interpretAPIEventsPage(answer, pageNum, resp) |
| 155 |
} |
| 156 |
return nil |
| 157 |
} |
| 158 |
return nil |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
func getEventsPages(body io.Reader) (EventsPaginationInfo, io.Reader, error) { |
| 165 |
var buf bytes.Buffer |
| 166 |
tee := io.TeeReader(body, &buf) |
| 167 |
|
| 168 |
bodyBytes, err := ioutil.ReadAll(tee) |
| 169 |
if err != nil { |
| 170 |
return EventsPaginationInfo{}, nil, err |
| 171 |
} |
| 172 |
|
| 173 |
var pages *GetAPIEventsResponse |
| 174 |
json.Unmarshal(bodyBytes, &pages) |
| 175 |
return pages.Links, bytes.NewReader(buf.Bytes()), nil |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string { |
| 181 |
result := map[string]string{} |
| 182 |
pairings := map[string]interface{}{ |
| 183 |
"filter[customer_id]": i.CustomerID, |
| 184 |
"filter[service_id]": i.ServiceID, |
| 185 |
"filter[event_type]": i.EventType, |
| 186 |
"filter[user_id]": i.UserID, |
| 187 |
"page[size]": i.MaxResults, |
| 188 |
"page[number]": i.PageNumber, |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
for key, value := range pairings { |
| 194 |
switch t := reflect.TypeOf(value).String(); t { |
| 195 |
case "string": |
| 196 |
if value != "" { |
| 197 |
result[key] = value.(string) |
| 198 |
} |
| 199 |
case "int": |
| 200 |
if value != 0 { |
| 201 |
result[key] = strconv.Itoa(value.(int)) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
return result |
| 207 |
} |