Skip to content

Commit

Permalink
add query for prices
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddyP committed Jan 27, 2024
1 parent 6651743 commit 966a56e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package main
func main() {
queryUserExample()
queryConsumptionExample()
queryPriceExample()
setupWebsocket()
}
27 changes: 27 additions & 0 deletions examples/queryPrice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"context"
"fmt"
"log/slog"
"os"

"github.com/SiddyP/gotibber/tibber"
)

func queryPriceExample() {

ctx := context.Background()

t := tibber.Client{
APIClient: tibber.NewAPIClient(&tibber.APIConfig{
Token: os.Getenv("TIBBER_API_TOKEN"),
URL: "https://api.tibber.com/v1-beta/gql",
}),
Logger: slog.Default(),
}

p := t.QueryPrice(ctx, &tibber.Price{})

fmt.Printf("Price: %v\n", p.Viewer.Homes)
}
2 changes: 1 addition & 1 deletion tibber/apiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestClient(t *testing.T) {
func TestCreateClient(t *testing.T) {
assert := assert.New(t)

client := NewAPIClient(&APIConfig{
Expand Down
43 changes: 43 additions & 0 deletions tibber/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Consumption struct {
Last int
}

type Price struct {
}

type WebsocketSubscriptionUrl struct {
}

Expand Down Expand Up @@ -91,3 +94,43 @@ func (q *Consumption) query(ctx context.Context, t *Client) HomeConsumptionRespo
}
return h
}

func (p *Price) query(ctx context.Context, t *Client) PriceResponse {
req := graphql.NewRequest(`
query {
viewer {
homes {
currentSubscription{
priceInfo{
current{
total
energy
tax
startsAt
}
today {
total
energy
tax
startsAt
}
tomorrow {
total
energy
tax
startsAt
}
}
}
}
}
}
`)
setHeaders(req, t)

var price PriceResponse
if err := t.APIClient.GQLClient.Run(ctx, req, &price); err != nil {
log.Fatal(err)
}
return price
}
29 changes: 29 additions & 0 deletions tibber/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,32 @@ type HomeConsumptionResponse struct {
} `json:"homes"`
} `json:"viewer"`
}

type PriceResponse struct {
Viewer struct {
Homes []struct {
CurrentSubscription struct {
PriceInfo struct {
Today []struct {
StartsAt time.Time `json:"startsAt"`
Total float64 `json:"total"`
Energy float64 `json:"energy"`
Tax float64 `json:"tax"`
} `json:"today"`
Tomorrow []struct {
StartsAt time.Time `json:"startsAt"`
Total float64 `json:"total"`
Energy float64 `json:"energy"`
Tax float64 `json:"tax"`
} `json:"tomorrow"`
Current struct {
StartsAt time.Time `json:"startsAt"`
Total float64 `json:"total"`
Energy float64 `json:"energy"`
Tax float64 `json:"tax"`
} `json:"current"`
} `json:"priceInfo"`
} `json:"currentSubscription"`
} `json:"homes"`
} `json:"viewer"`
}
4 changes: 4 additions & 0 deletions tibber/tibber.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (t *Client) QueryWebsocketSubscriptionUrl(ctx context.Context, w *Websocket
return w.query(ctx, t)
}

func (t *Client) QueryPrice(ctx context.Context, p *Price) PriceResponse {
return p.query(ctx, t)
}

// Subscribe to Tibber websocket
func (t *Client) Subscribe(ctx context.Context) {
defer t.Wg.Done()
Expand Down
2 changes: 1 addition & 1 deletion tibber/tibber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestTibberClient(t *testing.T) {
func TestCreateTibberClient(t *testing.T) {
assert := assert.New(t)

tC := Client{
Expand Down

0 comments on commit 966a56e

Please sign in to comment.