parse challenge tokens from received response

see #482
This commit is contained in:
Bernhard B
2024-03-22 15:52:12 +01:00
parent bc14eccf7d
commit ce556f0a73

View File

@@ -7,6 +7,7 @@ import (
"net"
"time"
"sync"
"strings"
"github.com/bbernhard/signal-cli-rest-api/utils"
uuid "github.com/gofrs/uuid"
@@ -17,6 +18,7 @@ import (
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data"`
}
type JsonRpc2MessageResponse struct {
@@ -31,6 +33,18 @@ type JsonRpc2ReceivedMessage struct {
Err Error `json:"error"`
}
type RateLimitMessage struct {
Response RateLimitResponse `json:"response"`
}
type RateLimitResponse struct {
Results []RateLimitResult `json:"results"`
}
type RateLimitResult struct {
Token string `json:"token"`
}
type JsonRpc2Client struct {
conn net.Conn
receivedResponsesById map[string]chan JsonRpc2MessageResponse
@@ -125,6 +139,20 @@ func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{
log.Debug("json-rpc response error: ", string(resp.Err.Message))
if resp.Err.Code != 0 {
log.Debug("json-rpc command error code: ", resp.Err.Code)
if resp.Err.Code == -5 {
var rateLimitMessage RateLimitMessage
err = json.Unmarshal(resp.Err.Data, &rateLimitMessage)
if err != nil {
return "", errors.New(resp.Err.Message + " (Couldn't parse JSON for more details")
}
challengeTokens := []string{}
for _, rateLimitResult := range rateLimitMessage.Response.Results {
challengeTokens = append(challengeTokens, rateLimitResult.Token)
}
return "", errors.New(resp.Err.Message + " Challenge Tokens: " + strings.Join(challengeTokens, ","))
}
return "", errors.New(resp.Err.Message)
}