Skip to content

Commit

Permalink
demo generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarota committed Nov 5, 2024
1 parent 0ee71f4 commit 6f902ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
17 changes: 6 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
Email: req.User.Email,
Username: req.User.Name,
}
r := &castleFilterAPIRequest{
r := &FilterRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
Expand All @@ -86,7 +86,7 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
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 @@ -98,7 +98,7 @@ 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 := &castleRiskAPIRequest{
r := &RiskRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
Expand All @@ -108,10 +108,10 @@ func (c *Castle) Risk(ctx context.Context, req *Request) (RecommendedAction, err
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 @@ -126,12 +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:
err = json.NewEncoder(b).Encode(request)
}
err = json.NewEncoder(b).Encode(r)
if err != nil {
return RecommendedActionNone, err
}
Expand Down
14 changes: 3 additions & 11 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ type UserParams struct {
}

type castleAPIRequest interface {
GetEventType() EventType
*FilterRequest | *RiskRequest
}

type castleFilterAPIRequest struct {
type FilterRequest struct {
Type EventType `json:"type"`
Name string `json:"name,omitempty"`
Status EventStatus `json:"status"`
Expand All @@ -91,11 +91,7 @@ type castleFilterAPIRequest struct {
CreatedAt time.Time `json:"created_at"`
}

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

type castleRiskAPIRequest struct {
type RiskRequest struct {
Type EventType `json:"type"`
Name string `json:"name,omitempty"`
Status EventStatus `json:"status"`
Expand All @@ -106,10 +102,6 @@ type castleRiskAPIRequest struct {
CreatedAt time.Time `json:"created_at"`
}

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

type castleAPIResponse struct {
Type string `json:"type"`
Message string `json:"message"`
Expand Down

0 comments on commit 6f902ad

Please sign in to comment.