diff --git a/.gitignore b/.gitignore index d03526f..f589a79 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,5 @@ _testmain.go *.test *.prof -/vendor \ No newline at end of file +/vendor +/.idea/ diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json deleted file mode 100644 index e61f415..0000000 --- a/Godeps/Godeps.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "ImportPath": "github.com/hellofresh/zendesk-go", - "GoVersion": "go1.6", - "GodepVersion": "v60", - "Deps": [ - { - "ImportPath": "github.com/golang/protobuf/proto", - "Rev": "f0a097ddac24fb00e07d2ac17f8671423f3ea47c" - }, - { - "ImportPath": "github.com/ttacon/builder", - "Rev": "7f152c3cf4714fd6318739f8f3dbcd14c2a18b39" - }, - { - "ImportPath": "github.com/ttacon/libphonenumber", - "Rev": "1197dfb91fa03c779576ad248ad5ca0a43fb3a31" - }, - { - "ImportPath": "golang.org/x/net/publicsuffix", - "Rev": "4876518f9e71663000c348837735820161a42df7" - }, - { - "ImportPath": "gopkg.in/resty.v0", - "Comment": "v0.6", - "Rev": "aed15a666de54595af7d8895aa7299ceff5635f8" - }, - { - "ImportPath": "gopkg.in/yaml.v2", - "Rev": "a83829b6f1293c91addabc89d0571c246397bbf4" - } - ] -} diff --git a/Godeps/Readme b/Godeps/Readme deleted file mode 100644 index 4cdaa53..0000000 --- a/Godeps/Readme +++ /dev/null @@ -1,5 +0,0 @@ -This directory tree is generated automatically by godep. - -Please do not edit. - -See https://github.com/tools/godep for more information. diff --git a/README.md b/README.md index db51008..743cbb4 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ client, _ := zendesk.FromEnv( zendesk.LoadConfiguration("./config/configuration.yml"), ) -u, err := client.ZendeskApi().CreateOrUpdateUser(user) +u, err := client.User().CreateOrUpdate(user) if err != nil { log.Println(err) @@ -51,135 +51,88 @@ if err != nil { ## User functions available -#### GetUser +#### Get user Return the user ``` -client.ZendeskApi().GetUser(1) +client.User().GetById(1) ``` -#### GetUsers +#### Get users Return the list of all users ``` -client.ZendeskApi().GetZendeskApi() +client.User().GetAll() ``` -#### GetUsersByGroup -Return the list of all users in a group +#### Get agents +Return the list of all agents ``` -client.ZendeskApi().GetUsersByGroup(4) +client.User().GetAllAgents(4) ``` -#### CreateOrUpdateUser +#### Create or update user Create or update a user ``` -client.ZendeskApi().CreateOrUpdateUser(user) +client.User().CreateOrUpdate(user) ``` -#### CreateUser +#### Create user Create a new user ``` -client.ZendeskApi().CreateUser(user) +client.User().Create(user) ``` -#### UpdateUser +#### Update user Update an existing user ``` -client.ZendeskApi().UpdateUser(user) +client.User().Update(user) ``` -#### DeleteUser +#### Delete user Delete an existing user ``` -client.ZendeskApi().DeleteUser(1) +client.User().Delete(1) ``` ## Ticket function available -#### GetTicket +#### Get ticket Return the ticket ``` -client.ZendeskApi().GetTicket(1) +client.Ticket().GetById(1) ``` -#### GetTickets +#### Get all tickets Return the list of all tickets ``` -client.ZendeskApi().GetTickets() +client.Ticket().GetAll() ``` -#### GetRecentTickets -Return the recent tickets - -``` -client.ZendeskApi().GetRecentTickets() -``` - -#### GetTicketsFromOrganization -Return the tickets from an organization - -``` -client.ZendeskApi().GetTicketsFromOrganization(10) -``` - -#### GetManyTickets -Return a list of tickets - -``` -client.ZendeskApi().GetManyTickets(int[]{1, 2, 3, 5}) -``` - -#### GetRequestedTicketsFromUser -Return the tickets requests by an user - -``` -client.ZendeskApi().GetRequestedTicketsFromUser(2) -``` - -#### GetCcdTicketsFromUser -``` -client.ZendeskApi().GetCcdTicketsFromUser(2) -``` - -#### GetAssignedTicketsFromUser -Return the tickets assigned from user - -``` -client.ZendeskApi().GetAssignedTicketsFromUser(2) -``` - -#### CreateTicket +#### Create ticket Create a new ticket ``` -client.ZendeskApi().CreateTicket(ticket) +client.Ticket().Create(ticket) ``` -#### UpdateTicket +#### Update ticket Update a ticket ``` -client.ZendeskApi().UpdateTicket(ticket) -``` - -#### UpdateTicketMarkAsSpam -Update a ticket to mark as spam - -``` -client.ZendeskApi().UpdateTicketMarkAsSpam(1) +client.Ticket().Create(ticket) ``` -#### DeleteTicket +#### Delete ticket Delete an existing ticket ``` -client.ZendeskApi().DeleteTicket(1) +client.Ticket().Delete(1) ``` \ No newline at end of file diff --git a/adapter.go b/adapter.go index 7d1cab1..eb38c86 100644 --- a/adapter.go +++ b/adapter.go @@ -1,6 +1,6 @@ package zendesk -import "gopkg.in/resty.v0" +import "github.com/go-resty/resty/v2" type ZendeskApi interface { GetById(id int) (interface{}, error) @@ -14,4 +14,4 @@ type ZendeskApi interface { // Parse response parseSingleObject(response *resty.Response) interface{} parseMultiObjects(response *resty.Response) []interface{} -} \ No newline at end of file +} diff --git a/builder.go b/builder.go index 99ad46a..2ed5ba6 100644 --- a/builder.go +++ b/builder.go @@ -2,23 +2,23 @@ package zendesk import ( "fmt" - "io/ioutil" - + "github.com/go-resty/resty/v2" "gopkg.in/yaml.v2" - "gopkg.in/resty.v0" + "io/ioutil" ) func FromToken(config ZendeskConfiguration) Client { username := fmt.Sprintf("%s/token", config.Email) - restyClient := resty.SetBasicAuth(username, config.Token) + restyClient := resty.New() + restyClient.SetBasicAuth(username, config.Token) restyClient.SetHeader("Accept", "application/json") restyClient.SetHeader("Content-Type", "application/json") return Client{ - domain: config.Domain, + domain: config.Domain, apiVersion: config.ApiVersion, - client: restyClient, + client: restyClient, } } diff --git a/client.go b/client.go index 426bb1c..6b58c5e 100644 --- a/client.go +++ b/client.go @@ -1,14 +1,14 @@ package zendesk import ( - "gopkg.in/resty.v0" - "fmt" "encoding/json" + "fmt" + "github.com/go-resty/resty/v2" ) type Client struct { - domain string - client *resty.Client + domain string + client *resty.Client apiVersion string } @@ -20,6 +20,14 @@ func (c Client) Ticket() TicketApiHandler { return TicketApiHandler{c} } +func (c Client) PhoneNumber() PhoneNumberApiHandler { + return PhoneNumberApiHandler{c} +} + +func (c Client) Comment() CommentApiHandler { + return CommentApiHandler{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 +68,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/comment.go b/comment.go new file mode 100644 index 0000000..e8636c3 --- /dev/null +++ b/comment.go @@ -0,0 +1,35 @@ +package zendesk + +import ( + "fmt" + "github.com/go-resty/resty/v2" +) + +type CommentApiHandler struct { + client Client +} + +type MultipleComment struct { + Response []Comment `json:"comments"` +} + +func (c CommentApiHandler) GetForTicket(ticketId int) ([]Comment, error) { + response, err := c.client.get( + fmt.Sprintf("/tickets/%d/comments.json", ticketId), + nil, + ) + + if err != nil { + return nil, err + } + + return c.parseMultiObjects(response), err +} + +func (c CommentApiHandler) parseMultiObjects(response *resty.Response) []Comment { + var object MultipleComment + + c.client.parseResponseToInterface(response, &object) + + return object.Response +} diff --git a/comment_struct.go b/comment_struct.go new file mode 100644 index 0000000..a180751 --- /dev/null +++ b/comment_struct.go @@ -0,0 +1,7 @@ +package zendesk + +type Comment struct { + Id int64 `json:"id,omitempty"` + AuthorId int64 `json:"author_id,omitempty"` + CreatedAt string `json:"created_at,omitempty"` +} diff --git a/global_struct.go b/global_struct.go index e1a7e70..7801c1e 100644 --- a/global_struct.go +++ b/global_struct.go @@ -20,4 +20,4 @@ type JobStatus struct { Status string `json:"status,omitempty"` Message string `json:"mesage,omitempty"` Results string `json:"results,omitempty"` -} \ No newline at end of file +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c2aa826 --- /dev/null +++ b/go.mod @@ -0,0 +1,17 @@ +module github.com/gomaps/zendesk-go + +go 1.18 + +require ( + github.com/go-resty/resty/v2 v2.7.0 + github.com/ttacon/libphonenumber v1.2.1 + gopkg.in/yaml.v2 v2.4.0 +) + +require ( + github.com/go-resty/resty/v2 v2.7.0 + github.com/golang/protobuf v1.5.2 // indirect + github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect + golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect + google.golang.org/protobuf v1.26.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..88ee664 --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= +github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0XS0qTf5FznsMOzTjGqavBGuCbo0= +github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2/go.mod h1:4kyMkleCiLkgY6z8gK5BkI01ChBtxR0ro3I1ZDcGM3w= +github.com/ttacon/libphonenumber v1.2.1 h1:fzOfY5zUADkCkbIafAed11gL1sW+bJ26p6zWLBMElR4= +github.com/ttacon/libphonenumber v1.2.1/go.mod h1:E0TpmdVMq5dyVlQ7oenAkhsLu86OkUl+yR4OAxyEg/M= +golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM= +golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/phone_number.go b/phone_number.go new file mode 100644 index 0000000..1cdf812 --- /dev/null +++ b/phone_number.go @@ -0,0 +1,32 @@ +package zendesk + +import "github.com/go-resty/resty/v2" + +type PhoneNumberApiHandler struct { + client Client +} + +type MultiplePhoneNumber struct { + Response []PhoneNumber `json:"phone_numbers"` +} + +func (p PhoneNumberApiHandler) GetAll() ([]PhoneNumber, error) { + response, err := p.client.get( + "/channels/voice/phone_numbers.json", + map[string]string{"minimal_mode": "true"}, + ) + + if err != nil { + return nil, err + } + + return p.parseMultiObjects(response), err +} + +func (p PhoneNumberApiHandler) parseMultiObjects(response *resty.Response) []PhoneNumber { + var object MultiplePhoneNumber + + p.client.parseResponseToInterface(response, &object) + + return object.Response +} diff --git a/phone_number_struct.go b/phone_number_struct.go new file mode 100644 index 0000000..eeb1d5a --- /dev/null +++ b/phone_number_struct.go @@ -0,0 +1,7 @@ +package zendesk + +type PhoneNumber struct { + Id int64 `json:"id,omitempty"` + DisplayNumber string `json:"display_number,omitempty"` + Nickname string `json:"nickname,omitempty"` +} diff --git a/test/zendesk_test.go b/test/zendesk_test.go index d3eba4b..7481dd7 100644 --- a/test/zendesk_test.go +++ b/test/zendesk_test.go @@ -1,13 +1,15 @@ package test import ( + "fmt" "testing" - "github.com/hellofresh/zendesk-go" + "time" + "zendesk-go" ) var client = zendesk.FromToken( zendesk.LoadConfiguration("./../config/configuration.yml"), -); +) var id int @@ -22,7 +24,7 @@ func TestUserApiHandler_GetAll(t *testing.T) { } for _, user := range users { - id = user.Id + id = int(user.Id) break } } @@ -36,9 +38,18 @@ func TestUserApiHandler_GetById(t *testing.T) { } } +func TestUserApiHandler_GetAllAgents(t *testing.T) { + _, err := client.User().GetAllAgents() + + if err != nil { + t.Errorf("Error: %s", err) + t.Fail() + } +} + func TestUserApiHandler_Create(t *testing.T) { user := zendesk.User{ - Name: "Felipe Pieretti Umpierre", + Name: "Felipe Pieretti Umpierre", Email: "fum@hellofresh.com", } @@ -52,7 +63,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 +79,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", }) @@ -94,10 +105,32 @@ func TestUserApiHandler_Delete(t *testing.T) { } } +func TestUserApiHandler_Merge(t *testing.T) { + user := zendesk.User{ + Name: "User 1", + Email: fmt.Sprintf("test-%d@hellofresh.com", time.Now().Unix()), + } + newUser, err := client.User().CreateOrUpdate(user) + + user2 := zendesk.User{ + Name: "User 2", + Phone: "(415) 123-4567", + } + + userToKeep, err := client.User().CreateOrUpdate(user2) + + _, err = client.User().Merge(int(newUser.Id), userToKeep) + + if err != nil { + t.Errorf("Error: %s", err) + t.Fail() + } +} + func TestUserApiHandler_Update(t *testing.T) { user := zendesk.User{ - Id: id, - Name: "Felipe Pieretti Umpierre - hallo", + Id: int64(id), + Name: "Felipe Pieretti Umpierre - hallo", Email: "fum@hellofresh.com", } @@ -171,7 +204,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 +214,31 @@ func TestTicketApiHandler_Update(t *testing.T) { t.Errorf("Error: %s", err) t.Fail() } -} \ No newline at end of file +} + +func TestTicketApiHandler_GetRequestedByUser(t *testing.T) { + _, err := client.Ticket().GetRequestedByUser(0) + + if err != nil { + t.Errorf("Error: %s", err) + t.Fail() + } +} + +func TestCommentApiHandler_GetForTicket(t *testing.T) { + _, err := client.Comment().GetForTicket(0) + + if err != nil { + t.Errorf("Error: %s", err) + t.Fail() + } +} + +func TestPhoneNumberApiHandler_GetAll(t *testing.T) { + _, err := client.PhoneNumber().GetAll() + + if err != nil { + t.Errorf("Error: %s", err) + t.Fail() + } +} diff --git a/ticket.go b/ticket.go index dcf4a5a..ed5cf19 100644 --- a/ticket.go +++ b/ticket.go @@ -2,7 +2,7 @@ package zendesk import ( "fmt" - "gopkg.in/resty.v0" + "github.com/go-resty/resty/v2" ) type TicketApiHandler struct { @@ -24,7 +24,6 @@ func (t TicketApiHandler) GetById(id int) (Ticket, error) { ) if err != nil { - } return t.parseSingleObject(response), err @@ -37,7 +36,19 @@ func (t TicketApiHandler) GetAll() ([]Ticket, error) { ) if err != nil { + } + + return t.parseMultiObjects(response), err +} +func (t TicketApiHandler) GetRequestedByUser(userId int64) ([]Ticket, error) { + response, err := t.client.get( + fmt.Sprintf("/users/%d/tickets/requested.json", userId), + nil, + ) + + if err != nil { + return nil, err } return t.parseMultiObjects(response), err @@ -97,4 +108,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..69295f5 100644 --- a/ticket_struct.go +++ b/ticket_struct.go @@ -3,34 +3,38 @@ package zendesk type Via struct{} 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 int64 `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 []CustomField `json:"custom_fields,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"` +} + +type CustomField struct { + ID int `json:"id"` + Value interface{} `json:"value"` } diff --git a/user.go b/user.go index cafeb53..a9d5436 100644 --- a/user.go +++ b/user.go @@ -2,7 +2,7 @@ package zendesk import ( "fmt" - "gopkg.in/resty.v0" + "github.com/go-resty/resty/v2" ) type UserApiHandler struct { @@ -43,6 +43,34 @@ func (u UserApiHandler) GetAll() ([]User, error) { return u.parseMultiObjects(response), err } +func (u UserApiHandler) GetAllAgents() ([]User, error) { + response, err := u.client.get( + "/users.json", + map[string]string{"role": "agent"}, + ) + + if err != nil { + return nil, err + } + + users := u.parseMultiObjects(response) + + // This could be done in a single API call with role[]=agent&role[]=admin but the current interface makes doing this difficult + response, err = u.client.get( + "/users.json", + map[string]string{"role": "admin"}, + ) + + if err != nil { + return nil, err + } + + adminUsers := u.parseMultiObjects(response) + users = append(users, adminUsers...) + + return users, err +} + func (u UserApiHandler) Create(v User) (User, error) { var object SingleUser @@ -99,6 +127,18 @@ func (u UserApiHandler) Delete(id int) (int, error) { return response.StatusCode(), err } +func (u UserApiHandler) Merge(userIdToMerge int, userToKeep User) (User, error) { + var object SingleUser + + _, err := u.client.put( + fmt.Sprintf("/users/%d/merge.json", userIdToMerge), + map[string]User{"user": userToKeep}, + &object, + ) + + return object.Response, err +} + func (u UserApiHandler) parseMultiObjects(response *resty.Response) []User { var object MultipleUser @@ -113,4 +153,4 @@ func (u UserApiHandler) parseSingleObject(response *resty.Response) User { u.client.parseResponseToInterface(response, &object) return object.Response -} \ No newline at end of file +} diff --git a/user_struct.go b/user_struct.go index 27a853c..a124c09 100644 --- a/user_struct.go +++ b/user_struct.go @@ -10,36 +10,36 @@ type ManyUsers struct { } type User struct { - Id int`json:"id,omitempty"` - Url string `json:"url,omitempty"` - Name string `json:"name,omitempty"` - ExternalId string `json:"external_id,omitempty"` - Alias string `json:"alias,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - UpdatedAt string `json:"updated_at,omitempty"` - Active bool `json:"active,omitempty"` - Verified bool `json:"verified,omitempty"` - Shared bool `json:"shared,omitempty"` - SharedAgent bool `json:"shared_agent,omitempty"` - Locale string `json:"locale,omitempty"` - LocaleId int `json:"locale_id,omitempty"` - TimeZone string `json:"time_zone,omitempty"` - LastLoginAt string `json:"last_login_at,omitempty"` - Email string `json:"email,omitempty"` - Phone string `json:"phone,omitempty"` - Signature string `json:"signature,omitempty"` - Details string `json:"details,omitempty"` - Notes string `json:"notes,omitempty"` - OrganizationId int `json:"organization_id,omitempty"` - Role string `json:"role,omitempty"` - CustomRoleId string `json:"custom_role_id,omitempty"` - Moderator bool `json:"moderator,omitempty"` - TicketRestriction string `json:"ticket_restriction,omitempty"` - OnlyPrivateComments bool `json:"only_private_comments,omitempty"` - Tags []string `json:"tags,omitempty"` - Suspended bool `json:"suspended,omitempty"` - RestrictedAgent bool `json:"restricted_agent,omitempty"` - Photo *Attachment `json:"photo,omitempty"` + Id int64 `json:"id,omitempty"` + Url string `json:"url,omitempty"` + Name string `json:"name,omitempty"` + ExternalId string `json:"external_id,omitempty"` + Alias string `json:"alias,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` + Active bool `json:"active,omitempty"` + Verified bool `json:"verified,omitempty"` + Shared bool `json:"shared,omitempty"` + SharedAgent bool `json:"shared_agent,omitempty"` + Locale string `json:"locale,omitempty"` + LocaleId int `json:"locale_id,omitempty"` + TimeZone string `json:"time_zone,omitempty"` + LastLoginAt string `json:"last_login_at,omitempty"` + Email string `json:"email,omitempty"` + Phone string `json:"phone,omitempty"` + Signature string `json:"signature,omitempty"` + Details string `json:"details,omitempty"` + Notes string `json:"notes,omitempty"` + OrganizationId int `json:"organization_id,omitempty"` + Role string `json:"role,omitempty"` + CustomRoleId int `json:"custom_role_id,omitempty"` + Moderator bool `json:"moderator,omitempty"` + TicketRestriction string `json:"ticket_restriction,omitempty"` + OnlyPrivateComments bool `json:"only_private_comments,omitempty"` + Tags []string `json:"tags,omitempty"` + Suspended bool `json:"suspended,omitempty"` + RestrictedAgent bool `json:"restricted_agent,omitempty"` + Photo *Attachment `json:"photo,omitempty"` UserFields map[string]interface{} `json:"user_fields,omitempty"` } @@ -56,4 +56,4 @@ func (users *ManyUsers) AppendUsers(user User) []User { users.Users = append(users.Users, user) return users.Users -} \ No newline at end of file +}