-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: project inbound data filters service
- Loading branch information
Showing
4 changed files
with
210 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package sentry | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
type ProjectInboundDataFilter struct { | ||
ID string `json:"id"` | ||
Active BoolOrStringSlice `json:"active"` | ||
} | ||
|
||
type ProjectInboundDataFiltersService service | ||
|
||
func (s *ProjectInboundDataFiltersService) List(ctx context.Context, organizationSlug string, projectSlug string) ([]*ProjectInboundDataFilter, *Response, error) { | ||
u := "0/projects/" + organizationSlug + "/" + projectSlug + "/filters/" | ||
req, err := s.client.NewRequest(http.MethodGet, u, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
filters := []*ProjectInboundDataFilter{} | ||
resp, err := s.client.Do(ctx, req, &filters) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
return filters, resp, nil | ||
} | ||
|
||
type UpdateProjectInboundDataFilterParams struct { | ||
Active *bool `json:"active,omitempty"` | ||
Subfilters []string `json:"subfilters,omitempty"` | ||
} | ||
|
||
func (s *ProjectInboundDataFiltersService) Update(ctx context.Context, organizationSlug string, projectSlug string, filterID string, params *UpdateProjectInboundDataFilterParams) (*Response, error) { | ||
u := "0/projects/" + organizationSlug + "/" + projectSlug + "/filters/" + filterID + "/" | ||
req, err := s.client.NewRequest(http.MethodPut, u, params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s.client.Do(ctx, req, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package sentry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProjectInboundDataFiltersService_List(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/api/0/projects/organization_slug/project_slug/filters/", func(w http.ResponseWriter, r *http.Request) { | ||
assertMethod(t, http.MethodGet, r) | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprint(w, `[ | ||
{ | ||
"id": "browser-extensions", | ||
"active": false | ||
}, | ||
{ | ||
"id": "filtered-transaction", | ||
"active": true | ||
}, | ||
{ | ||
"id": "legacy-browsers", | ||
"active": [ | ||
"ie_pre_9" | ||
] | ||
}, | ||
{ | ||
"id": "localhost", | ||
"active": false | ||
}, | ||
{ | ||
"id": "web-crawlers", | ||
"active": false | ||
} | ||
]`) | ||
}) | ||
|
||
ctx := context.Background() | ||
filters, _, err := client.ProjectInboundDataFilters.List(ctx, "organization_slug", "project_slug") | ||
assert.NoError(t, err) | ||
|
||
expected := []*ProjectInboundDataFilter{ | ||
{ | ||
ID: "browser-extensions", | ||
Active: BoolOrStringSlice{IsBool: true, BoolVal: false}, | ||
}, | ||
{ | ||
ID: "filtered-transaction", | ||
Active: BoolOrStringSlice{IsBool: true, BoolVal: true}, | ||
}, | ||
{ | ||
ID: "legacy-browsers", | ||
Active: BoolOrStringSlice{IsBool: false, SliceVal: []string{"ie_pre_9"}}, | ||
}, | ||
{ | ||
ID: "localhost", | ||
Active: BoolOrStringSlice{IsBool: true, BoolVal: false}, | ||
}, | ||
{ | ||
ID: "web-crawlers", | ||
Active: BoolOrStringSlice{IsBool: true, BoolVal: false}, | ||
}, | ||
} | ||
assert.Equal(t, expected, filters) | ||
} | ||
|
||
func TestProjectInboundDataFiltersService_UpdateActive(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/api/0/projects/organization_slug/project_slug/filters/filter_id/", func(w http.ResponseWriter, r *http.Request) { | ||
assertMethod(t, http.MethodPut, r) | ||
assertPostJSON(t, map[string]interface{}{ | ||
"active": true, | ||
}, r) | ||
}) | ||
|
||
ctx := context.Background() | ||
params := &UpdateProjectInboundDataFilterParams{ | ||
Active: Bool(true), | ||
} | ||
_, err := client.ProjectInboundDataFilters.Update(ctx, "organization_slug", "project_slug", "filter_id", params) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestProjectInboundDataFiltersService_UpdateSubfilters(t *testing.T) { | ||
client, mux, _, teardown := setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/api/0/projects/organization_slug/project_slug/filters/filter_id/", func(w http.ResponseWriter, r *http.Request) { | ||
assertMethod(t, http.MethodPut, r) | ||
assertPostJSON(t, map[string]interface{}{ | ||
"subfilters": []interface{}{"ie_pre_9"}, | ||
}, r) | ||
}) | ||
|
||
ctx := context.Background() | ||
params := &UpdateProjectInboundDataFilterParams{ | ||
Subfilters: []string{"ie_pre_9"}, | ||
} | ||
_, err := client.ProjectInboundDataFilters.Update(ctx, "organization_slug", "project_slug", "filter_id", params) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package sentry | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// BoolOrStringSlice is a type that can be unmarshaled from either a bool or a | ||
// string slice. | ||
type BoolOrStringSlice struct { | ||
IsBool bool | ||
BoolVal bool | ||
SliceVal []string | ||
} | ||
|
||
var _ json.Unmarshaler = (*BoolOrStringSlice)(nil) | ||
|
||
// UnmarshalJSON implements json.Unmarshaler. | ||
func (bos *BoolOrStringSlice) UnmarshalJSON(data []byte) error { | ||
// Try to unmarshal as a bool | ||
var boolVal bool | ||
if err := json.Unmarshal(data, &boolVal); err == nil { | ||
bos.IsBool = true | ||
bos.BoolVal = boolVal | ||
return nil | ||
} | ||
|
||
// Try to unmarshal as a string slice | ||
var sliceVal []string | ||
if err := json.Unmarshal(data, &sliceVal); err == nil { | ||
bos.IsBool = false | ||
bos.SliceVal = sliceVal | ||
return nil | ||
} | ||
|
||
// If neither worked, return an error | ||
return fmt.Errorf("unable to unmarshal as bool or string slice: %s", string(data)) | ||
} |