Skip to content

Commit

Permalink
Merge pull request #286 from paoloromolini/custom_objects_v2_search
Browse files Browse the repository at this point in the history
feat: add SearchCustomObjectRecords function
  • Loading branch information
nukosuke authored Sep 20, 2023
2 parents 1684b1d + eacd31f commit 096ccfd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
50 changes: 46 additions & 4 deletions zendesk/custom_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ type CustomObjectRecord struct {
type CustomObjectAPI interface {
CreateCustomObjectRecord(
ctx context.Context, record CustomObjectRecord, customObjectKey string) (CustomObjectRecord, error)
SearchCustomObjectRecords(
AutocompleteSearchCustomObjectRecords(
ctx context.Context,
customObjectKey string,
opts *CustomObjectAutocompleteOptions,
) ([]CustomObjectRecord, Page, error)
SearchCustomObjectRecords(
ctx context.Context, customObjectKey string, opts *SearchCustomObjectRecordsOptions,
) ([]CustomObjectRecord, Page, error)
ListCustomObjectRecords(
ctx context.Context, customObjectKey string, opts *CustomObjectListOptions) ([]CustomObjectRecord, Page, error)
ShowCustomObjectRecord(
Expand Down Expand Up @@ -98,19 +101,58 @@ func (z *Client) ListCustomObjectRecords(
return result.CustomObjectRecords, result.Page, nil
}

// AutocompleteSearchCustomObjectRecords search for a custom object record by the name field
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#autocomplete-custom-object-record-search
func (z *Client) AutocompleteSearchCustomObjectRecords(
ctx context.Context, customObjectKey string, opts *CustomObjectAutocompleteOptions,
) ([]CustomObjectRecord, Page, error) {
var result struct {
CustomObjectRecords []CustomObjectRecord `json:"custom_object_records"`
Page
}
tmp := opts
if tmp == nil {
tmp = &CustomObjectAutocompleteOptions{}
}
url := fmt.Sprintf("/custom_objects/%s/records/autocomplete", customObjectKey)
urlWithOptions, err := addOptions(url, tmp)
body, err := z.get(ctx, urlWithOptions)

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

type SearchCustomObjectRecordsOptions struct {
PageOptions

// One of name, created_at, updated_at, -name, -created_at, or -updated_at.
// The - denotes the sort will be descending. Defaults to sorting by relevance.
Sort string `url:"sort,omitempty"`

// Query string
Query string `url:"query,omitempty"`
}

// SearchCustomObjectRecords search for a custom object record by the name field
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#search-custom-object-records
func (z *Client) SearchCustomObjectRecords(
ctx context.Context, customObjectKey string, opts *CustomObjectAutocompleteOptions) ([]CustomObjectRecord, Page, error) {
ctx context.Context, customObjectKey string, opts *SearchCustomObjectRecordsOptions,
) ([]CustomObjectRecord, Page, error) {
var result struct {
CustomObjectRecords []CustomObjectRecord `json:"custom_object_records"`
Page
}
tmp := opts
if tmp == nil {
tmp = &CustomObjectAutocompleteOptions{}
tmp = &SearchCustomObjectRecordsOptions{}
}
url := fmt.Sprintf("/custom_objects/%s/records/autocomplete", customObjectKey)
url := fmt.Sprintf("/custom_objects/%s/records/search", customObjectKey)
urlWithOptions, err := addOptions(url, tmp)
body, err := z.get(ctx, urlWithOptions)

Expand Down
18 changes: 17 additions & 1 deletion zendesk/mock/client.go

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

0 comments on commit 096ccfd

Please sign in to comment.