Skip to content

Commit 48826e7

Browse files
committed
add support for TicketMetricEvents API
1 parent e4a6604 commit 48826e7

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

zendesk/ticket_metric_events.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package zendesk
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"time"
7+
)
8+
9+
// SLA represents the SLA payload as part of a ticket metric event
10+
type SLA struct {
11+
Target int `json:"target"`
12+
BusinessHours bool `json:"business_hours"`
13+
Policy struct {
14+
ID int `json:"id"`
15+
Title string `json:"title"`
16+
Description string `json:"description"`
17+
} `json:"policy"`
18+
}
19+
20+
// TicketMetricEvent represents a ticket metrc event
21+
type TicketMetricEvent struct {
22+
ID int64 `json:"id"`
23+
InstanceID int `json:"instance_id"`
24+
Metric string `json:"metric"`
25+
TicketID int `json:"ticket_id"`
26+
Time time.Time `json:"time"`
27+
Type string `json:"type"`
28+
29+
// optional fields depending on type
30+
Status *TimeDuration `json:"status,omitempty"`
31+
SLA *SLA `json:"sla,omitempty"`
32+
GroupSLA *SLA `json:"group_sla,omitempty"`
33+
Deleted bool `json:"deleted,omitempty"`
34+
}
35+
36+
// Timestamp is used to unmarshal a UNIX timestamp into time.Time
37+
type Timestamp struct {
38+
time.Time
39+
}
40+
41+
func (p *Timestamp) UnmarshalJSON(bytes []byte) error {
42+
var raw int64
43+
err := json.Unmarshal(bytes, &raw)
44+
if err != nil {
45+
return err
46+
}
47+
p.Time = time.Unix(raw, 0)
48+
return nil
49+
}
50+
51+
// TicketMetricEventsPage represents the page information of the TicketMetricEvents API response
52+
type TicketMetricEventsPage struct {
53+
Count int `json:"count"`
54+
EndTime Timestamp `json:"end_time"`
55+
EndOfStream bool `json:"end_of_stream"`
56+
NextPage string `json:"next_page"`
57+
}
58+
59+
// TicketMetricEventsAPI is the interface of the TicketMetricEvents API
60+
type TicketMetricEventsAPI interface {
61+
GetTicketMetricEvents(ctx context.Context, start time.Time) ([]TicketMetricEvent, TicketMetricEventsPage, error)
62+
}
63+
64+
// TicketMetricEventsOptions represents the options for the GetTicketMetricEvents method
65+
type TicketMetricEventsOptions struct {
66+
StartTime int64 `url:"start_time"`
67+
}
68+
69+
// GetTicketMetricEvents
70+
func (z *Client) GetTicketMetricEvents(ctx context.Context, start time.Time) ([]TicketMetricEvent, TicketMetricEventsPage, error) {
71+
var data struct {
72+
TicketMetricEventsPage
73+
TicketMetricEvents []TicketMetricEvent `json:"ticket_metric_events"`
74+
}
75+
76+
opts := TicketMetricEventsOptions{
77+
StartTime: start.Unix(),
78+
}
79+
u, err := addOptions("/incremental/ticket_metric_events.json", opts)
80+
if err != nil {
81+
return nil, TicketMetricEventsPage{}, err
82+
}
83+
84+
body, err := z.get(ctx, u)
85+
if err != nil {
86+
return nil, TicketMetricEventsPage{}, err
87+
}
88+
89+
err = json.Unmarshal(body, &data)
90+
if err != nil {
91+
return nil, TicketMetricEventsPage{}, err
92+
}
93+
return data.TicketMetricEvents, data.TicketMetricEventsPage, nil
94+
}

0 commit comments

Comments
 (0)