Skip to content
This repository has been archived by the owner on Jun 24, 2021. It is now read-only.

Commit

Permalink
Add Ticket Search support and fix Ticket unmarshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
zach-dunton-sf committed Dec 11, 2018
1 parent 717dd54 commit dabcfd4
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 47 deletions.
15 changes: 10 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
32 changes: 32 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions search_struct.go
Original file line number Diff line number Diff line change
@@ -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"`
}
37 changes: 27 additions & 10 deletions test/zendesk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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: "[email protected]",
}

Expand All @@ -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: "[email protected]",
}

Expand All @@ -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: "[email protected]",
})

many.AppendUsers(zendesk.User{
Name: "User-2",
Name: "User-2",
Email: "[email protected]",
})

Expand All @@ -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: "[email protected]",
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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",
}

Expand All @@ -181,4 +198,4 @@ func TestTicketApiHandler_Update(t *testing.T) {
t.Errorf("Error: %s", err)
t.Fail()
}
}
}
5 changes: 3 additions & 2 deletions ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package zendesk

import (
"fmt"
"gopkg.in/resty.v0"

resty "gopkg.in/resty.v0"
)

type TicketApiHandler struct {
Expand Down Expand Up @@ -97,4 +98,4 @@ func (t TicketApiHandler) parseSingleObject(response *resty.Response) Ticket {
t.client.parseResponseToInterface(response, &object)

return object.Response
}
}
65 changes: 35 additions & 30 deletions ticket_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit dabcfd4

Please sign in to comment.