vk-webhook-flooder-mock/handler/vk.go

52 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-01-13 13:28:05 +03:00
package handler
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"strconv"
"time"
"github.com/SevereCloud/vksdk/v2/api"
"github.com/SevereCloud/vksdk/v2/object"
"github.com/gin-gonic/gin"
"vk-webhook-flooder-mock/dto"
)
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
func UsersGetHandler(c *gin.Context) {
// Simulate real network latency + VK API latency.
time.Sleep(time.Millisecond * time.Duration(rnd.Intn(150)+200))
var req dto.UsersGetRequest
if err := c.ShouldBind(&req); err != nil {
log.Printf("invalid users.get request: %s", err)
c.String(http.StatusBadRequest, "Invalid request data")
return
}
val := api.UsersGetResponse{}
for _, id := range req.UserIDs {
uID, _ := strconv.Atoi(id)
val = append(val, object.UsersUser{
ID: uID,
FirstName: "Mocked",
LastName: fmt.Sprintf("Profile #%s", id),
})
}
data, err := json.Marshal(val)
if err != nil {
c.Error(err)
return
}
var res api.Response
res.Response = data
c.JSON(http.StatusOK, res)
}