diff --git a/.gitignore b/.gitignore index 1c55b44e..4837de48 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,8 @@ vendor/ # coverage by go test coverage.txt +profile.cov .idea *.iml -.pre-commit-config.yaml \ No newline at end of file +.pre-commit-config.yaml diff --git a/README.md b/README.md index d8049d49..004fc1ff 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,10 @@ func main() { go-zendesk has a [mock package](https://pkg.go.dev/github.com/nukosuke/go-zendesk/zendesk/mock) generated by [golang/mock](https://github.com/golang/mock). You can simulate the response from Zendesk API with it. +## To regenerate the mock client + +`go generate ./...` + ## Authors - [nukosuke](https://github.com/nukosuke) - [tamccall](https://github.com/tamccall) diff --git a/fixture/GET/search_count_ticket.json b/fixture/GET/search_count_ticket.json new file mode 100644 index 00000000..3205fc91 --- /dev/null +++ b/fixture/GET/search_count_ticket.json @@ -0,0 +1,3 @@ +{ + "count": 10 +} diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go index caea67f1..665d6fb0 100644 --- a/zendesk/mock/client.go +++ b/zendesk/mock/client.go @@ -953,6 +953,22 @@ func (mr *ClientMockRecorder) Search(arg0, arg1 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Search", reflect.TypeOf((*Client)(nil).Search), arg0, arg1) } +// SearchCount mocks base method +func (m *Client) SearchCount(ctx context.Context, opts *zendesk.CountOptions) (int, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SearchCount", ctx, opts) + ret0, _ := ret[0].(int) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// SearchCount indicates an expected call of SearchCount +func (mr *ClientMockRecorder) SearchCount(ctx, opts interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SearchCount", reflect.TypeOf((*Client)(nil).SearchCount), ctx, + opts) +} + // UpdateAutomation mocks base method. func (m *Client) UpdateAutomation(arg0 context.Context, arg1 int64, arg2 zendesk.Automation) (zendesk.Automation, error) { m.ctrl.T.Helper() diff --git a/zendesk/search.go b/zendesk/search.go index 0cff9889..576316de 100644 --- a/zendesk/search.go +++ b/zendesk/search.go @@ -16,8 +16,16 @@ type SearchOptions struct { SortOrder string `url:"sort_order,omitempty"` } +// CountOptions are the options that can be provided to the search results count API +// +// ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count +type CountOptions struct { + Query string `url:"query"` +} + type SearchAPI interface { Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error) + SearchCount(ctx context.Context, opts *CountOptions) (int, error) } type SearchResults struct { @@ -140,3 +148,33 @@ func (z *Client) Search(ctx context.Context, opts *SearchOptions) (SearchResults return data.Results, data.Page, nil } + +// SearchCount allows users to get count of results of a query of zendesk's unified search api. +// +// ref: https://developer.zendesk.com/rest_api/docs/support/search#show-results-count +func (z *Client) SearchCount(ctx context.Context, opts *CountOptions) (int, error) { + var data struct { + Count int `json:"count"` + } + + if opts == nil { + return 0, &OptionsError{opts} + } + + u, err := addOptions("/search/count.json", opts) + if err != nil { + return 0, err + } + + body, err := z.get(ctx, u) + if err != nil { + return 0, err + } + + err = json.Unmarshal(body, &data) + if err != nil { + return 0, err + } + + return data.Count, nil +} diff --git a/zendesk/search_test.go b/zendesk/search_test.go index 4d3668f7..29422ba9 100644 --- a/zendesk/search_test.go +++ b/zendesk/search_test.go @@ -33,6 +33,22 @@ func TestSearchTickets(t *testing.T) { } } +func TestCountTickets(t *testing.T) { + mockAPI := newMockAPI(http.MethodGet, "search_count_ticket.json") + client := newTestClient(mockAPI) + defer mockAPI.Close() + + count, err := client.SearchCount(ctx, &CountOptions{}) + if err != nil { + t.Fatalf("Failed to get count: %s", err) + } + + expected := 10 + if count != expected { + t.Fatalf("expected count of tickets is %d, but got %d", expected, count) + } +} + func BenchmarkUnmarshalSearchResults(b *testing.B) { file := readFixture("ticket_result.json") for i := 0; i < b.N; i++ {