Skip to content

Commit a4cebf3

Browse files
author
nukosuke
authored
Merge pull request #280 from paoloromolini/custom_objects_v2
Custom objects v2
2 parents 518a951 + c7149ad commit a4cebf3

File tree

4 files changed

+286
-0
lines changed

4 files changed

+286
-0
lines changed

zendesk/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type API interface {
3333
UserFieldAPI
3434
ViewAPI
3535
WebhookAPI
36+
CustomObjectAPI
3637
}
3738

3839
var _ API = (*Client)(nil)

zendesk/custom_object.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package zendesk
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"time"
8+
)
9+
10+
type CustomObjectRecord struct {
11+
Url string `json:"url,omitempty"`
12+
Name string `json:"name,omitempty"`
13+
ID string `json:"id,omitempty"`
14+
CustomObjectKey string `json:"custom_object_key"`
15+
CustomObjectFields map[string]interface{} `json:"custom_object_fields" binding:"required"`
16+
CreatedByUserID string `json:"created_by_user_id,omitempty"`
17+
UpdatedByUserID string `json:"updated_by_user_id,omitempty"`
18+
CreatedAt time.Time `json:"created_at,omitempty"`
19+
UpdatedAt time.Time `json:"updated_at,omitempty"`
20+
ExternalID string `json:"external_id,omitempty"`
21+
}
22+
23+
// CustomObjectAPI an interface containing all custom object related methods
24+
type CustomObjectAPI interface {
25+
CreateCustomObjectRecord(
26+
ctx context.Context, record CustomObjectRecord, customObjectKey string) (CustomObjectRecord, error)
27+
SearchCustomObjectRecords(
28+
ctx context.Context,
29+
customObjectKey string,
30+
opts *CustomObjectAutocompleteOptions,
31+
) ([]CustomObjectRecord, Page, error)
32+
ListCustomObjectRecords(
33+
ctx context.Context, customObjectKey string, opts *CustomObjectListOptions) ([]CustomObjectRecord, Page, error)
34+
ShowCustomObjectRecord(
35+
ctx context.Context, customObjectKey string, customObjectRecordID string,
36+
) (*CustomObjectRecord, error)
37+
UpdateCustomObjectRecord(
38+
ctx context.Context, customObjectKey string, customObjectRecordID string, record CustomObjectRecord,
39+
) (*CustomObjectRecord, error)
40+
}
41+
42+
// CustomObjectAutocompleteOptions custom object search options
43+
type CustomObjectAutocompleteOptions struct {
44+
PageOptions
45+
Name string `url:"name"`
46+
}
47+
48+
// CreateCustomObjectRecord CreateCustomObject create a custom object record
49+
func (z *Client) CreateCustomObjectRecord(
50+
ctx context.Context, record CustomObjectRecord, customObjectKey string,
51+
) (CustomObjectRecord, error) {
52+
53+
var data, result struct {
54+
CustomObjectRecord CustomObjectRecord `json:"custom_object_record"`
55+
}
56+
data.CustomObjectRecord = record
57+
58+
body, err := z.post(ctx, fmt.Sprintf("/custom_objects/%s/records.json", customObjectKey), data)
59+
if err != nil {
60+
return CustomObjectRecord{}, err
61+
}
62+
err = json.Unmarshal(body, &result)
63+
if err != nil {
64+
return CustomObjectRecord{}, err
65+
}
66+
return result.CustomObjectRecord, nil
67+
}
68+
69+
// CustomObjectListOptions custom object list options
70+
type CustomObjectListOptions struct {
71+
PageOptions
72+
ExternalIds []string `url:"external_ids"`
73+
}
74+
75+
// ListCustomObjectRecords list objects
76+
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#list-custom-object-records
77+
func (z *Client) ListCustomObjectRecords(
78+
ctx context.Context, customObjectKey string, opts *CustomObjectListOptions) ([]CustomObjectRecord, Page, error) {
79+
var result struct {
80+
CustomObjectRecords []CustomObjectRecord `json:"custom_object_records"`
81+
Page
82+
}
83+
tmp := opts
84+
if tmp == nil {
85+
tmp = &CustomObjectListOptions{}
86+
}
87+
url := fmt.Sprintf("/custom_objects/%s/records", customObjectKey)
88+
urlWithOptions, err := addOptions(url, tmp)
89+
body, err := z.get(ctx, urlWithOptions)
90+
91+
if err != nil {
92+
return nil, Page{}, err
93+
}
94+
err = json.Unmarshal(body, &result)
95+
if err != nil {
96+
return nil, Page{}, err
97+
}
98+
return result.CustomObjectRecords, result.Page, nil
99+
}
100+
101+
// SearchCustomObjectRecords search for a custom object record by the name field
102+
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#search-custom-object-records
103+
func (z *Client) SearchCustomObjectRecords(
104+
ctx context.Context, customObjectKey string, opts *CustomObjectAutocompleteOptions) ([]CustomObjectRecord, Page, error) {
105+
var result struct {
106+
CustomObjectRecords []CustomObjectRecord `json:"custom_object_records"`
107+
Page
108+
}
109+
tmp := opts
110+
if tmp == nil {
111+
tmp = &CustomObjectAutocompleteOptions{}
112+
}
113+
url := fmt.Sprintf("/custom_objects/%s/records/autocomplete", customObjectKey)
114+
urlWithOptions, err := addOptions(url, tmp)
115+
body, err := z.get(ctx, urlWithOptions)
116+
117+
if err != nil {
118+
return nil, Page{}, err
119+
}
120+
err = json.Unmarshal(body, &result)
121+
if err != nil {
122+
return nil, Page{}, err
123+
}
124+
return result.CustomObjectRecords, result.Page, nil
125+
}
126+
127+
// ShowCustomObjectRecord returns a custom record for a specific object using a provided id.
128+
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#show-custom-object-record
129+
func (z *Client) ShowCustomObjectRecord(
130+
ctx context.Context, customObjectKey string, customObjectRecordID string,
131+
) (*CustomObjectRecord, error) {
132+
var result struct {
133+
CustomObjectRecord CustomObjectRecord `json:"custom_object_record"`
134+
}
135+
136+
url := fmt.Sprintf("/custom_objects/%s/records/%s", customObjectKey, customObjectRecordID)
137+
body, err := z.get(ctx, url)
138+
139+
if err != nil {
140+
return nil, err
141+
}
142+
err = json.Unmarshal(body, &result)
143+
144+
if err != nil {
145+
return nil, err
146+
}
147+
return &result.CustomObjectRecord, nil
148+
}
149+
150+
// UpdateCustomObjectRecord Updates an individual custom object record
151+
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#update-custom-object-record
152+
func (z *Client) UpdateCustomObjectRecord(
153+
ctx context.Context, customObjectKey string, customObjectRecordID string, record CustomObjectRecord,
154+
) (*CustomObjectRecord, error) {
155+
var data, result struct {
156+
CustomObjectRecord CustomObjectRecord `json:"custom_object_record"`
157+
}
158+
data.CustomObjectRecord = record
159+
160+
url := fmt.Sprintf("/custom_objects/%s/records/%s", customObjectKey, customObjectRecordID)
161+
body, err := z.patch(ctx, url, data)
162+
163+
if err != nil {
164+
return nil, err
165+
}
166+
err = json.Unmarshal(body, &result)
167+
168+
if err != nil {
169+
return nil, err
170+
}
171+
return &result.CustomObjectRecord, nil
172+
}

