Skip to content

Commit

Permalink
Merge 948ce49 into 865066e
Browse files Browse the repository at this point in the history
  • Loading branch information
nukosuke authored Mar 19, 2019
2 parents 865066e + 948ce49 commit 7b13358
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 0 deletions.
53 changes: 53 additions & 0 deletions fixture/GET/brand.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"brand":
{
"url": "https://example.zendesk.com/api/v2/brands/360002143133.json",
"id": 360002143133,
"name": "brand2",
"brand_url": "https://example-brand2.zendesk.com",
"subdomain": "example-brand2",
"has_help_center": false,
"help_center_state": "disabled",
"active": true,
"default": false,
"is_deleted": false,
"logo": {
"url": "https://example.zendesk.com/api/v2/attachments/360000142813.json",
"id": 360000142813,
"file_name": "zendesk_logo.jpeg",
"content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo.jpeg",
"mapped_content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo.jpeg",
"content_type": "image/jpeg",
"size": 4060,
"width": 80,
"height": 80,
"inline": false,
"thumbnails": [
{
"url": "https://example.zendesk.com/api/v2/attachments/360000142833.json",
"id": 360000142833,
"file_name": "zendesk_logo_thumb.jpeg",
"content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_thumb.jpeg",
"mapped_content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_thumb.jpeg",
"content_type": "image/jpeg",
"size": 2040,
"width": 32,
"height": 32,
"inline": false
},
{
"url": "https://example.zendesk.com/api/v2/attachments/360000142853.json",
"id": 360000142853,
"file_name": "zendesk_logo_small.jpeg",
"content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_small.jpeg",
"mapped_content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_small.jpeg",
"content_type": "image/jpeg",
"size": 1322,
"width": 24,
"height": 24,
"inline": false
}
]
}
}
}
53 changes: 53 additions & 0 deletions fixture/PUT/brands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"brand":
{
"url": "https://example.zendesk.com/api/v2/brands/360002143133.json",
"id": 360002143133,
"name": "brand2",
"brand_url": "https://example-brand2.zendesk.com",
"subdomain": "example-brand2",
"has_help_center": false,
"help_center_state": "disabled",
"active": true,
"default": false,
"is_deleted": false,
"logo": {
"url": "https://example.zendesk.com/api/v2/attachments/360000142813.json",
"id": 360000142813,
"file_name": "zendesk_logo.jpeg",
"content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo.jpeg",
"mapped_content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo.jpeg",
"content_type": "image/jpeg",
"size": 4060,
"width": 80,
"height": 80,
"inline": false,
"thumbnails": [
{
"url": "https://example.zendesk.com/api/v2/attachments/360000142833.json",
"id": 360000142833,
"file_name": "zendesk_logo_thumb.jpeg",
"content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_thumb.jpeg",
"mapped_content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_thumb.jpeg",
"content_type": "image/jpeg",
"size": 2040,
"width": 32,
"height": 32,
"inline": false
},
{
"url": "https://example.zendesk.com/api/v2/attachments/360000142853.json",
"id": 360000142853,
"file_name": "zendesk_logo_small.jpeg",
"content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_small.jpeg",
"mapped_content_url": "https://example.zendesk.com/system/brands/3600/0014/2813/zendesk_logo_small.jpeg",
"content_type": "image/jpeg",
"size": 1322,
"width": 24,
"height": 24,
"inline": false
}
]
}
}
}
60 changes: 60 additions & 0 deletions zendesk/brand.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zendesk

import (
"encoding/json"
"fmt"
"time"
)

Expand All @@ -28,6 +29,9 @@ type Brand struct {
// BrandAPI an interface containing all methods associated with zendesk brands
type BrandAPI interface {
CreateBrand(brand Brand) (Brand, error)
GetBrand(brandID int64) (Brand, error)
UpdateBrand(brandID int64, brand Brand) (Brand, error)
DeleteBrand(brandID int64) error
}

// CreateBrand creates new brand
Expand All @@ -49,3 +53,59 @@ func (z *Client) CreateBrand(brand Brand) (Brand, error) {
}
return result.Brand, nil
}

// GetBrand gets a specified brand
// ref: https://developer.zendesk.com/rest_api/docs/support/brands#show-brand
func (z *Client) GetBrand(brandID int64) (Brand, error) {
var result struct {
Brand Brand `json:"brand"`
}

body, err := z.Get(fmt.Sprintf("/brands/%d.json", brandID))

if err != nil {
return Brand{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return Brand{}, err
}

return result.Brand, err
}

// UpdateBrand updates a brand with the specified brand
// ref: https://developer.zendesk.com/rest_api/docs/support/brands#update-brand
func (z *Client) UpdateBrand(brandID int64, brand Brand) (Brand, error) {
var result, data struct {
Brand Brand `json:"brand"`
}

data.Brand = brand

body, err := z.Put(fmt.Sprintf("/brands/%d.json", brandID), data)

if err != nil {
return Brand{}, err
}

err = json.Unmarshal(body, &result)
if err != nil {
return Brand{}, err
}

return result.Brand, err
}

// DeleteBrand deletes the specified brand
// ref: https://developer.zendesk.com/rest_api/docs/support/brands#delete-brand
func (z *Client) DeleteBrand(brandID int64) error {
err := z.Delete(fmt.Sprintf("/brands/%d.json", brandID))

if err != nil {
return err
}

return nil
}
46 changes: 46 additions & 0 deletions zendesk/brand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zendesk

import (
"net/http"
"net/http/httptest"
"testing"
)

Expand All @@ -15,3 +16,48 @@ func TestCreateBrand(t *testing.T) {
t.Fatalf("Failed to send request to create brand: %s", err)
}
}

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

brand, err := client.GetBrand(123)
if err != nil {
t.Fatalf("Failed to get brand: %s", err)
}

expectedID := int64(360002143133)
if brand.ID != expectedID {
t.Fatalf("Returned brand does not have the expected ID %d. Brand ID is %d", expectedID, brand.ID)
}
}

func TestUpdateBrand(t *testing.T) {
mockAPI := newMockAPIWithStatus(http.MethodPut, "brands.json", http.StatusOK)
client := newTestClient(mockAPI)
defer mockAPI.Close()

updatedBrand, err := client.UpdateBrand(int64(1234), Brand{})
if err != nil {
t.Fatalf("Failed to send request to create brand: %s", err)
}

expectedID := int64(360002143133)
if updatedBrand.ID != expectedID {
t.Fatalf("Updated brand %v did not have expected id %d", updatedBrand, expectedID)
}
}

func TestDeleteBrand(t *testing.T) {
mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
w.Write(nil)
}))

c := newTestClient(mockAPI)
err := c.DeleteBrand(1234)
if err != nil {
t.Fatalf("Failed to delete brand: %s", err)
}
}
44 changes: 44 additions & 0 deletions zendesk/mock/client.go

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

0 comments on commit 7b13358

Please sign in to comment.