Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle query parameters consistently with go-querystring #889

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 7 additions & 13 deletions access_application.go
Expand Up @@ -5,10 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 7 additions & 13 deletions access_bookmark.go
Expand Up @@ -5,10 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 7 additions & 13 deletions access_group.go
Expand Up @@ -5,10 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 6 additions & 13 deletions access_policy.go
Expand Up @@ -5,10 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -78,26 +77,20 @@ 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,
id,
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
}
Expand Down
20 changes: 7 additions & 13 deletions account_members.go
Expand Up @@ -5,9 +5,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 7 additions & 13 deletions accounts.go
Expand Up @@ -5,10 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cloudflare.go
Expand Up @@ -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
Expand Down
20 changes: 6 additions & 14 deletions filter.go
Expand Up @@ -5,10 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 6 additions & 13 deletions firewall_rules.go
Expand Up @@ -6,9 +6,9 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 7 additions & 13 deletions images.go
Expand Up @@ -8,10 +8,9 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -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
}
Expand Down