Skip to content

Commit

Permalink
return error on incorrect request type and default created_at value
Browse files Browse the repository at this point in the history
  • Loading branch information
pansbro12 committed Nov 6, 2024
1 parent 399f110 commit a8c9229
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -75,6 +76,10 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
Email: req.User.Email,
Username: req.User.Name,
}
createdAt := req.CreatedAt
if req.CreatedAt.IsZero() {
createdAt = time.Now()
}
r := &castleFilterAPIRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Expand All @@ -83,7 +88,7 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
Params: params,
Context: req.Context,
Properties: req.Properties,
CreatedAt: req.CreatedAt,
CreatedAt: createdAt,
}
return c.sendCall(ctx, r, FilterEndpoint)
}
Expand All @@ -97,6 +102,10 @@ func (c *Castle) Risk(ctx context.Context, req *Request) (RecommendedAction, err
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
createdAt := req.CreatedAt
if req.CreatedAt.IsZero() {
createdAt = time.Now()
}
r := &castleRiskAPIRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Expand All @@ -105,7 +114,7 @@ func (c *Castle) Risk(ctx context.Context, req *Request) (RecommendedAction, err
User: req.User,
Context: req.Context,
Properties: req.Properties,
CreatedAt: req.CreatedAt,
CreatedAt: createdAt,
}
return c.sendCall(ctx, r, RiskEndpoint)
}
Expand All @@ -128,6 +137,8 @@ func (c *Castle) sendCall(ctx context.Context, r castleAPIRequest, url string) (
switch request := r.(type) {
case *castleFilterAPIRequest, *castleRiskAPIRequest:
err = json.NewEncoder(b).Encode(request)
default:
err = fmt.Errorf("incorrect request type passed as argument.")
}
if err != nil {
return RecommendedActionNone, err
Expand Down

0 comments on commit a8c9229

Please sign in to comment.