Skip to content

Commit

Permalink
Merge pull request #151 from tamccall/search
Browse files Browse the repository at this point in the history
Added SearchAPI to client
  • Loading branch information
tamccall authored Dec 12, 2019
2 parents 10abdf3 + 7b87871 commit 8f6e34b
Show file tree
Hide file tree
Showing 9 changed files with 508 additions and 0 deletions.
18 changes: 18 additions & 0 deletions fixture/GET/search_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"results": [
{
"url": "https://d3v-terraform-provider.zendesk.com/api/v2/groups/360007194452.json",
"id": 360007194452,
"name": "nyan",
"description": "",
"deleted": false,
"created_at": "2019-12-09T02:40:11Z",
"updated_at": "2019-12-09T02:40:11Z",
"result_type": "group"
}
],
"facets": null,
"next_page": null,
"previous_page": null,
"count": 1
}
59 changes: 59 additions & 0 deletions fixture/GET/search_ticket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"results": [
{
"url": "https://d3v-terraform-provider.zendesk.com/api/v2/tickets/4.json",
"id": 4,
"external_id": null,
"via": {
"channel": "api",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2019-06-06T10:02:04Z",
"updated_at": "2019-06-06T10:02:04Z",
"type": null,
"subject": "nyanyanyanya",
"raw_subject": "nyanyanyanya",
"description": "(●ↀ ω ↀ )",
"priority": "urgent",
"status": "new",
"recipient": null,
"requester_id": 377922500012,
"submitter_id": 377922500012,
"assignee_id": null,
"organization_id": 360363695492,
"group_id": 360004077472,
"collaborator_ids": [
377922500012
],
"follower_ids": [
377922500012
],
"email_cc_ids": [],
"forum_topic_id": null,
"problem_id": null,
"has_incidents": false,
"is_public": true,
"due_at": null,
"tags": [],
"custom_fields": [],
"satisfaction_rating": null,
"sharing_agreement_ids": [],
"fields": [],
"followup_ids": [],
"ticket_form_id": 360000389592,
"brand_id": 360002256672,
"satisfaction_probability": null,
"allow_channelback": false,
"allow_attachments": true,
"result_type": "ticket"
}
],
"facets": null,
"next_page": null,
"previous_page": null,
"count": 1
}
80 changes: 80 additions & 0 deletions fixture/GET/search_user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"results": [
{
"id": 1234,
"url": "https://d3v-terraform-provider.zendesk.com/api/v2/users/1234.json",
"name": "Foo Bar",
"email": "a@a.com",
"created_at": "2019-03-18T05:48:08Z",
"updated_at": "2019-12-09T06:48:10Z",
"time_zone": "Eastern Time (US & Canada)",
"iana_time_zone": "America/New_York",
"phone": null,
"shared_phone_number": null,
"photo": {
"url": "https://d3v-terraform-provider.zendesk.com/api/v2/attachments/360215133211.json",
"id": 360215133211,
"file_name": "profile_image_1234_9184218.png",
"content_url": "https://d3v-terraform-provider.zendesk.com/system/photos/3602/1513/3211/profile_image_1234_9184218.png",
"mapped_content_url": "https://d3v-terraform-provider.zendesk.com/system/photos/3602/1513/3211/profile_image_1234_9184218.png",
"content_type": "image/png",
"size": 1979,
"width": 80,
"height": 80,
"inline": false,
"deleted": false,
"thumbnails": [
{
"url": "https://d3v-terraform-provider.zendesk.com/api/v2/attachments/360215133231.json",
"id": 360215133231,
"file_name": "profile_image_1234_9184218_thumb.png",
"content_url": "https://d3v-terraform-provider.zendesk.com/system/photos/3602/1513/3211/profile_image_1234_9184218_thumb.png",
"mapped_content_url": "https://d3v-terraform-provider.zendesk.com/system/photos/3602/1513/3211/profile_image_1234_9184218_thumb.png",
"content_type": "image/png",
"size": 784,
"width": 32,
"height": 32,
"inline": false,
"deleted": false
}
]
},
"locale_id": 1,
"locale": "en-US",
"organization_id": 360363695492,
"role": "admin",
"verified": true,
"external_id": null,
"tags": [],
"alias": "",
"active": true,
"shared": false,
"shared_agent": false,
"last_login_at": "2019-12-09T06:48:10Z",
"two_factor_auth_enabled": null,
"signature": "",
"details": "",
"notes": "",
"role_type": null,
"custom_role_id": null,
"moderator": true,
"ticket_restriction": null,
"only_private_comments": false,
"restricted_agent": false,
"suspended": false,
"chat_only": false,
"default_group_id": 360004077472,
"report_csv": false,
"user_fields": {
"github_profile": "https://github.com/foobar",
"nickname": "foo bar",
"twitter_account": "https://twitter.com/foobar1345"
},
"result_type": "user"
}
],
"facets": null,
"next_page": null,
"previous_page": null,
"count": 1
}
53 changes: 53 additions & 0 deletions fixture/ticket_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[
{
"url": "https://d3v-terraform-provider.zendesk.com/api/v2/tickets/4.json",
"id": 4,
"external_id": null,
"via": {
"channel": "api",
"source": {
"from": {},
"to": {},
"rel": null
}
},
"created_at": "2019-06-06T10:02:04Z",
"updated_at": "2019-06-06T10:02:04Z",
"type": null,
"subject": "nyanyanyanya",
"raw_subject": "nyanyanyanya",
"description": "(●ↀ ω ↀ )",
"priority": "urgent",
"status": "new",
"recipient": null,
"requester_id": 377922500012,
"submitter_id": 377922500012,
"assignee_id": null,
"organization_id": 360363695492,
"group_id": 360004077472,
"collaborator_ids": [
377922500012
],
"follower_ids": [
377922500012
],
"email_cc_ids": [],
"forum_topic_id": null,
"problem_id": null,
"has_incidents": false,
"is_public": true,
"due_at": null,
"tags": [],
"custom_fields": [],
"satisfaction_rating": null,
"sharing_agreement_ids": [],
"fields": [],
"followup_ids": [],
"ticket_form_id": 360000389592,
"brand_id": 360002256672,
"satisfaction_probability": null,
"allow_channelback": false,
"allow_attachments": true,
"result_type": "ticket"
}
]
1 change: 1 addition & 0 deletions zendesk/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type API interface {
UserAPI
UserFieldAPI
OrganizationAPI
SearchAPI
SLAPolicyAPI
}

