Skip to content

Commit

Permalink
Merge pull request #904 from jacobbednarz/fix-format-for-multiple-fil…
Browse files Browse the repository at this point in the history
…ter-deletes

filter: update `DeleteFilters` to correctly handle multiple parameters
  • Loading branch information
jacobbednarz committed May 24, 2022
2 parents 5f6a958 + db957c4 commit 9af8b92
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"net/url"

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

var ErrNotEnoughFilterIDsProvided = errors.New("at least one filter ID must be provided.")

// Filter holds the structure of the filter type.
type Filter struct {
ID string `json:"id,omitempty"`
Expand Down Expand Up @@ -197,10 +199,21 @@ func (api *API) DeleteFilter(ctx context.Context, zoneID, filterID string) error
//
// API reference: https://developers.cloudflare.com/firewall/api/cf-filters/delete/#delete-multiple-filters
func (api *API) DeleteFilters(ctx context.Context, zoneID string, filterIDs []string) error {
ids := strings.Join(filterIDs, ",")
uri := fmt.Sprintf("/zones/%s/filters?id=%s", zoneID, ids)
if len(filterIDs) == 0 {
return ErrNotEnoughFilterIDsProvided
}

_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
// Swap this to a typed struct and passing in all parameters together.
q := url.Values{}
for _, id := range filterIDs {
q.Add("id", id)
}

queryParams := "?" + q.Encode()

uri := fmt.Sprintf("/zones/%s/filters", zoneID)

_, err := api.makeRequestContext(ctx, http.MethodDelete, uri+queryParams, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 9af8b92

Please sign in to comment.