From 803a22e7abed4df2540006f27a7ab27c637f0cf8 Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Wed, 18 May 2022 13:28:18 +1000 Subject: [PATCH] handle query parameters consistently with go-querystring --- access_application.go | 20 +++++++------------- access_bookmark.go | 20 +++++++------------- access_group.go | 20 +++++++------------- access_policy.go | 19 ++++++------------- account_members.go | 20 +++++++------------- accounts.go | 20 +++++++------------- cloudflare.go | 4 ++-- filter.go | 20 ++++++-------------- firewall_rules.go | 19 ++++++------------- images.go | 20 +++++++------------- pages_project.go | 20 +++++++------------- rate_limiting.go | 20 +++++++------------- 12 files changed, 76 insertions(+), 146 deletions(-) diff --git a/access_application.go b/access_application.go index 64f82076f..bd872e351 100644 --- a/access_application.go +++ b/access_application.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -97,20 +96,15 @@ func (api *API) ZoneLevelAccessApplications(ctx context.Context, zoneID string, } func (api *API) accessApplications(ctx context.Context, id string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessApplication, ResultInfo, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf("/%s/%s/access/apps", routeRoot, id) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []AccessApplication{}, ResultInfo{}, err } diff --git a/access_bookmark.go b/access_bookmark.go index 18d152b17..5eb4d5f01 100644 --- a/access_bookmark.go +++ b/access_bookmark.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -53,20 +52,15 @@ func (api *API) ZoneLevelAccessBookmarks(ctx context.Context, zoneID string, pag } func (api *API) accessBookmarks(ctx context.Context, id string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessBookmark, ResultInfo, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf("/%s/%s/access/bookmarks", routeRoot, id) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []AccessBookmark{}, ResultInfo{}, err } diff --git a/access_group.go b/access_group.go index 314985d69..795349c00 100644 --- a/access_group.go +++ b/access_group.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -215,25 +214,19 @@ func (api *API) ZoneLevelAccessGroups(ctx context.Context, zoneID string, pageOp } func (api *API) accessGroups(ctx context.Context, id string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessGroup, ResultInfo, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf( "/%s/%s/access/groups", routeRoot, id, ) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []AccessGroup{}, ResultInfo{}, err } @@ -336,6 +329,7 @@ func (api *API) updateAccessGroup(ctx context.Context, id string, accessGroup Ac if accessGroup.ID == "" { return AccessGroup{}, errors.Errorf("access group ID cannot be empty") } + uri := fmt.Sprintf( "/%s/%s/access/groups/%s", routeRoot, diff --git a/access_policy.go b/access_policy.go index 022ab4495..937fb60cb 100644 --- a/access_policy.go +++ b/access_policy.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -78,14 +77,6 @@ func (api *API) ZoneLevelAccessPolicies(ctx context.Context, zoneID, application } func (api *API) accessPolicies(ctx context.Context, id string, applicationID string, pageOpts PaginationOptions, routeRoot RouteRoot) ([]AccessPolicy, ResultInfo, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf( "/%s/%s/access/apps/%s/policies", routeRoot, @@ -93,11 +84,13 @@ func (api *API) accessPolicies(ctx context.Context, id string, applicationID str applicationID, ) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []AccessPolicy{}, ResultInfo{}, err } diff --git a/account_members.go b/account_members.go index ba2c55447..1046b5a79 100644 --- a/account_members.go +++ b/account_members.go @@ -5,9 +5,8 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -63,20 +62,15 @@ func (api *API) AccountMembers(ctx context.Context, accountID string, pageOpts P return []AccountMember{}, ResultInfo{}, errors.New(errMissingAccountID) } - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf("/accounts/%s/members", accountID) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []AccountMember{}, ResultInfo{}, err } diff --git a/accounts.go b/accounts.go index 338ef40c0..0013be28c 100644 --- a/accounts.go +++ b/accounts.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -53,20 +52,15 @@ type AccountDetailResponse struct { // // API reference: https://api.cloudflare.com/#accounts-list-accounts func (api *API) Accounts(ctx context.Context, pageOpts PaginationOptions) ([]Account, ResultInfo, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := "/accounts" - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []Account{}, ResultInfo{}, err } diff --git a/cloudflare.go b/cloudflare.go index c0d02ecbd..c4fb1d3c3 100644 --- a/cloudflare.go +++ b/cloudflare.go @@ -457,8 +457,8 @@ func (api *API) Raw(method, endpoint string, data interface{}) (json.RawMessage, // PaginationOptions can be passed to a list request to configure paging // These values will be defaulted if omitted, and PerPage has min/max limits set by resource. type PaginationOptions struct { - Page int `json:"page,omitempty"` - PerPage int `json:"per_page,omitempty"` + Page int `json:"page,omitempty" url:"page,omitempty"` + PerPage int `json:"per_page,omitempty" url:"per_page,omitempty"` } // RetryPolicy specifies number of retries and min/max retry delays diff --git a/filter.go b/filter.go index f657303a9..ec295492b 100644 --- a/filter.go +++ b/filter.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "strings" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -85,21 +84,14 @@ func (api *API) Filter(ctx context.Context, zoneID, filterID string) (Filter, er // API reference: https://developers.cloudflare.com/firewall/api/cf-filters/get/#get-all-filters func (api *API) Filters(ctx context.Context, zoneID string, pageOpts PaginationOptions) ([]Filter, error) { uri := fmt.Sprintf("/zones/%s/filters", zoneID) - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) - } - - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []Filter{}, err } diff --git a/firewall_rules.go b/firewall_rules.go index 480b38958..cd49438b0 100644 --- a/firewall_rules.go +++ b/firewall_rules.go @@ -6,9 +6,9 @@ import ( "fmt" "net/http" "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -46,21 +46,14 @@ type FirewallRuleResponse struct { // API reference: https://developers.cloudflare.com/firewall/api/cf-firewall-rules/get/#get-all-rules func (api *API) FirewallRules(ctx context.Context, zoneID string, pageOpts PaginationOptions) ([]FirewallRule, error) { uri := fmt.Sprintf("/zones/%s/firewall/rules", zoneID) - v := url.Values{} - - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) - } - - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []FirewallRule{}, err } diff --git a/images.go b/images.go index 292778d08..852eb528f 100644 --- a/images.go +++ b/images.go @@ -8,10 +8,9 @@ import ( "io" "mime/multipart" "net/http" - "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -205,20 +204,15 @@ func (api *API) CreateImageDirectUploadURL(ctx context.Context, accountID string // // API Reference: https://api.cloudflare.com/#cloudflare-images-list-images func (api *API) ListImages(ctx context.Context, accountID string, pageOpts PaginationOptions) ([]Image, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf("/accounts/%s/images/v1", accountID) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []Image{}, err } diff --git a/pages_project.go b/pages_project.go index dba045e37..f79dcdd2f 100644 --- a/pages_project.go +++ b/pages_project.go @@ -5,10 +5,9 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "time" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -121,20 +120,15 @@ type pagesProjectListResponse struct { // // API reference: https://api.cloudflare.com/#pages-project-get-projects func (api *API) ListPagesProjects(ctx context.Context, accountID string, pageOpts PaginationOptions) ([]PagesProject, ResultInfo, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf("/accounts/%s/pages/projects", accountID) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []PagesProject{}, ResultInfo{}, err } diff --git a/rate_limiting.go b/rate_limiting.go index a9a71ccf9..e97dc1ff6 100644 --- a/rate_limiting.go +++ b/rate_limiting.go @@ -5,9 +5,8 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" + "github.com/google/go-querystring/query" "github.com/pkg/errors" ) @@ -107,20 +106,15 @@ func (api *API) CreateRateLimit(ctx context.Context, zoneID string, limit RateLi // // API reference: https://api.cloudflare.com/#rate-limits-for-a-zone-list-rate-limits func (api *API) ListRateLimits(ctx context.Context, zoneID string, pageOpts PaginationOptions) ([]RateLimit, ResultInfo, error) { - v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) - } - if pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) - } - uri := fmt.Sprintf("/zones/%s/rate_limits", zoneID) - if len(v) > 0 { - uri = fmt.Sprintf("%s?%s", uri, v.Encode()) + + v, _ := query.Values(pageOpts) + queryParams := v.Encode() + if queryParams != "" { + queryParams = "?" + queryParams } - res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri+queryParams, nil) if err != nil { return []RateLimit{}, ResultInfo{}, err }