cloudflare / cloudflare-go

The official Go library for the Cloudflare API
https://developers.cloudflare.com/api
BSD 3-Clause "New" or "Revised" License
1.48k stars 587 forks source link

Fix pagination for filter/firewall-rules API #1024

Closed tamas-jozsa closed 2 years ago

tamas-jozsa commented 2 years ago

Confirmation

cloudflare-go version

latest

Go environment

GO111MODULE="" GOARCH="arm64" GOBIN="" GOCACHE="/Users/tjozsa/Library/Caches/go-build" GOENV="/Users/tjozsa/Library/Application Support/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="arm64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/tjozsa/go/pkg/mod" GONOPROXY="code.cfops.it" GONOSUMDB="code.cfops.it" GOOS="darwin" GOPATH="/Users/tjozsa/go" GOPRIVATE="code.cfops.it" GOPROXY="https://proxy.golang.org,direct" GOROOT="/opt/homebrew/Cellar/go/1.18.5/libexec" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/opt/homebrew/Cellar/go/1.18.5/libexec/pkg/tool/darwin_arm64" GOVCS="" GOVERSION="go1.18.5" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/81/zyf3xcqd6wv1nh70f4pspr900000gp/T/go-build1439680234=/tmp/go-build -gno-record-gcc-switches -fno-common"

Expected output

api.FirewallRules and api.Filters calling the public API only once with required params for paginations.

Actual output

They submit at least two requests:

/zones/b1fbb152bbde3bd28919a7f4bdca841f/firewall/rules
/zones/b1fbb152bbde3bd28919a7f4bdca841f/firewall/rules?page=1&per_page=50

Code demonstrating the issue

I've modified cloudflare-go a bit to present the problem I'm seeing. A simple printing out the uri we are calling in cloudflare.go makeRequestContext

func (api *API) makeRequestContext(ctx context.Context, method, uri string, params interface{}) ([]byte, error) {
    fmt.Println(uri)
    return api.makeRequestWithAuthType(ctx, method, uri, params, api.authType)
}

Then when I called in my example code:

fwRules, _, err := api.FirewallRules(context.Background(), cloudflare.ZoneIdentifier("b1fbb152bbde3bd28919a7f4bdca841f"), cloudflare.FirewallRuleListParams{})

stdout follows:

/zones/b1fbb152bbde3bd28919a7f4bdca841f/firewall/rules
/zones/b1fbb152bbde3bd28919a7f4bdca841f/firewall/rules?page=1&per_page=50

Steps to reproduce

see code demonstration

References

I found this while I was upgrading cf-terraforming to use the latest cloudflare-go. My tests were not passing, because they needed an additional interaction:

github-actions[bot] commented 2 years ago

This functionality has been released in v0.47.0.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!