-
Notifications
You must be signed in to change notification settings - Fork 8
/
events.go
67 lines (55 loc) · 1.8 KB
/
events.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
package scalingo
//go:generate go run cmd/gen_events_boilerplate/main.go
//go:generate go run cmd/gen_events_specialize/main.go
import (
"context"
"encoding/json"
"gopkg.in/errgo.v1"
"github.com/Scalingo/go-scalingo/v6/http"
)
type EventsService interface {
EventTypesList(context.Context) ([]EventType, error)
EventCategoriesList(context.Context) ([]EventCategory, error)
EventsList(ctx context.Context, app string, opts PaginationOpts) (Events, PaginationMeta, error)
UserEventsList(context.Context, PaginationOpts) (Events, PaginationMeta, error)
}
var _ EventsService = (*Client)(nil)
type EventsRes struct {
Events []*Event `json:"events"`
Meta struct {
PaginationMeta PaginationMeta `json:"pagination"`
}
}
func (c *Client) EventsList(ctx context.Context, app string, opts PaginationOpts) (Events, PaginationMeta, error) {
var eventsRes EventsRes
err := c.ScalingoAPI().SubresourceList(ctx, "apps", app, "events", opts.ToMap(), &eventsRes)
if err != nil {
return nil, PaginationMeta{}, errgo.Mask(err)
}
var events Events
for _, ev := range eventsRes.Events {
events = append(events, ev.Specialize())
}
return events, eventsRes.Meta.PaginationMeta, nil
}
func (c *Client) UserEventsList(ctx context.Context, opts PaginationOpts) (Events, PaginationMeta, error) {
req := &http.APIRequest{
Endpoint: "/events",
Params: opts.ToMap(),
}
var eventsRes EventsRes
res, err := c.ScalingoAPI().Do(ctx, req)
if err != nil {
return nil, PaginationMeta{}, errgo.Mask(err, errgo.Any)
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&eventsRes)
if err != nil {
return nil, PaginationMeta{}, errgo.Mask(err, errgo.Any)
}
var events Events
for _, ev := range eventsRes.Events {
events = append(events, ev.Specialize())
}
return events, eventsRes.Meta.PaginationMeta, nil
}