Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active December 3, 2021 07:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/1a9e13f8e451a9789394f378dccc3683 to your computer and use it in GitHub Desktop.
Save peterhellberg/1a9e13f8e451a9789394f378dccc3683 to your computer and use it in GitHub Desktop.
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)
}
@peterhellberg
Copy link
Author

Example response

{
  "viewer": {
    "name": "Peter Vilhelm Hellberg",
    "homes": [
      {
        "address": {
          "address1": "Mosstenabacken 1L",
          "postalCode": "12432",
          "city": "Bandhagen",
          "country": "SE",
          "latitude": "59.2708586",
          "longitude": "18.048243"
        },
        "consumption": {
          "nodes": [
            {
              "from": "2020-08-13T14:00:00+02:00",
              "to": "2020-08-13T15:00:00+02:00",
              "cost": 0.079468,
              "unitPrice": 0.496675,
              "unitPriceVAT": 0.099335,
              "consumption": 0.16,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T15:00:00+02:00",
              "to": "2020-08-13T16:00:00+02:00",
              "cost": 0.06653725,
              "unitPrice": 0.511825,
              "unitPriceVAT": 0.102365,
              "consumption": 0.13,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T16:00:00+02:00",
              "to": "2020-08-13T17:00:00+02:00",
              "cost": 0.042065,
              "unitPrice": 0.5258125,
              "unitPriceVAT": 0.1051625,
              "consumption": 0.08,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T17:00:00+02:00",
              "to": "2020-08-13T18:00:00+02:00",
              "cost": 0.04571,
              "unitPrice": 0.571375,
              "unitPriceVAT": 0.114275,
              "consumption": 0.08,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T18:00:00+02:00",
              "to": "2020-08-13T19:00:00+02:00",
              "cost": 0.052774,
              "unitPrice": 0.659675,
              "unitPriceVAT": 0.131935,
              "consumption": 0.08,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T19:00:00+02:00",
              "to": "2020-08-13T20:00:00+02:00",
              "cost": 0.120632,
              "unitPrice": 0.7096,
              "unitPriceVAT": 0.14192,
              "consumption": 0.17,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T20:00:00+02:00",
              "to": "2020-08-13T21:00:00+02:00",
              "cost": 0.13553925,
              "unitPrice": 0.645425,
              "unitPriceVAT": 0.129085,
              "consumption": 0.21,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T21:00:00+02:00",
              "to": "2020-08-13T22:00:00+02:00",
              "cost": 0.12817575,
              "unitPrice": 0.474725,
              "unitPriceVAT": 0.094945,
              "consumption": 0.27,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T22:00:00+02:00",
              "to": "2020-08-13T23:00:00+02:00",
              "cost": 0.0689175,
              "unitPrice": 0.3445875,
              "unitPriceVAT": 0.0689175,
              "consumption": 0.2,
              "consumptionUnit": "kWh"
            },
            {
              "from": "2020-08-13T23:00:00+02:00",
              "to": "2020-08-14T00:00:00+02:00",
              "cost": 0.036215,
              "unitPrice": 0.181075,
              "unitPriceVAT": 0.036215,
              "consumption": 0.2,
              "consumptionUnit": "kWh"
            }
          ]
        }
      }
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment