Skip to content

Commit

Permalink
Escape query passed in graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
toini committed Feb 19, 2020
1 parent 930c858 commit 368ab6d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tibber

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -181,9 +182,18 @@ func (ts *Stream) sendInitMsg() {
ts.client.WriteMessage(websocket.TextMessage, []byte(init))
}

func jsonEscape(i string) string {
b, err := json.Marshal(i)
if err != nil {
panic(err)
}
// Trim the beginning and trailing " character
return string(b[1 : len(b)-1])
}

func (ts *Stream) sendSubMsg() {
homeID := ts.ID
sub := fmt.Sprintf(`
var subscriptionQuery = fmt.Sprintf(`
subscription {
liveMeasurement(homeId:"%s") {
timestamp
Expand All @@ -207,6 +217,17 @@ func (ts *Stream) sendSubMsg() {
currentPhase2
currentPhase3
}
}`, homeID)
}`,
homeID)

sub := fmt.Sprintf(`
{
"query": "%s",
"variables":null,
"type":"subscription_start",
"id":0
}`, jsonEscape(subscriptionQuery))

log.Debug("Subscribe with query", sub)
ts.client.WriteMessage(websocket.TextMessage, []byte(sub))
}

0 comments on commit 368ab6d

Please sign in to comment.