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

update castle sdk remove deprecated fields #26

Merged
merged 9 commits into from
Nov 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 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 @@ -71,14 +72,23 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
r := &castleAPIRequest{
params := Params{
Email: req.User.Email,
Username: req.User.ID,
}
createdAt := req.CreatedAt
if createdAt.IsZero() {
createdAt = time.Now()
}
r := &castleFilterAPIRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
RequestToken: req.Context.RequestToken,
User: req.User,
Params: params,
Context: req.Context,
Properties: req.Properties,
CreatedAt: createdAt,
}
return c.sendCall(ctx, r, FilterEndpoint)
}
Expand All @@ -92,19 +102,24 @@ func (c *Castle) Risk(ctx context.Context, req *Request) (RecommendedAction, err
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
r := &castleAPIRequest{
createdAt := req.CreatedAt
if createdAt.IsZero() {
createdAt = time.Now()
}
r := &castleRiskAPIRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
RequestToken: req.Context.RequestToken,
User: req.User,
Context: req.Context,
Properties: req.Properties,
CreatedAt: createdAt,
}
return c.sendCall(ctx, r, RiskEndpoint)
}

func (c *Castle) sendCall(ctx context.Context, r *castleAPIRequest, url string) (_ RecommendedAction, err error) {
func (c *Castle) sendCall(ctx context.Context, r castleAPIRequest, url string) (_ RecommendedAction, err error) {
defer func() {
if !c.metricsEnabled {
return
Expand All @@ -118,7 +133,13 @@ func (c *Castle) sendCall(ctx context.Context, r *castleAPIRequest, url string)
}()

b := new(bytes.Buffer)
err = json.NewEncoder(b).Encode(r)

switch request := r.(type) {
case *castleFilterAPIRequest, *castleRiskAPIRequest:
pansbro12 marked this conversation as resolved.
Show resolved Hide resolved
err = json.NewEncoder(b).Encode(request)
default:
err = fmt.Errorf("incorrect request type passed as argument.")
}
if err != nil {
return RecommendedActionNone, err
}
Expand Down
8 changes: 5 additions & 3 deletions 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 All @@ -32,9 +33,11 @@ func configureRequest(httpReq *http.Request) *castle.Request {
},
User: castle.User{
ID: "user-id",
Email: "[email protected]",
Traits: map[string]string{"trait1": "traitValue1"},
},
Properties: map[string]string{"prop1": "propValue1"},
CreatedAt: time.Now(),
}
}

Expand Down Expand Up @@ -150,7 +153,7 @@ func TestCastle_Filter(t *testing.T) {
Type castle.EventType `json:"type"`
Status castle.EventStatus `json:"status"`
RequestToken string `json:"request_token"`
User castle.User `json:"user"`
Params castle.Params `json:"params"`
Context *castle.Context `json:"context"`
Properties map[string]string `json:"properties"`
}
Expand All @@ -168,9 +171,8 @@ func TestCastle_Filter(t *testing.T) {

assert.Equal(t, castle.EventTypeLogin, reqData.Type)
assert.Equal(t, castle.EventStatusSucceeded, reqData.Status)
assert.Equal(t, "user-id", reqData.User.ID)
assert.Equal(t, "user@test.com", reqData.Params.Email)
assert.Equal(t, map[string]string{"prop1": "propValue1"}, reqData.Properties)
assert.Equal(t, map[string]string{"trait1": "traitValue1"}, reqData.User.Traits)
assert.Equal(t, castle.FromHTTPRequest(httpReq), reqData.Context)

executed = true
Expand Down
35 changes: 34 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package castle

import "time"

type Event struct {
EventType EventType
EventStatus EventStatus
Expand Down Expand Up @@ -50,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 @@ -68,14 +71,44 @@ type User struct {
Traits map[string]string `json:"traits,omitempty"`
}

type castleAPIRequest struct {
type Params struct {
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Username string `json:"username,omitempty"`
}

type castleAPIRequest interface {
GetEventType() EventType
}

type castleFilterAPIRequest struct {
Type EventType `json:"type"`
Name string `json:"name,omitempty"`
Status EventStatus `json:"status"`
RequestToken string `json:"request_token"`
Params Params `json:"params"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties,omitempty"`
CreatedAt time.Time `json:"created_at"`
}

func (r *castleFilterAPIRequest) GetEventType() EventType {
return r.Type
}

type castleRiskAPIRequest struct {
Type EventType `json:"type"`
Name string `json:"name,omitempty"`
Status EventStatus `json:"status"`
RequestToken string `json:"request_token"`
User User `json:"user"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties,omitempty"`
CreatedAt time.Time `json:"created_at"`
}

func (r *castleRiskAPIRequest) GetEventType() EventType {
return r.Type
}

type castleAPIResponse struct {
Expand Down
Loading