Skip to content

Commit

Permalink
Merge 468ef76 into f6cfa64
Browse files Browse the repository at this point in the history
  • Loading branch information
nukosuke committed Jun 11, 2019
2 parents f6cfa64 + 468ef76 commit d57bfde
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
31 changes: 31 additions & 0 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions zendesk/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,53 @@ type TicketComment struct {
Body string `json:"body"`
}

type TicketListOptions struct {
PageOptions

// SortBy can take "assignee", "assignee.name", "created_at", "group", "id",
// "locale", "requester", "requester.name", "status", "subject", "updated_at"
SortBy string `url:"sort_by,omitempty"`

// SortOrder can take "asc" or "desc"
SortOrder string `url:"sort_order,omitempty"`
}

// TicketAPI an interface containing all ticket related methods
type TicketAPI interface {
GetTickets(ctx context.Context, opts *TicketListOptions) ([]Ticket, Page, error)
GetTicket(ctx context.Context, id int64) (Ticket, error)
CreateTicket(ctx context.Context, ticket Ticket) (Ticket, error)
}

// GetTickets get ticket list
//
// ref: https://developer.zendesk.com/rest_api/docs/support/tickets#list-tickets
func (z *Client) GetTickets(ctx context.Context, opts *TicketListOptions) ([]Ticket, Page, error) {
var data struct {
Tickets []Ticket `json:"tickets"`
Page
}

tmp := opts
if tmp == nil {
tmp = &TicketListOptions{}
}

u, err := addOptions("/tickets.json", tmp)
if err != nil {
return nil, Page{}, err
}

body, err := z.get(ctx, u)
if err != nil {
return nil, Page{}, err
}

err = json.Unmarshal(body, &data)
if err != nil {
return nil, Page{}, err
}
return data.Tickets, data.Page, nil
}

// GetTicket gets a specified ticket
Expand Down
23 changes: 23 additions & 0 deletions zendesk/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@ import (
"testing"
)

func TestGetTickets(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "tickets.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

tickets, _, err := client.GetTickets(ctx, &TicketListOptions{
PageOptions: PageOptions{
Page: 1,
PerPage: 10,
},
SortBy: "id",
SortOrder: "asc",
})
if err != nil {
t.Fatalf("Failed to get tickets: %s", err)
}

expectedLength := 2
if len(tickets) != expectedLength {
t.Fatalf("Returned tickets does not have the expected length %d. Tickets length is %d", expectedLength, len(tickets))
}
}

func TestGetTicket(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "ticket.json")
client := newTestClient(mockAPI)
Expand Down

0 comments on commit d57bfde

Please sign in to comment.