|
| 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 | +} |
0 commit comments