Skip to content

Commit

Permalink
Added OBP to CBO migration guide
Browse files Browse the repository at this point in the history
minor bug fixes
  • Loading branch information
JinHuangAtZen committed Oct 11, 2023
1 parent 0a1eb24 commit 216c976
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 33 deletions.
92 changes: 92 additions & 0 deletions CBPMigration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# OBP to CBP migration

Zendesk is [obsoleting](https://support.zendesk.com/hc/en-us/articles/4408846180634-Introducing-Pagination-Changes-Zendesk-API#h_01F7Y57A0G5M3R8JXGCQTBKVWA) Offset Based Pagination (OBP), it's recomended to start adopting Cursor Based Pagination (CBP). This SDK have created pagination Iterators to help facilite the change.

To use the pagination iterator, start with `NewPaginationOptions()` function, it will return a `PaginationOptions` object, you can specify the default page size in `PageSize` variable. By default, `PageSize` is 100. Then you can call the `client.GetXXXXXIterator(ctx, ops)` to return an object pagination iterator, with the iterator, you can iterator through the objects with `HasMore()` and `GetNext()` until `HasMore` return `false`.

```go
ops := NewPaginationOptions()
// ops.PageSize = 50 // PageSize can be set to 50
it := client.GetTicketsIterator(ctx, ops)
for it.HasMore() {
tickets, err := it.GetNext()
if err == nil {
for _, ticket := range tickets {
println(ticket.Subject)
}
}
}
```

If the API endpoint requires more options like organization ID, it can be set into the `Id` attribute like below example:

```go
ops := NewPaginationOptions()
ops.Sort = "updated_at"
ops.PageSize = 10
ops.Id = 360363695492
it := client.GetOrganizationTicketsIterator(ctx, ops)

for it.HasMore() {
tickets, err := it.GetNext()
if err == nil {
for _, ticket := range tickets {
println(ticket.Subject)
}
}
}
```

For any API specific parameters, they are predefined in the `CommonOptions` struct, we can set these attributes in the `PaginationOptions` object.
If new attributes are introduced to any existing or new API endpoints, it can be added into this struct.

```go
type CommonOptions struct {
Active bool `url:"active,omitempty"`
Role string `url:"role,omitempty"`
Roles []string `url:"role[],omitempty"`
PermissionSet int64 `url:"permission_set,omitempty"`

// SortBy can take "assignee", "assignee.name", "created_at", "group", "id",
// "locale", "requester", "requester.name", "status", "subject", "updated_at"
SortBy string `url:"sort_by,omitempty"`

// SortOrder can take "asc" or "desc"
SortOrder string `url:"sort_order,omitempty"`
Sort string `url:"sort,omitempty"`
Id int64
GroupID int64 `json:"group_id,omitempty" url:"group_id,omitempty"`
UserID int64 `json:"user_id,omitempty" url:"user_id,omitempty"`
OrganizationID int64 `json:"organization_id,omitempty" url:"organization_id,omitempty"`

Access string `json:"access"`
Category int `json:"category"`
Include string `json:"include" url:"include,omitempty"`
OnlyViewable bool `json:"only_viewable"`
Query string `url:"query"`
EndUserVisible bool `url:"end_user_visible,omitempty"`
FallbackToDefault bool `url:"fallback_to_default,omitempty"`
AssociatedToBrand bool `url:"associated_to_brand,omitempty"`
CategoryID string `url:"category_id,omitempty"`

IncludeInlineImages string `url:"include_inline_images,omitempty"`
}
```

## To regenerate CBP(Cursor Based Pagination), OBP(Offset Based Pagination) helper function and Iterators

If a new API endpoint supports CBP, add a new element to the funcData in script/codegen/main.go file like this:

```go
{
FuncName: "Automations",
ObjectName: "Automation",
ApiEndpoint: "/automation.json",
JsonName: "automations",
FileName: "automation",
},
```

should use the script to generate the helper functions and the iterator
`go run script/codegen/main.go`

32 changes: 1 addition & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,7 @@ You can simulate the response from Zendesk API with it.

`go generate ./...`

## To regenerate CBP(Cursor Based Pagination), OBP(Offset Based Pagination) helper function and Iterators

If a new API endpoint supports CBP, add a new element to the funcData in script/codegen/main.go file like this:

```go
{
FuncName: "Automations",
ObjectName: "Automation",
ApiEndpoint: "/automation.json",
JsonName: "automations",
FileName: "automation",
},
```
should use the script to generate the helper functions and the iterator
`go run script/codegen/main.go`

## Example for using the CBP/OBP iterator

```go
ops := NewPaginationOptions()
it := client.GetTicketsIterator(ctx, ops)
for it.HasMore() {
tickets, err := it.GetNext()
if err == nil {
for _, ticket := range tickets {
println(ticket.Subject)
}
}
}
```

## Zendesk [OBP(Offset Based Pagination) to CBP(Cursor Based Pagination) migration guide](CBPMigration.md)

## Maintainer

Expand Down
2 changes: 1 addition & 1 deletion script/codegen/main.go

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

2 changes: 1 addition & 1 deletion zendesk/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestGetOrganizationTicketsIterator(t *testing.T) {
ops := NewPaginationOptions()
ops.Sort = "updated_at"
ops.PageSize = 10

ops.Id = 360363695492
it := client.GetOrganizationTicketsIterator(ctx, ops)

expectedLength := 2
Expand Down

0 comments on commit 216c976

Please sign in to comment.