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

demo generics #27

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
20 changes: 14 additions & 6 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,16 +72,21 @@ 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{
user_params := UserParams{
Email: req.User.Email,
Username: req.User.Name,
}
r := &FilterRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
RequestToken: req.Context.RequestToken,
User: req.User,
Params: user_params,
Context: req.Context,
Properties: req.Properties,
CreatedAt: time.Now(),
}
return c.sendCall(ctx, r, FilterEndpoint)
return sendCall(ctx, c, r, FilterEndpoint)
}

// Risk sends a risk request to castle.io
Expand All @@ -92,19 +98,20 @@ 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{
r := &RiskRequest{
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: time.Now(),
}
return c.sendCall(ctx, r, RiskEndpoint)
return sendCall(ctx, c, r, RiskEndpoint)
}

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

b := new(bytes.Buffer)

err = json.NewEncoder(b).Encode(r)
if err != nil {
return RecommendedActionNone, err
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ 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"},
Expand Down Expand Up @@ -150,7 +151,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.UserParams `json:"params"`
Context *castle.Context `json:"context"`
Properties map[string]string `json:"properties"`
}
Expand All @@ -168,9 +169,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
26 changes: 25 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 @@ -68,14 +70,36 @@ type User struct {
Traits map[string]string `json:"traits,omitempty"`
}

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

type castleAPIRequest interface {
*FilterRequest | *RiskRequest
}

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

type RiskRequest 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"`
}

type castleAPIResponse struct {
Expand Down
Loading