Skip to content

Commit

Permalink
Merge pull request #196 from jmeridth/add-search-count-endpoint-to-sdk
Browse files Browse the repository at this point in the history
Add Search Results Count to sdk
  • Loading branch information
nukosuke authored Jan 22, 2021
2 parents 4bfaec9 + a906b5b commit fde1b33
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ vendor/

# coverage by go test
coverage.txt
profile.cov

.idea
*.iml
.pre-commit-config.yaml
.pre-commit-config.yaml
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions fixture/GET/search_count_ticket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"count": 10
}
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.

38 changes: 38 additions & 0 deletions zendesk/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions zendesk/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down

0 comments on commit fde1b33

Please sign in to comment.