Skip to content

Commit 7cdb5f2

Browse files
committed
feat: add ShowCustomObjectRecord and UpdateCustomObjectRecord methods
1 parent b824acd commit 7cdb5f2

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

zendesk/custom_object.go

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type CustomObjectRecord struct {
1212
Name string `json:"name"`
1313
ID string `json:"id"`
1414
CustomObjectKey string `json:"custom_object_key"`
15-
CustomObjectFields map[string]interface{} `json:"custom_object_fields"`
15+
CustomObjectFields map[string]interface{} `json:"custom_object_fields" binding:"required"`
1616
CreatedByUserID string `json:"created_by_user_id"`
1717
UpdatedByUserID string `json:"updated_by_user_id"`
1818
CreatedAt time.Time `json:"created_at"`
@@ -31,6 +31,12 @@ type CustomObjectAPI interface {
3131
) ([]CustomObjectRecord, Page, error)
3232
ListCustomObjectRecords(
3333
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)
3440
}
3541

3642
// CustomObjectAutocompleteOptions custom object search options
@@ -117,3 +123,50 @@ func (z *Client) SearchCustomObjectRecords(
117123
}
118124
return result.CustomObjectRecords, result.Page, nil
119125
}
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: 30 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)