Skip to content

Commit 76a5fe4

Browse files
author
nukosuke
authored
Merge pull request #292 from JinHuangAtZen/jin.huang/pagination-code-template
Created code generator for OBP/CBP functions and iterator
2 parents 10b7bdb + 216c976 commit 76a5fe4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3057
-212
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
path-to-profile: profile.cov
4343
flag-name: Go-${{ matrix.go-version }}
4444
parallel: true
45-
ignore: zendesk/mock/client.go
45+
ignore: zendesk/mock/client.go, zendesk/*_generated.go
4646

4747
finalize:
4848
needs: test

CBPMigration.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# OBP to CBP migration
2+
3+
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.
4+
5+
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`.
6+
7+
```go
8+
ops := NewPaginationOptions()
9+
// ops.PageSize = 50 // PageSize can be set to 50
10+
it := client.GetTicketsIterator(ctx, ops)
11+
for it.HasMore() {
12+
tickets, err := it.GetNext()
13+
if err == nil {
14+
for _, ticket := range tickets {
15+
println(ticket.Subject)
16+
}
17+
}
18+
}
19+
```
20+
21+
If the API endpoint requires more options like organization ID, it can be set into the `Id` attribute like below example:
22+
23+
```go
24+
ops := NewPaginationOptions()
25+
ops.Sort = "updated_at"
26+
ops.PageSize = 10
27+
ops.Id = 360363695492
28+
it := client.GetOrganizationTicketsIterator(ctx, ops)
29+
30+
for it.HasMore() {
31+
tickets, err := it.GetNext()
32+
if err == nil {
33+
for _, ticket := range tickets {
34+
println(ticket.Subject)
35+
}
36+
}
37+
}
38+
```
39+
40+
For any API specific parameters, they are predefined in the `CommonOptions` struct, we can set these attributes in the `PaginationOptions` object.
41+
If new attributes are introduced to any existing or new API endpoints, it can be added into this struct.
42+
43+
```go
44+
type CommonOptions struct {
45+
Active bool `url:"active,omitempty"`
46+
Role string `url:"role,omitempty"`
47+
Roles []string `url:"role[],omitempty"`
48+
PermissionSet int64 `url:"permission_set,omitempty"`
49+
50+
// SortBy can take "assignee", "assignee.name", "created_at", "group", "id",
51+
// "locale", "requester", "requester.name", "status", "subject", "updated_at"
52+
SortBy string `url:"sort_by,omitempty"`
53+
54+
// SortOrder can take "asc" or "desc"
55+
SortOrder string `url:"sort_order,omitempty"`
56+
Sort string `url:"sort,omitempty"`
57+
Id int64
58+
GroupID int64 `json:"group_id,omitempty" url:"group_id,omitempty"`
59+
UserID int64 `json:"user_id,omitempty" url:"user_id,omitempty"`
60+
OrganizationID int64 `json:"organization_id,omitempty" url:"organization_id,omitempty"`
61+
62+
Access string `json:"access"`
63+
Category int `json:"category"`
64+
Include string `json:"include" url:"include,omitempty"`
65+
OnlyViewable bool `json:"only_viewable"`
66+
Query string `url:"query"`
67+
EndUserVisible bool `url:"end_user_visible,omitempty"`
68+
FallbackToDefault bool `url:"fallback_to_default,omitempty"`
69+
AssociatedToBrand bool `url:"associated_to_brand,omitempty"`
70+
CategoryID string `url:"category_id,omitempty"`
71+
72+
IncludeInlineImages string `url:"include_inline_images,omitempty"`
73+
}
74+
```
75+
76+
## To regenerate CBP(Cursor Based Pagination), OBP(Offset Based Pagination) helper function and Iterators
77+
78+
If a new API endpoint supports CBP, add a new element to the funcData in script/codegen/main.go file like this:
79+
80+
```go
81+
{
82+
FuncName: "Automations",
83+
ObjectName: "Automation",
84+
ApiEndpoint: "/automation.json",
85+
JsonName: "automations",
86+
FileName: "automation",
87+
},
88+
```
89+
90+
should use the script to generate the helper functions and the iterator
91+
`go run script/codegen/main.go`
92+

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# go-zendesk
2+
23
[![Actions Status](https://github.com/nukosuke/go-zendesk/workflows/CI/badge.svg)](https://github.com/nukosuke/go-zendesk/actions)
34
[![Build status](https://ci.appveyor.com/api/projects/status/ce4p1mswjkdftv6o/branch/master?svg=true)](https://ci.appveyor.com/project/nukosuke/go-zendesk/branch/master)
45
[![Coverage Status](https://coveralls.io/repos/github/nukosuke/go-zendesk/badge.svg?branch=master)](https://coveralls.io/github/nukosuke/go-zendesk?branch=master)
@@ -12,7 +13,7 @@ Zendesk API client library for Go
1213

1314
## Installation
1415

15-
``` shell
16+
```shell
1617
$ go get github.com/nukosuke/go-zendesk
1718
```
1819

@@ -48,14 +49,18 @@ func main() {
4849
```
4950

5051
## Want to mock API?
52+
5153
go-zendesk has a [mock package](https://pkg.go.dev/github.com/nukosuke/go-zendesk/zendesk/mock) generated by [golang/mock](https://github.com/golang/mock).
5254
You can simulate the response from Zendesk API with it.
5355

5456
## To regenerate the mock client
5557

5658
`go generate ./...`
5759

60+
## Zendesk [OBP(Offset Based Pagination) to CBP(Cursor Based Pagination) migration guide](CBPMigration.md)
61+
5862
## Maintainer
63+
5964
- [nukosuke](https://github.com/nukosuke)
6065

6166
## License
@@ -64,5 +69,4 @@ MIT License.
6469

6570
See the file [LICENSE](./LICENSE).
6671

67-
6872
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fnukosuke%2Fgo-zendesk.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fnukosuke%2Fgo-zendesk?ref=badge_large)

0 commit comments

Comments
 (0)