Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add export search api support #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions zendesk/mock/client.go

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

41 changes: 41 additions & 0 deletions zendesk/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,19 @@ type CountOptions struct {
Query string `url:"query"`
}

// SearchExportOptions are the options that can be provided to the export search API
//
// https://developer.zendesk.com/api-reference/ticketing/ticket-management/search/#export-search-results
type SearchExportOptions struct {
CursorPagination
Query string `url:"query"`
Filter string `url:"filter[type],omitempty"`
}

type SearchAPI interface {
Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error)
SearchCount(ctx context.Context, opts *CountOptions) (int, error)
SearchExport(ctx context.Context, opts *SearchExportOptions) (SearchResults, CursorPaginationMeta, error)
GetSearchIterator(ctx context.Context, opts *PaginationOptions) *Iterator[SearchResults]
GetSearchOBP(ctx context.Context, opts *OBPOptions) ([]SearchResults, Page, error)
GetSearchCBP(ctx context.Context, opts *CBPOptions) ([]SearchResults, CursorPaginationMeta, error)
Expand Down Expand Up @@ -181,3 +191,34 @@ func (z *Client) SearchCount(ctx context.Context, opts *CountOptions) (int, erro

return data.Count, nil
}

// SearchExport allows users to query zendesk's unified export search api.
//
// ref: https://developer.zendesk.com/api-reference/ticketing/ticket-management/search/#export-search-results
func (z *Client) SearchExport(ctx context.Context, opts *SearchExportOptions) (SearchResults, CursorPaginationMeta, error) {
var data struct {
Results SearchResults `json:"results"`
Meta CursorPaginationMeta `json:"meta"`
}

if opts == nil {
return SearchResults{}, CursorPaginationMeta{}, &OptionsError{opts}
}

u, err := addOptions("/search/export", opts)
if err != nil {
return SearchResults{}, CursorPaginationMeta{}, err
}

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

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

return data.Results, data.Meta, nil
}
25 changes: 25 additions & 0 deletions zendesk/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ func TestCountTickets(t *testing.T) {
}
}

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

results, _, err := client.SearchExport(ctx, &SearchExportOptions{})
if err != nil {
t.Fatalf("Failed to get export search results: %s", err)
}

list := results.List()
if len(list) != 1 {
t.Fatalf("expected length of sla policies is , but got %d", len(list))
}

ticket, ok := list[0].(Ticket)
if !ok {
t.Fatalf("Cannot assert %v as a ticket", list[0])
}

if ticket.ID != 4 {
t.Fatalf("Ticket did not have the expected id %v", ticket)
}
}

func BenchmarkUnmarshalSearchResults(b *testing.B) {
file := readFixture("ticket_result.json")
for i := 0; i < b.N; i++ {
Expand Down