mirror of
https://github.com/chaitin/SafeLine.git
synced 2026-01-31 13:53:33 +08:00
fix: add behavior source (#843)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -64,7 +65,8 @@ func (h *SafelineHandler) Exist(c *gin.Context) {
|
||||
}
|
||||
|
||||
type BehaviorReq struct {
|
||||
Type service.BehaviorType `json:"type"`
|
||||
Source string `json:"source"`
|
||||
Type service.BehaviorType `json:"type"`
|
||||
}
|
||||
|
||||
// Behavior record user behavior
|
||||
@@ -78,12 +80,23 @@ type BehaviorReq struct {
|
||||
// @Router /behavior [post]
|
||||
func (h *SafelineHandler) Behavior(c *gin.Context) {
|
||||
req := &BehaviorReq{}
|
||||
if err := c.ShouldBindJSON(req); err != nil {
|
||||
if err := c.BindJSON(req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
err := h.safelineService.PostBehavior(c, req.Type)
|
||||
if req.Type >= service.BehaviorTypeMax || req.Type <= service.BehaviorTypeMin {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid behavior type"})
|
||||
return
|
||||
}
|
||||
|
||||
byteReq, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
err = h.safelineService.PostBehavior(c, byteReq)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
@@ -111,10 +112,8 @@ const (
|
||||
BehaviorTypeMax
|
||||
)
|
||||
|
||||
func (s *SafelineService) PostBehavior(ctx context.Context, behaviorType BehaviorType) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.APIHost+"/api/v1/public/safeline/behavior",
|
||||
strings.NewReader(fmt.Sprintf(`{"type": %d}`, behaviorType)),
|
||||
)
|
||||
func (s *SafelineService) PostBehavior(ctx context.Context, body []byte) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.APIHost+"/api/v1/public/safeline/behavior", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user