Expand Down
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.

142 changes: 142 additions & 0 deletions zendesk/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package zendesk

import (
"context"
"encoding/json"
"fmt"
)

// SearchOptions are the options that can be provided to the search API
//
// ref: https://developer.zendesk.com/rest_api/docs/support/search#available-parameters
type SearchOptions struct {
PageOptions
Query string `url:"query"`
SortBy string `url:"sort_by,omitempty"`
SortOrder string `url:"sort_order,omitempty"`
}

type SearchAPI interface {
Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error)
}

type SearchResults struct {
results []interface{}
}

func (r *SearchResults) MarshalJSON() ([]byte, error) {
return json.Marshal(r.results)
}

func (r *SearchResults) UnmarshalJSON(b []byte) error {
var (
results []interface{}
tmp []json.RawMessage
)

err := json.Unmarshal(b, &tmp)
if err != nil {
return err
}

for _, v := range tmp {
value, err := r.getObject(v)
if err != nil {
return err
}

results = append(results, value)
}

r.results = results

return nil
}

func (r *SearchResults) getObject(blob json.RawMessage) (interface{}, error) {
m := make(map[string]interface{})

err := json.Unmarshal(blob, &m)
if err != nil {
return nil, err
}

t, ok := m["result_type"].(string)
if !ok {
return nil, fmt.Errorf("could not assert result type to string. json was: %v", blob)
}

var value interface{}

switch t {
case "group":
var g Group
err = json.Unmarshal(blob, &g)
value = g
case "ticket":
var t Ticket
err = json.Unmarshal(blob, &t)
value = t
case "user":
var u User
err = json.Unmarshal(blob, &u)
value = u
case "organization":
var o Organization
err = json.Unmarshal(blob, &o)
value = o
case "topic":
var t Topic
err = json.Unmarshal(blob, &t)
value = t
default:
err = fmt.Errorf("value of result was an unsupported type %s", t)
}

if err != nil {
return nil, err
}

return value, nil
}

// String return string formatted for Search results
func (r *SearchResults) String() string {
return fmt.Sprintf("%v", r.results)
}

// List return internal array in Search Results
func (r *SearchResults) List() []interface{} {
return r.results
}

// Search allows users to query zendesk's unified search api.
//
// ref: https://developer.zendesk.com/rest_api/docs/support/search
func (z *Client) Search(ctx context.Context, opts *SearchOptions) (SearchResults, Page, error) {
var data struct {
Results SearchResults `json:"results"`
Page
}

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

u, err := addOptions("/search.json", opts)
if err != nil {
return SearchResults{}, Page{}, err
}

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

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

return data.Results, data.Page, nil
}
Loading

0 comments on commit 8f6e34b

Please sign in to comment.