|
| 1 | +package zendesk |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +// TicketMetricEvent represents a ticket metrc event |
| 10 | +type TicketMetricEvent struct { |
| 11 | + ID int64 `json:"id"` |
| 12 | + InstanceID int `json:"instance_id"` |
| 13 | + Metric string `json:"metric"` |
| 14 | + TicketID int `json:"ticket_id"` |
| 15 | + Time time.Time `json:"time"` |
| 16 | + Type string `json:"type"` |
| 17 | +} |
| 18 | + |
| 19 | +// Timestamp is used to unmarshal a UNIX timestamp into time.Time |
| 20 | +type Timestamp struct { |
| 21 | + time.Time |
| 22 | +} |
| 23 | + |
| 24 | +func (p *Timestamp) UnmarshalJSON(bytes []byte) error { |
| 25 | + var raw int64 |
| 26 | + err := json.Unmarshal(bytes, &raw) |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + p.Time = time.Unix(raw, 0) |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +// TicketMetricEventsPage represents the page information of the TicketMetricEvents API response |
| 35 | +type TicketMetricEventsPage struct { |
| 36 | + Count int `json:"count"` |
| 37 | + EndTime Timestamp `json:"end_time"` |
| 38 | + EndOfStream bool `json:"end_of_stream"` |
| 39 | + NextPage string `json:"next_page"` |
| 40 | +} |
| 41 | + |
| 42 | +// TicketMetricEventsAPI is the interface of the TicketMetricEvents API |
| 43 | +type TicketMetricEventsAPI interface { |
| 44 | + GetTicketMetricEvents(ctx context.Context, start time.Time) ([]TicketMetricEvent, TicketMetricEventsPage, error) |
| 45 | +} |
| 46 | + |
| 47 | +// TicketMetricEventsOptions represents the options for the GetTicketMetricEvents method |
| 48 | +type TicketMetricEventsOptions struct { |
| 49 | + StartTime int64 `url:"start_time"` |
| 50 | +} |
| 51 | + |
| 52 | +// GetTicketMetricEvents |
| 53 | +func (z *Client) GetTicketMetricEvents(ctx context.Context, start time.Time) ([]TicketMetricEvent, TicketMetricEventsPage, error) { |
| 54 | + var data struct { |
| 55 | + TicketMetricEventsPage |
| 56 | + TicketMetricEvents []TicketMetricEvent `json:"ticket_metric_events"` |
| 57 | + } |
| 58 | + |
| 59 | + opts := TicketMetricEventsOptions{ |
| 60 | + StartTime: start.Unix(), |
| 61 | + } |
| 62 | + u, err := addOptions("/incremental/ticket_metric_events.json", opts) |
| 63 | + if err != nil { |
| 64 | + return nil, TicketMetricEventsPage{}, err |
| 65 | + } |
| 66 | + |
| 67 | + body, err := z.get(ctx, u) |
| 68 | + if err != nil { |
| 69 | + return nil, TicketMetricEventsPage{}, err |
| 70 | + } |
| 71 | + |
| 72 | + err = json.Unmarshal(body, &data) |
| 73 | + if err != nil { |
| 74 | + return nil, TicketMetricEventsPage{}, err |
| 75 | + } |
| 76 | + return data.TicketMetricEvents, data.TicketMetricEventsPage, nil |
| 77 | +} |
0 commit comments