diff options
Diffstat (limited to 'vendor/github.com/SevereCloud/vksdk/v2/longpoll-bot/longpoll.go')
-rw-r--r-- | vendor/github.com/SevereCloud/vksdk/v2/longpoll-bot/longpoll.go | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/vendor/github.com/SevereCloud/vksdk/v2/longpoll-bot/longpoll.go b/vendor/github.com/SevereCloud/vksdk/v2/longpoll-bot/longpoll.go index c4a3efec..6cde105a 100644 --- a/vendor/github.com/SevereCloud/vksdk/v2/longpoll-bot/longpoll.go +++ b/vendor/github.com/SevereCloud/vksdk/v2/longpoll-bot/longpoll.go @@ -8,8 +8,11 @@ package longpoll // import "github.com/SevereCloud/vksdk/v2/longpoll-bot" import ( "context" "encoding/json" + "errors" "fmt" + "io" "net/http" + "strconv" "github.com/SevereCloud/vksdk/v2" "github.com/SevereCloud/vksdk/v2/api" @@ -117,7 +120,7 @@ func (lp *LongPoll) check(ctx context.Context) (response Response, err error) { } defer resp.Body.Close() - err = json.NewDecoder(resp.Body).Decode(&response) + response, err = parseResponse(resp.Body) if err != nil { return response, err } @@ -127,6 +130,59 @@ func (lp *LongPoll) check(ctx context.Context) (response Response, err error) { return response, err } +func parseResponse(reader io.Reader) (response Response, err error) { + decoder := json.NewDecoder(reader) + for decoder.More() { + token, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + break + } + + return response, err + } + + t, ok := token.(string) + if !ok { + continue + } + + switch t { + case "failed": + raw, err := decoder.Token() + if err != nil { + return response, err + } + + response.Failed = int(raw.(float64)) + case "updates": + var updates []events.GroupEvent + + err = decoder.Decode(&updates) + if err != nil { + return response, err + } + + response.Updates = updates + case "ts": + // can be a number in the response with "failed" field: {"ts":8,"failed":1} + // or string, e.g. {"ts":"8","updates":[]} + rawTs, err := decoder.Token() + if err != nil { + return response, err + } + + if ts, isNumber := rawTs.(float64); isNumber { + response.Ts = strconv.Itoa(int(ts)) + } else { + response.Ts = rawTs.(string) + } + } + } + + return response, err +} + func (lp *LongPoll) checkResponse(response Response) (err error) { switch response.Failed { case 0: |