fix: add behavior source (#843)

This commit is contained in:
dhsifss
2024-04-24 18:31:52 +08:00
committed by GitHub
parent 4a7ad7527e
commit 6e04f0b10f
2 changed files with 19 additions and 7 deletions

View File

@@ -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

View File

@@ -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
}