Last active
December 3, 2021 07:29
-
-
Save peterhellberg/1a9e13f8e451a9789394f378dccc3683 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"flag" | |
"os" | |
"time" | |
"github.com/c7/graphql" | |
) | |
const endpoint = "https://api.tibber.com/v1-beta/gql" | |
const query = `{ | |
viewer { | |
name | |
homes { | |
address { | |
address1 | |
postalCode | |
city | |
country | |
latitude | |
longitude | |
} | |
consumption(resolution: HOURLY, last: 24, filterEmptyNodes: true) { | |
nodes { | |
from | |
to | |
cost | |
unitPrice | |
unitPriceVAT | |
consumption | |
consumptionUnit | |
} | |
} | |
} | |
} | |
}` | |
type Response struct { | |
Viewer struct { | |
Name string `json:"name"` | |
Homes []Home `json:"homes"` | |
} `json:"viewer"` | |
} | |
type Home struct { | |
Address struct { | |
Address1 string `json:"address1"` | |
PostalCode string `json:"postalCode"` | |
City string `json:"city"` | |
Country string `json:"country"` | |
Latitude string `json:"latitude"` | |
Longitude string `json:"longitude"` | |
} `json:"address"` | |
Consumption struct { | |
Nodes []Node `json:"nodes"` | |
} `json:"consumption"` | |
} | |
type Node struct { | |
From time.Time `json:"from"` | |
To time.Time `json:"to"` | |
Cost float64 `json:"cost"` | |
UnitPrice float64 `json:"unitPrice"` | |
UnitPriceVAT float64 `json:"unitPriceVAT"` | |
Consumption float64 `json:"consumption"` | |
ConsumptionUnit string `json:"consumptionUnit"` | |
} | |
func main() { | |
var token string | |
flag.StringVar(&token, "token", "", "") | |
flag.Parse() | |
ctx := context.Background() | |
gql := graphql.NewClient(endpoint) | |
req := graphql.NewRequest(query) | |
req.Header.Add("Authorization", "Bearer "+token) | |
var resp Response | |
if err := gql.Run(ctx, req, &resp); err != nil { | |
panic(err) | |
} | |
json.NewEncoder(os.Stdout).Encode(&resp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example response