Skip to content

Commit

Permalink
include created at in request
Browse files Browse the repository at this point in the history
  • Loading branch information
pansbro12 committed Nov 5, 2024
1 parent 0ee71f4 commit 399f110
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
13 changes: 5 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
user_params := UserParams{
params := Params{
Email: req.User.Email,
Username: req.User.Name,
}
Expand All @@ -81,10 +80,10 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
Name: req.Event.Name,
Status: req.Event.EventStatus,
RequestToken: req.Context.RequestToken,
Params: user_params,
Params: params,
Context: req.Context,
Properties: req.Properties,
CreatedAt: time.Now(),
CreatedAt: req.CreatedAt,
}
return c.sendCall(ctx, r, FilterEndpoint)
}
Expand All @@ -106,7 +105,7 @@ func (c *Castle) Risk(ctx context.Context, req *Request) (RecommendedAction, err
User: req.User,
Context: req.Context,
Properties: req.Properties,
CreatedAt: time.Now(),
CreatedAt: req.CreatedAt,
}
return c.sendCall(ctx, r, RiskEndpoint)
}
Expand All @@ -127,9 +126,7 @@ func (c *Castle) sendCall(ctx context.Context, r castleAPIRequest, url string) (
b := new(bytes.Buffer)

switch request := r.(type) {
case *castleRiskAPIRequest:
err = json.NewEncoder(b).Encode(request)
case *castleFilterAPIRequest:
case *castleFilterAPIRequest, *castleRiskAPIRequest:
err = json.NewEncoder(b).Encode(request)
}
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -36,6 +37,7 @@ func configureRequest(httpReq *http.Request) *castle.Request {
Traits: map[string]string{"trait1": "traitValue1"},
},
Properties: map[string]string{"prop1": "propValue1"},
CreatedAt: time.Now(),
}
}

Expand Down Expand Up @@ -151,7 +153,7 @@ func TestCastle_Filter(t *testing.T) {
Type castle.EventType `json:"type"`
Status castle.EventStatus `json:"status"`
RequestToken string `json:"request_token"`
Params castle.UserParams `json:"params"`
Params castle.Params `json:"params"`
Context *castle.Context `json:"context"`
Properties map[string]string `json:"properties"`
}
Expand Down
5 changes: 3 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Request struct {
Event Event
User User
Properties map[string]string
CreatedAt time.Time
}

// Context captures data from HTTP request.
Expand All @@ -70,7 +71,7 @@ type User struct {
Traits map[string]string `json:"traits,omitempty"`
}

type UserParams struct {
type Params struct {
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Username string `json:"username,omitempty"`
Expand All @@ -85,7 +86,7 @@ type castleFilterAPIRequest struct {
Name string `json:"name,omitempty"`
Status EventStatus `json:"status"`
RequestToken string `json:"request_token"`
Params UserParams `json:"params"`
Params Params `json:"params"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties,omitempty"`
CreatedAt time.Time `json:"created_at"`
Expand Down

0 comments on commit 399f110

Please sign in to comment.