forked from gbrlsnchs/terraform-provider-zendesk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from clearnote01/refactor-client-extension
setup list support in macros
- Loading branch information
Showing
5 changed files
with
180 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/nukosuke/terraform-provider-zendesk/zendesk/models" | ||
|
||
"github.com/nukosuke/go-zendesk/zendesk" | ||
) | ||
|
||
// MacroAPI an interface containing all macro related methods | ||
type MacroAPI interface { | ||
GetMacros(ctx context.Context, options *zendesk.MacroListOptions) ([]models.Macro, zendesk.Page, error) | ||
CreateMacro(ctx context.Context, macro models.Macro) (models.Macro, error) | ||
DeleteMacro(ctx context.Context, id int64) error | ||
UpdateMacro(ctx context.Context, id int64, form models.Macro) (models.Macro, error) | ||
GetMacro(ctx context.Context, id int64) (models.Macro, error) | ||
} | ||
|
||
// GetMacros fetches macros | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#list-macros | ||
func (z *Client) GetMacros(ctx context.Context, options *zendesk.MacroListOptions) ([]models.Macro, zendesk.Page, error) { | ||
var data struct { | ||
Macros []models.Macro `json:"macros"` | ||
zendesk.Page | ||
} | ||
|
||
tmp := options | ||
if tmp == nil { | ||
tmp = &zendesk.MacroListOptions{} | ||
} | ||
|
||
u, err := addOptions("/macros.json", tmp) | ||
if err != nil { | ||
return nil, zendesk.Page{}, err | ||
} | ||
|
||
body, err := z.Get(ctx, u) | ||
if err != nil { | ||
return []models.Macro{}, zendesk.Page{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &data) | ||
if err != nil { | ||
return []models.Macro{}, zendesk.Page{}, err | ||
} | ||
return data.Macros, data.Page, nil | ||
} | ||
|
||
// CreateMacro creates new macro | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#create-macros | ||
func (z *Client) CreateMacro(ctx context.Context, macro models.Macro) (models.Macro, error) { | ||
var data, result struct { | ||
Macro models.Macro `json:"macro"` | ||
} | ||
data.Macro = macro | ||
|
||
body, err := z.Post(ctx, "/macros.json", data) | ||
if err != nil { | ||
return models.Macro{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return models.Macro{}, err | ||
} | ||
return result.Macro, nil | ||
} | ||
|
||
// GetMacro returns the specified macro | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#show-macro | ||
func (z *Client) GetMacro(ctx context.Context, id int64) (models.Macro, error) { | ||
var result struct { | ||
Macro models.Macro `json:"macro"` | ||
} | ||
|
||
body, err := z.Get(ctx, fmt.Sprintf("/macros/%d.json", id)) | ||
if err != nil { | ||
return models.Macro{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return models.Macro{}, err | ||
} | ||
return result.Macro, nil | ||
} | ||
|
||
// UpdateMacro updates the specified macro and returns the updated form | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#update-macros | ||
func (z *Client) UpdateMacro(ctx context.Context, id int64, form models.Macro) (models.Macro, error) { | ||
var data, result struct { | ||
Macro models.Macro `json:"macro"` | ||
} | ||
|
||
data.Macro = form | ||
body, err := z.Put(ctx, fmt.Sprintf("/macros/%d.json", id), data) | ||
if err != nil { | ||
return models.Macro{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return models.Macro{}, err | ||
} | ||
|
||
return result.Macro, nil | ||
} | ||
|
||
// DeleteMacro deletes the specified macro | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/macro#delete-macro | ||
func (z *Client) DeleteMacro(ctx context.Context, id int64) error { | ||
err := z.Delete(ctx, fmt.Sprintf("/macros/%d.json", id)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type Macro struct { | ||
Actions []MacroAction `json:"actions"` | ||
Active bool `json:"active"` | ||
CreatedAt time.Time `json:"created_at,omitempty"` | ||
Description interface{} `json:"description"` | ||
ID int64 `json:"id,omitempty"` | ||
Position int `json:"position,omitempty"` | ||
Restriction interface{} `json:"restriction"` | ||
Title string `json:"title"` | ||
UpdatedAt time.Time `json:"updated_at,omitempty"` | ||
URL string `json:"url,omitempty"` | ||
} | ||
|
||
type MacroAction struct { | ||
Field string `json:"field"` | ||
Value interface{} `json:"value"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters