Skip to content

Commit

Permalink
Impl GetBrands
Browse files Browse the repository at this point in the history
  • Loading branch information
nukosuke committed Mar 15, 2019
1 parent a5b6689 commit dce4936
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions zendesk/brand.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ type BrandAPI interface {
CreateBrand(brand Brand) (Brand, error)
}

// GetBrands fetches brand list
// https://developer.zendesk.com/rest_api/docs/support/brands#list-brands
func (z *Client) GetBrands() ([]Brand, Page, error) {
var data struct {
Brands []Brand `json:"brands"`
Page
}

body, err := z.Get("/brands.json")
if err != nil {
return []Brand{}, Page{}, err
}

err = json.Unmarshal(body, &data)
if err != nil {
return []Brand{}, Page{}, err
}
return data.Brands, data.Page, nil
}

// CreateBrand creates new brand
// https://developer.zendesk.com/rest_api/docs/support/brands#create-brand
func (z *Client) CreateBrand(brand Brand) (Brand, error) {
Expand Down
15 changes: 15 additions & 0 deletions zendesk/brand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import (
"testing"
)

func TestGetBrands(t *testing.T) {
mockAPI := newMockAPI(http.MethodGet, "brands.json")
client := newTestClient(mockAPI)
defer mockAPI.Close()

brands, _, err := client.GetBrands()
if err != nil {
t.Fatalf("Failed to get brands: %s", err)
}

if len(brands) != 2 {
t.Fatalf("expected length of brands is 2, but got %d", len(brands))
}
}

func TestCreateBrand(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPost, "brands.json", http.StatusCreated)
client := newTestClient(mockAPI)
Expand Down

0 comments on commit dce4936

Please sign in to comment.