From dabcfd41893e3770081b2143bab7cb821662afec Mon Sep 17 00:00:00 2001 From: Zach Dunton Date: Tue, 11 Dec 2018 18:06:04 +0100 Subject: [PATCH] Add Ticket Search support and fix Ticket unmarshaling --- client.go | 15 ++++++---- search.go | 32 ++++++++++++++++++++++ search_struct.go | 8 ++++++ test/zendesk_test.go | 37 ++++++++++++++++++------- ticket.go | 5 ++-- ticket_struct.go | 65 ++++++++++++++++++++++++-------------------- 6 files changed, 115 insertions(+), 47 deletions(-) create mode 100644 search.go create mode 100644 search_struct.go diff --git a/client.go b/client.go index 426bb1c..85f37f1 100644 --- a/client.go +++ b/client.go @@ -1,14 +1,15 @@ package zendesk import ( - "gopkg.in/resty.v0" - "fmt" "encoding/json" + "fmt" + + "gopkg.in/resty.v0" ) type Client struct { - domain string - client *resty.Client + domain string + client *resty.Client apiVersion string } @@ -20,6 +21,10 @@ func (c Client) Ticket() TicketApiHandler { return TicketApiHandler{c} } +func (c Client) Search() SearchApiHandler { + return SearchApiHandler{c} +} + func (c Client) toFullUrl(path string) string { return fmt.Sprintf("https://%v.zendesk.com/api/%s/%s", c.domain, c.apiVersion, path) } @@ -60,4 +65,4 @@ func (c Client) delete(path string) (*resty.Response, error) { func (c Client) parseResponseToInterface(response *resty.Response, v interface{}) { json.Unmarshal(response.Body(), &v) -} \ No newline at end of file +} diff --git a/search.go b/search.go new file mode 100644 index 0000000..a95c9f2 --- /dev/null +++ b/search.go @@ -0,0 +1,32 @@ +package zendesk + +import ( + "fmt" + + resty "gopkg.in/resty.v0" +) + +type SearchApiHandler struct { + client Client +} + +func (s SearchApiHandler) SearchTickets(searchString string) (TicketSearch, error) { + response, err := s.client.get( + fmt.Sprintf("/search.json?query=type:ticket %s", searchString), + nil, + ) + + if err != nil { + + } + + return s.parseResults(response), err +} + +func (s SearchApiHandler) parseResults(response *resty.Response) TicketSearch { + var object TicketSearch + + s.client.parseResponseToInterface(response, &object) + + return object +} diff --git a/search_struct.go b/search_struct.go new file mode 100644 index 0000000..3e9d733 --- /dev/null +++ b/search_struct.go @@ -0,0 +1,8 @@ +package zendesk + +type TicketSearch struct { + Count int `json:"count,omitempty"` + NextPage string `json:"next_page,omitempty"` + PrevPage string `json:"prev_page,omitempty"` + Tickets []Ticket `json:"results,omitempty"` +} diff --git a/test/zendesk_test.go b/test/zendesk_test.go index d3eba4b..36a5e7d 100644 --- a/test/zendesk_test.go +++ b/test/zendesk_test.go @@ -2,12 +2,13 @@ package test import ( "testing" - "github.com/hellofresh/zendesk-go" + + zendesk "github.com/hellofresh/zendesk-go" ) var client = zendesk.FromToken( zendesk.LoadConfiguration("./../config/configuration.yml"), -); +) var id int @@ -38,7 +39,7 @@ func TestUserApiHandler_GetById(t *testing.T) { func TestUserApiHandler_Create(t *testing.T) { user := zendesk.User{ - Name: "Felipe Pieretti Umpierre", + Name: "Felipe Pieretti Umpierre", Email: "fum@hellofresh.com", } @@ -52,7 +53,7 @@ func TestUserApiHandler_Create(t *testing.T) { func TestUserApiHandler_CreateOrUpdate(t *testing.T) { user := zendesk.User{ - Name: "Felipe Pieretti Umpierre = Updated", + Name: "Felipe Pieretti Umpierre = Updated", Email: "fum@hellofresh.com", } @@ -68,12 +69,12 @@ func TestUserApiHandler_CreateOrUpdateMany(t *testing.T) { var many zendesk.ManyUsers many.AppendUsers(zendesk.User{ - Name: "User 1", + Name: "User 1", Email: "user-1@hellofresh.com", }) many.AppendUsers(zendesk.User{ - Name: "User-2", + Name: "User-2", Email: "user-2@hellofresh.com", }) @@ -96,8 +97,8 @@ func TestUserApiHandler_Delete(t *testing.T) { func TestUserApiHandler_Update(t *testing.T) { user := zendesk.User{ - Id: id, - Name: "Felipe Pieretti Umpierre - hallo", + Id: id, + Name: "Felipe Pieretti Umpierre - hallo", Email: "fum@hellofresh.com", } @@ -109,6 +110,22 @@ func TestUserApiHandler_Update(t *testing.T) { } } +// --------- SEARCH -------- + +func TestSearchApiHandler_Search(t *testing.T) { + tickets, err := client.Search().SearchTickets("TEST") + + if err != nil { + t.Errorf("Error: %s", err) + t.Fail() + } + + for _, ticket := range tickets.Tickets { + id = ticket.Id + break + } +} + // --------- TICKET -------- func TestTicketApiHandler_GetAll(t *testing.T) { @@ -171,7 +188,7 @@ func TestTicketApiHandler_Delete(t *testing.T) { func TestTicketApiHandler_Update(t *testing.T) { ticket := zendesk.Ticket{ - Id: id, + Id: id, Description: "Test ticket", } @@ -181,4 +198,4 @@ func TestTicketApiHandler_Update(t *testing.T) { t.Errorf("Error: %s", err) t.Fail() } -} \ No newline at end of file +} diff --git a/ticket.go b/ticket.go index dcf4a5a..b1a1fa8 100644 --- a/ticket.go +++ b/ticket.go @@ -2,7 +2,8 @@ package zendesk import ( "fmt" - "gopkg.in/resty.v0" + + resty "gopkg.in/resty.v0" ) type TicketApiHandler struct { @@ -97,4 +98,4 @@ func (t TicketApiHandler) parseSingleObject(response *resty.Response) Ticket { t.client.parseResponseToInterface(response, &object) return object.Response -} \ No newline at end of file +} diff --git a/ticket_struct.go b/ticket_struct.go index 1d70c13..abb56e4 100644 --- a/ticket_struct.go +++ b/ticket_struct.go @@ -2,35 +2,40 @@ package zendesk type Via struct{} +type CustomFields struct { + Id int `json:"id,omitempty"` + Value string `json:"value,omitempty"` +} + type Ticket struct { - Id int `json:"id,omitempty"` - Url string `json:"url,omitempty"` - ExternalId string `json:"external_id,omitempty"` - Type string `json:"type,omitempty"` - Subject string `json:"subject,omitempty"` - RawSubject string `json:"raw_subject,omitempty"` - Description string `json:"description,omitempty"` - Priority string `json:"priority,omitempty"` - Status string `json:"status,omitempty"` - Recipient string `json:"recipient,omitempty"` - RequesterId int `json:"requester_id,omitempty"` - SubmitterId int `json:"submitter_id,omitempty"` - AssigneeId int `json:"assignee_id,omitempty"` - OrganizationId int `json:"organization_id,omitempty"` - GroupId int `json:"group_id,omitempty"` - CollaboratorsId []int `json:"collaborators_id,omitempty"` - ForumTopicId int `json:"forum_topic_id,omitempty"` - ProblemId int `json:"problem_id,omitempty"` - HasIncidents bool `json:"has_incidents,omitempty"` - DueAt string `json:"due_at,omitempty"` - Tags []string `json:"tags,omitempty"` - Via *Via `json:"via,omitempty"` - CustomFields []string `json:"custom_fields,omitempty"` - SatisfactionRating []string `json:"satisfaction_rating,omitempty"` - SharingAgreementIds []int `json:"sharing_agreement_ids,omitempty"` - FollowupIds []int `json:"followup_ids,omitempty"` - TicketFormId int `json:"ticket_form_id,omitempty"` - BrandId int `json:"brand_id,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` + Id int `json:"id,omitempty"` + Url string `json:"url,omitempty"` + ExternalId string `json:"external_id,omitempty"` + Type string `json:"type,omitempty"` + Subject string `json:"subject,omitempty"` + RawSubject string `json:"raw_subject,omitempty"` + Description string `json:"description,omitempty"` + Priority string `json:"priority,omitempty"` + Status string `json:"status,omitempty"` + Recipient string `json:"recipient,omitempty"` + RequesterId int `json:"requester_id,omitempty"` + SubmitterId int `json:"submitter_id,omitempty"` + AssigneeId int `json:"assignee_id,omitempty"` + OrganizationId int `json:"organization_id,omitempty"` + GroupId int `json:"group_id,omitempty"` + CollaboratorsId []int `json:"collaborators_id,omitempty"` + ForumTopicId int `json:"forum_topic_id,omitempty"` + ProblemId int `json:"problem_id,omitempty"` + HasIncidents bool `json:"has_incidents,omitempty"` + DueAt string `json:"due_at,omitempty"` + Tags []string `json:"tags,omitempty"` + Via *Via `json:"via,omitempty"` + CustomFields []CustomFields `json:"custom_fields,omitempty"` + SatisfactionRating map[string]string `json:"satisfaction_rating,omitempty"` + SharingAgreementIds []int `json:"sharing_agreement_ids,omitempty"` + FollowupIds []int `json:"followup_ids,omitempty"` + TicketFormId int `json:"ticket_form_id,omitempty"` + BrandId int `json:"brand_id,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` }