Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context support for Group API #109

Merged
merged 4 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ $ go get github.com/nukosuke/go-zendesk
package main

import (
"context"

"github.com/nukosuke/go-zendesk/zendesk"
)

Expand All @@ -39,7 +41,7 @@ func main() {
client.SetCredential(zendesk.NewBasicAuthCredential("john.doe@example.com", "password"))

// Create resource
client.CreateGroup(zendesk.Group{
client.CreateGroup(context.Background(), zendesk.Group{
Name: "support team",
})
}
Expand Down
31 changes: 16 additions & 15 deletions zendesk/group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zendesk

import (
"context"
"encoding/json"
"fmt"
"time"
Expand All @@ -19,22 +20,22 @@ type Group struct {

// GroupAPI an interface containing all methods associated with zendesk groups
type GroupAPI interface {
GetGroups() ([]Group, Page, error)
GetGroup(groupID int64) (Group, error)
CreateGroup(group Group) (Group, error)
UpdateGroup(groupID int64, group Group) (Group, error)
DeleteGroup(groupID int64) error
GetGroups(ctx context.Context) ([]Group, Page, error)
GetGroup(ctx context.Context, groupID int64) (Group, error)
CreateGroup(ctx context.Context, group Group) (Group, error)
UpdateGroup(ctx context.Context, groupID int64, group Group) (Group, error)
DeleteGroup(ctx context.Context, groupID int64) error
}

// GetGroups fetches group list
// https://developer.zendesk.com/rest_api/docs/support/groups#list-groups
func (z *Client) GetGroups() ([]Group, Page, error) {
func (z *Client) GetGroups(ctx context.Context) ([]Group, Page, error) {
var data struct {
Groups []Group `json:"groups"`
Page
}

body, err := z.Get("/groups.json")
body, err := z.get(ctx, "/groups.json")
if err != nil {
return []Group{}, Page{}, err
}
Expand All @@ -48,13 +49,13 @@ func (z *Client) GetGroups() ([]Group, Page, error) {

// CreateGroup creates new group
// https://developer.zendesk.com/rest_api/docs/support/groups#create-group
func (z *Client) CreateGroup(group Group) (Group, error) {
func (z *Client) CreateGroup(ctx context.Context, group Group) (Group, error) {
var data, result struct {
Group Group `json:"group"`
}
data.Group = group

body, err := z.Post("/groups.json", data)
body, err := z.post(ctx, "/groups.json", data)
if err != nil {
return Group{}, err
}
Expand All @@ -68,12 +69,12 @@ func (z *Client) CreateGroup(group Group) (Group, error) {

// GetGroup gets a specified group
// ref: https://developer.zendesk.com/rest_api/docs/support/groups#show-group
func (z *Client) GetGroup(groupID int64) (Group, error) {
func (z *Client) GetGroup(ctx context.Context, groupID int64) (Group, error) {
var result struct {
Group Group `json:"group"`
}

body, err := z.Get(fmt.Sprintf("/groups/%d.json", groupID))
body, err := z.get(ctx, fmt.Sprintf("/groups/%d.json", groupID))

if err != nil {
return Group{}, err
Expand All @@ -89,13 +90,13 @@ func (z *Client) GetGroup(groupID int64) (Group, error) {

// UpdateGroup updates a group with the specified group
// ref: https://developer.zendesk.com/rest_api/docs/support/groups#update-group
func (z *Client) UpdateGroup(groupID int64, group Group) (Group, error) {
func (z *Client) UpdateGroup(ctx context.Context, groupID int64, group Group) (Group, error) {
var result, data struct {
Group Group `json:"group"`
}
data.Group = group

body, err := z.Put(fmt.Sprintf("/groups/%d.json", groupID), data)
body, err := z.put(ctx, fmt.Sprintf("/groups/%d.json", groupID), data)

if err != nil {
return Group{}, err
Expand All @@ -111,8 +112,8 @@ func (z *Client) UpdateGroup(groupID int64, group Group) (Group, error) {

// DeleteGroup deletes the specified group
// ref: https://developer.zendesk.com/rest_api/docs/support/groups#delete-group
func (z *Client) DeleteGroup(groupID int64) error {
err := z.Delete(fmt.Sprintf("/groups/%d.json", groupID))
func (z *Client) DeleteGroup(ctx context.Context, groupID int64) error {
err := z.delete(ctx, fmt.Sprintf("/groups/%d.json", groupID))

if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions zendesk/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestGetGroups(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

groups, _, err := client.GetGroups()
groups, _, err := client.GetGroups(ctx)
if err != nil {
t.Fatalf("Failed to get groups: %s", err)
}
Expand All @@ -26,7 +26,7 @@ func TestCreateGroup(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

_, err := client.CreateGroup(Group{})
_, err := client.CreateGroup(ctx, Group{})
if err != nil {
t.Fatalf("Failed to send request to create group: %s", err)
}
Expand All @@ -37,7 +37,7 @@ func TestGetGroup(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

group, err := client.GetGroup(123)
group, err := client.GetGroup(ctx, 123)
if err != nil {
t.Fatalf("Failed to get group: %s", err)
}
Expand All @@ -53,7 +53,7 @@ func TestUpdateGroup(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

updatedGroup, err := client.UpdateGroup(int64(1234), Group{})
updatedGroup, err := client.UpdateGroup(ctx, int64(1234), Group{})
if err != nil {
t.Fatalf("Failed to send request to create group: %s", err)
}
Expand All @@ -71,7 +71,7 @@ func TestDeleteGroup(t *testing.T) {
}))

c := newTestClient(mockAPI)
err := c.DeleteGroup(1234)
err := c.DeleteGroup(ctx, 1234)
if err != nil {
t.Fatalf("Failed to delete group: %s", err)
}
Expand Down
40 changes: 20 additions & 20 deletions zendesk/mock/client.go

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