zendesk/mock/client.go

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zendesk/zendesk.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,42 @@ func (z *Client) put(ctx context.Context, path string, data interface{}) ([]byte
220220
return body, nil
221221
}
222222

223+
// patch sends data to API and returns response body as []bytes
224+
func (z *Client) patch(ctx context.Context, path string, data interface{}) ([]byte, error) {
225+
bytes, err := json.Marshal(data)
226+
if err != nil {
227+
return nil, err
228+
}
229+
230+
req, err := http.NewRequest(http.MethodPatch, z.baseURL.String()+path, strings.NewReader(string(bytes)))
231+
if err != nil {
232+
return nil, err
233+
}
234+
235+
req = z.prepareRequest(ctx, req)
236+
237+
resp, err := z.httpClient.Do(req)
238+
if err != nil {
239+
return nil, err
240+
}
241+
242+
defer resp.Body.Close()
243+
body, err := ioutil.ReadAll(resp.Body)
244+
if err != nil {
245+
return nil, err
246+
}
247+
248+
// NOTE: some webhook mutation APIs return status No Content.
249+
if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNoContent) {
250+
return nil, Error{
251+
body: body,
252+
resp: resp,
253+
}
254+
}
255+
256+
return body, nil
257+
}
258+
223259
// delete sends data to API and returns an error if unsuccessful
224260
func (z *Client) delete(ctx context.Context, path string) error {
225261
req, err := http.NewRequest(http.MethodDelete, z.baseURL.String()+path, nil)

0 commit comments

Comments
 (0)