Skip to content

Commit

Permalink
Merge pull request #225 from nukosuke/work/missing-method-for-dynamic…
Browse files Browse the repository at this point in the history
…-content

Add missing method for dynamic content item
  • Loading branch information
nukosuke authored Apr 19, 2022
1 parent a080692 commit f8e55e7
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 186 deletions.
57 changes: 57 additions & 0 deletions zendesk/dynamic_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package zendesk
import (
"context"
"encoding/json"
"fmt"
"time"
)

// DynamicContentAPI an interface containing all methods associated with zendesk dynamic content
type DynamicContentAPI interface {
GetDynamicContentItems(ctx context.Context) ([]DynamicContentItem, Page, error)
CreateDynamicContentItem(ctx context.Context, item DynamicContentItem) (DynamicContentItem, error)
GetDynamicContentItem(ctx context.Context, id int64) (DynamicContentItem, error)
UpdateDynamicContentItem(ctx context.Context, id int64, item DynamicContentItem) (DynamicContentItem, error)
DeleteDynamicContentItem(ctx context.Context, id int64) error
}

// DynamicContentItem is zendesk dynamic content item JSON payload format
Expand Down Expand Up @@ -83,3 +87,56 @@ func (z *Client) CreateDynamicContentItem(ctx context.Context, item DynamicConte
}
return result.Item, nil
}

// GetDynamicContentItem returns the specified dynamic content item.
//
// ref: https://developer.zendesk.com/api-reference/ticketing/ticket-management/dynamic_content/#show-item
func (z *Client) GetDynamicContentItem(ctx context.Context, id int64) (DynamicContentItem, error) {
var result struct {
Item DynamicContentItem `json:"item"`
}

body, err := z.get(ctx, fmt.Sprintf("/dynamic_content/items/%d.json", id))
if err != nil {
return DynamicContentItem{}, err
}

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

return result.Item, nil
}

// UpdateDynamicContentItem updates the specified dynamic content item and returns the updated one
//
// ref: https://developer.zendesk.com/api-reference/ticketing/ticket-management/dynamic_content/#update-item
func (z *Client) UpdateDynamicContentItem(ctx context.Context, id int64, item DynamicContentItem) (DynamicContentItem, error) {
var data, result struct {
Item DynamicContentItem `json:"item"`
}
data.Item = item

body, err := z.put(ctx, fmt.Sprintf("/dynamic_content/items/%d.json", id), data)
if err != nil {
return DynamicContentItem{}, err
}

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

return result.Item, nil
}

// DeleteDynamicContentItem deletes the specified dynamic content item.
//
// ref: https://developer.zendesk.com/api-reference/ticketing/ticket-management/dynamic_content/#delete-item
func (z *Client) DeleteDynamicContentItem(ctx context.Context, id int64) error {
err := z.delete(ctx, fmt.Sprintf("/dynamic_content/items/%d.json", id))
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit f8e55e7

Please sign in to comment.