Skip to content

Commit

Permalink
feat: add ShowCustomObjectRecord and UpdateCustomObjectRecord methods
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloromolini committed Sep 4, 2023
1 parent b824acd commit 7cdb5f2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
55 changes: 54 additions & 1 deletion zendesk/custom_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CustomObjectRecord struct {
Name string `json:"name"`
ID string `json:"id"`
CustomObjectKey string `json:"custom_object_key"`
CustomObjectFields map[string]interface{} `json:"custom_object_fields"`
CustomObjectFields map[string]interface{} `json:"custom_object_fields" binding:"required"`
CreatedByUserID string `json:"created_by_user_id"`
UpdatedByUserID string `json:"updated_by_user_id"`
CreatedAt time.Time `json:"created_at"`
Expand All @@ -31,6 +31,12 @@ type CustomObjectAPI interface {
) ([]CustomObjectRecord, Page, error)
ListCustomObjectRecords(
ctx context.Context, customObjectKey string, opts *CustomObjectListOptions) ([]CustomObjectRecord, Page, error)
ShowCustomObjectRecord(
ctx context.Context, customObjectKey string, customObjectRecordID string,
) (*CustomObjectRecord, error)
UpdateCustomObjectRecord(
ctx context.Context, customObjectKey string, customObjectRecordID string, record CustomObjectRecord,
) (*CustomObjectRecord, error)
}

// CustomObjectAutocompleteOptions custom object search options
Expand Down Expand Up @@ -117,3 +123,50 @@ func (z *Client) SearchCustomObjectRecords(
}
return result.CustomObjectRecords, result.Page, nil
}

// ShowCustomObjectRecord returns a custom record for a specific object using a provided id.
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#show-custom-object-record
func (z *Client) ShowCustomObjectRecord(
ctx context.Context, customObjectKey string, customObjectRecordID string,
) (*CustomObjectRecord, error) {
var result struct {
CustomObjectRecord CustomObjectRecord `json:"custom_object_record"`
}

url := fmt.Sprintf("/custom_objects/%s/records/%s", customObjectKey, customObjectRecordID)
body, err := z.get(ctx, url)

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

if err != nil {
return nil, err
}
return &result.CustomObjectRecord, nil
}

// UpdateCustomObjectRecord Updates an individual custom object record
// https://developer.zendesk.com/api-reference/custom-objects/custom_object_records/#update-custom-object-record
func (z *Client) UpdateCustomObjectRecord(
ctx context.Context, customObjectKey string, customObjectRecordID string, record CustomObjectRecord,
) (*CustomObjectRecord, error) {
var data, result struct {
CustomObjectRecord CustomObjectRecord `json:"custom_object_record"`
}
data.CustomObjectRecord = record

url := fmt.Sprintf("/custom_objects/%s/records/%s", customObjectKey, customObjectRecordID)
body, err := z.patch(ctx, url, data)

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

if err != nil {
return nil, err
}
return &result.CustomObjectRecord, nil
}
30 changes: 30 additions & 0 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions zendesk/zendesk.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,42 @@ func (z *Client) put(ctx context.Context, path string, data interface{}) ([]byte
return body, nil
}

// patch sends data to API and returns response body as []bytes
func (z *Client) patch(ctx context.Context, path string, data interface{}) ([]byte, error) {
bytes, err := json.Marshal(data)
if err != nil {
return nil, err
}

req, err := http.NewRequest(http.MethodPatch, z.baseURL.String()+path, strings.NewReader(string(bytes)))
if err != nil {
return nil, err
}

req = z.prepareRequest(ctx, req)

resp, err := z.httpClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

// NOTE: some webhook mutation APIs return status No Content.
if !(resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNoContent) {
return nil, Error{
body: body,
resp: resp,
}
}

return body, nil
}

// delete sends data to API and returns an error if unsuccessful
func (z *Client) delete(ctx context.Context, path string) error {
req, err := http.NewRequest(http.MethodDelete, z.baseURL.String()+path, nil)
Expand Down

0 comments on commit 7cdb5f2

Please sign in to comment.