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.
- Loading branch information
1 parent
9e4febf
commit 09ac4ea
Showing
6 changed files
with
213 additions
and
30 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,31 @@ | ||
package client | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/google/go-querystring/query" | ||
"github.com/nukosuke/go-zendesk/zendesk" | ||
) | ||
|
||
type ( | ||
Client struct { | ||
// use struct embedding for extension | ||
zendesk.Client | ||
} | ||
) | ||
|
||
// addOptions build query string | ||
func addOptions(s string, opts interface{}) (string, error) { | ||
u, err := url.Parse(s) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
qs, err := query.Values(opts) | ||
if err != nil { | ||
return s, err | ||
} | ||
|
||
u.RawQuery = qs.Encode() | ||
return u.String(), 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,121 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/nukosuke/terraform-provider-zendesk/zendesk/models" | ||
|
||
"github.com/nukosuke/go-zendesk/zendesk" | ||
) | ||
|
||
// TicketFormAPI an interface containing all ticket form related methods | ||
type TicketFormAPI interface { | ||
GetTicketForms(ctx context.Context, options *zendesk.TicketFormListOptions) ([]models.TicketForm, zendesk.Page, error) | ||
CreateTicketForm(ctx context.Context, ticketForm models.TicketForm) (models.TicketForm, error) | ||
DeleteTicketForm(ctx context.Context, id int64) error | ||
UpdateTicketForm(ctx context.Context, id int64, form models.TicketForm) (models.TicketForm, error) | ||
GetTicketForm(ctx context.Context, id int64) (models.TicketForm, error) | ||
} | ||
|
||
// GetTicketForms fetches ticket forms | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_forms#list-ticket-forms | ||
func (z *Client) GetTicketForms(ctx context.Context, options *zendesk.TicketFormListOptions) ([]models.TicketForm, zendesk.Page, error) { | ||
var data struct { | ||
TicketForms []models.TicketForm `json:"ticket_forms"` | ||
zendesk.Page | ||
} | ||
|
||
tmp := options | ||
if tmp == nil { | ||
tmp = &zendesk.TicketFormListOptions{} | ||
} | ||
|
||
u, err := addOptions("/ticket_forms.json", tmp) | ||
if err != nil { | ||
return nil, zendesk.Page{}, err | ||
} | ||
|
||
body, err := z.Get(ctx, u) | ||
if err != nil { | ||
return []models.TicketForm{}, zendesk.Page{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &data) | ||
if err != nil { | ||
return []models.TicketForm{}, zendesk.Page{}, err | ||
} | ||
return data.TicketForms, data.Page, nil | ||
} | ||
|
||
// CreateTicketForm creates new ticket form | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_forms#create-ticket-forms | ||
func (z *Client) CreateTicketForm(ctx context.Context, ticketForm models.TicketForm) (models.TicketForm, error) { | ||
var data, result struct { | ||
TicketForm models.TicketForm `json:"ticket_form"` | ||
} | ||
data.TicketForm = ticketForm | ||
|
||
body, err := z.Post(ctx, "/ticket_forms.json", data) | ||
if err != nil { | ||
return models.TicketForm{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return models.TicketForm{}, err | ||
} | ||
return result.TicketForm, nil | ||
} | ||
|
||
// GetTicketForm returns the specified ticket form | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_forms#show-ticket-form | ||
func (z *Client) GetTicketForm(ctx context.Context, id int64) (models.TicketForm, error) { | ||
var result struct { | ||
TicketForm models.TicketForm `json:"ticket_form"` | ||
} | ||
|
||
body, err := z.Get(ctx, fmt.Sprintf("/ticket_forms/%d.json", id)) | ||
if err != nil { | ||
return models.TicketForm{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return models.TicketForm{}, err | ||
} | ||
return result.TicketForm, nil | ||
} | ||
|
||
// UpdateTicketForm updates the specified ticket form and returns the updated form | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_forms#update-ticket-forms | ||
func (z *Client) UpdateTicketForm(ctx context.Context, id int64, form models.TicketForm) (models.TicketForm, error) { | ||
var data, result struct { | ||
TicketForm models.TicketForm `json:"ticket_form"` | ||
} | ||
|
||
data.TicketForm = form | ||
body, err := z.Put(ctx, fmt.Sprintf("/ticket_forms/%d.json", id), data) | ||
if err != nil { | ||
return models.TicketForm{}, err | ||
} | ||
|
||
err = json.Unmarshal(body, &result) | ||
if err != nil { | ||
return models.TicketForm{}, err | ||
} | ||
|
||
return result.TicketForm, nil | ||
} | ||
|
||
// DeleteTicketForm deletes the specified ticket form | ||
// ref: https://developer.zendesk.com/rest_api/docs/support/ticket_forms#delete-ticket-form | ||
func (z *Client) DeleteTicketForm(ctx context.Context, id int64) error { | ||
err := z.Delete(ctx, fmt.Sprintf("/ticket_forms/%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,30 @@ | ||
package models | ||
|
||
type TicketForm struct { | ||
ID int64 `json:"id,omitempty"` | ||
URL string `json:"url,omitempty"` | ||
Name string `json:"name"` | ||
RawName string `json:"raw_name,omitempty"` | ||
DisplayName string `json:"display_name,omitempty"` | ||
RawDisplayName string `json:"raw_display_name,omitempty"` | ||
Position int64 `json:"position"` | ||
Active bool `json:"active,omitempty"` | ||
EndUserVisible bool `json:"end_user_visible,omitempty"` | ||
Default bool `json:"default,omitempty"` | ||
TicketFieldIDs []int64 `json:"ticket_field_ids,omitempty"` | ||
InAllBrands bool `json:"in_all_brands,omitempty"` | ||
RestrictedBrandIDs []int64 `json:"restricted_brand_ids,omitempty"` | ||
} | ||
|
||
type TicketFormListOptions struct { | ||
PageOptions | ||
Active bool `url:"active,omitempty"` | ||
EndUserVisible bool `url:"end_user_visible,omitempty"` | ||
FallbackToDefault bool `url:"fallback_to_default,omitempty"` | ||
AssociatedToBrand bool `url:"associated_to_brand,omitempty"` | ||
} | ||
|
||
type PageOptions struct { | ||
PerPage int `url:"per_page,omitempty"` | ||
Page int `url:"page,omitempty"` | ||
} |
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
Oops, something went wrong.