From 258bd851783be7ae54bc283a3ecd754c1c17387a Mon Sep 17 00:00:00 2001 From: Paolo Romolini Date: Thu, 28 Sep 2023 14:53:02 +0200 Subject: [PATCH] feat: add get sources by target and delete custom objects V2 Adds the lookup custom objects API. Signed-off-by: Paolo Romolini --- script/codegen/main.go | 8 +++ zendesk/custom_object.go | 21 ++++++++ zendesk/custom_object_generated.go | 79 ++++++++++++++++++++++++++++++ zendesk/mock/client.go | 14 ++++++ 4 files changed, 122 insertions(+) create mode 100644 zendesk/custom_object_generated.go diff --git a/script/codegen/main.go b/script/codegen/main.go index 56cff823..5dc14b25 100644 --- a/script/codegen/main.go +++ b/script/codegen/main.go @@ -262,6 +262,14 @@ var funcData []FuncTemplateData = []FuncTemplateData{ FileName: "organization_tickets", ExtraParam: true, }, + { + FuncName: "GetSourcesByTarget", + ObjectName: "CustomObjectRecord", + ApiEndpoint: "/api/v2/%s/%d/relationship_fields/%d/%s.json", + JsonName: "custom_object", + FileName: "custom_object", + ExtraParam: true, + }, } func main() { diff --git a/zendesk/custom_object.go b/zendesk/custom_object.go index 6386c5f5..91f689f0 100644 --- a/zendesk/custom_object.go +++ b/zendesk/custom_object.go @@ -40,6 +40,13 @@ type CustomObjectAPI interface { UpdateCustomObjectRecord( ctx context.Context, customObjectKey string, customObjectRecordID string, record CustomObjectRecord, ) (*CustomObjectRecord, error) + GetGetSourcesByTargetIterator(ctx context.Context, opts *PaginationOptions) *Iterator[CustomObjectRecord] + GetGetSourcesByTargetOBP(ctx context.Context, opts *OBPOptions) ([]CustomObjectRecord, Page, error) + GetGetSourcesByTargetCBP(ctx context.Context, opts *CBPOptions) ([]CustomObjectRecord, CursorPaginationMeta, error) + DeleteCustomObjectRecord( + ctx context.Context, + record CustomObjectRecord, + ) error } // CustomObjectAutocompleteOptions custom object search options @@ -213,3 +220,17 @@ func (z *Client) UpdateCustomObjectRecord( } return &result.CustomObjectRecord, nil } + +// DeleteCustomObjectRecord Delete a custom object record +// https://developer.zendesk.com/api-reference/custom-data/custom-objects/custom_object_records/#delete-custom-object-record +func (z *Client) DeleteCustomObjectRecord( + ctx context.Context, + record CustomObjectRecord, +) error { + endpointURL := fmt.Sprintf("/custom_objects/%s/records/%s", record.CustomObjectKey, record.ID) + err := z.delete(ctx, endpointURL) + if err != nil { + return err + } + return nil +} diff --git a/zendesk/custom_object_generated.go b/zendesk/custom_object_generated.go new file mode 100644 index 00000000..781225e6 --- /dev/null +++ b/zendesk/custom_object_generated.go @@ -0,0 +1,79 @@ + +// Code generated by Script. DO NOT EDIT. +// Source: script/codegen/main.go +// +// Generated by this command: +// +// go run script/codegen/main.go + +package zendesk + +import ( + "context" + "fmt" +) + +func (z *Client) GetGetSourcesByTargetIterator(ctx context.Context, opts *PaginationOptions) *Iterator[CustomObjectRecord] { + return &Iterator[CustomObjectRecord]{ + CommonOptions: opts.CommonOptions, + pageSize: opts.PageSize, + hasMore: true, + isCBP: opts.IsCBP, + pageAfter: "", + pageIndex: 1, + ctx: ctx, + obpFunc: z.GetGetSourcesByTargetOBP, + cbpFunc: z.GetGetSourcesByTargetCBP, + } +} + +func (z *Client) GetGetSourcesByTargetOBP(ctx context.Context, opts *OBPOptions) ([]CustomObjectRecord, Page, error) { + var data struct { + CustomObjectRecords []CustomObjectRecord `json:"custom_object"` + Page + } + + tmp := opts + if tmp == nil { + tmp = &OBPOptions{} + } + + path := fmt.Sprintf("/api/v2/%s/%d/relationship_fields/%d/%s.json", tmp.Id) + u, err := addOptions(path, tmp) + + if err != nil { + return nil, Page{}, err + } + + err = getData(z, ctx, u, &data) + if err != nil { + return nil, Page{}, err + } + return data.CustomObjectRecords, data.Page, nil +} + +func (z *Client) GetGetSourcesByTargetCBP(ctx context.Context, opts *CBPOptions) ([]CustomObjectRecord, CursorPaginationMeta, error) { + var data struct { + CustomObjectRecords []CustomObjectRecord `json:"custom_object"` + Meta CursorPaginationMeta `json:"meta"` + } + + tmp := opts + if tmp == nil { + tmp = &CBPOptions{} + } + + path := fmt.Sprintf("/api/v2/%s/%d/relationship_fields/%d/%s.json", tmp.Id) + u, err := addOptions(path, tmp) + + if err != nil { + return nil, data.Meta, err + } + + err = getData(z, ctx, u, &data) + if err != nil { + return nil, data.Meta, err + } + return data.CustomObjectRecords, data.Meta, nil +} + diff --git a/zendesk/mock/client.go b/zendesk/mock/client.go index fe8720fb..f7f6e890 100644 --- a/zendesk/mock/client.go +++ b/zendesk/mock/client.go @@ -442,6 +442,20 @@ func (mr *ClientMockRecorder) DeleteBrand(ctx, brandID any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteBrand", reflect.TypeOf((*Client)(nil).DeleteBrand), ctx, brandID) } +// DeleteCustomObjectRecord mocks base method. +func (m *Client) DeleteCustomObjectRecord(arg0 context.Context, arg1 zendesk.CustomObjectRecord) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteCustomObjectRecord", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteCustomObjectRecord indicates an expected call of DeleteCustomObjectRecord. +func (mr *ClientMockRecorder) DeleteCustomObjectRecord(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteCustomObjectRecord", reflect.TypeOf((*Client)(nil).DeleteCustomObjectRecord), arg0, arg1) +} + // DeleteDynamicContentItem mocks base method. func (m *Client) DeleteDynamicContentItem(ctx context.Context, id int64) error { m.ctrl.T.Helper()