forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.go
87 lines (76 loc) · 2.6 KB
/
support.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
79
80
81
82
83
84
85
86
87
package linodego
import (
"context"
"fmt"
"time"
"github.com/go-resty/resty/v2"
)
// Ticket represents a support ticket object
type Ticket struct {
ID int `json:"id"`
Attachments []string `json:"attachments"`
Closed *time.Time `json:"-"`
Description string `json:"description"`
Entity *TicketEntity `json:"entity"`
GravatarID string `json:"gravatar_id"`
Opened *time.Time `json:"-"`
OpenedBy string `json:"opened_by"`
Status TicketStatus `json:"status"`
Summary string `json:"summary"`
Updated *time.Time `json:"-"`
UpdatedBy string `json:"updated_by"`
}
// TicketEntity refers a ticket to a specific entity
type TicketEntity struct {
ID int `json:"id"`
Label string `json:"label"`
Type string `json:"type"`
URL string `json:"url"`
}
// TicketStatus constants start with Ticket and include Linode API Ticket Status values
type TicketStatus string
// TicketStatus constants reflect the current status of a Ticket
const (
TicketNew TicketStatus = "new"
TicketClosed TicketStatus = "closed"
TicketOpen TicketStatus = "open"
)
// TicketsPagedResponse represents a paginated ticket API response
type TicketsPagedResponse struct {
*PageOptions
Data []Ticket `json:"data"`
}
func (TicketsPagedResponse) endpoint(_ ...any) string {
return "support/tickets"
}
func (resp *TicketsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(TicketsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*TicketsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListTickets returns a collection of Support Tickets on the Account. Support Tickets
// can be both tickets opened with Linode for support, as well as tickets generated by
// Linode regarding the Account. This collection includes all Support Tickets generated
// on the Account, with open tickets returned first.
func (c *Client) ListTickets(ctx context.Context, opts *ListOptions) ([]Ticket, error) {
response := TicketsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetTicket gets a Support Ticket on the Account with the specified ID
func (c *Client) GetTicket(ctx context.Context, ticketID int) (*Ticket, error) {
e := fmt.Sprintf("support/tickets/%d", ticketID)
req := c.R(ctx).SetResult(&Ticket{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Ticket), nil
}