Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save token when it is refreshed #63

Merged
merged 3 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/prometheus/client_golang v1.17.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
golang.org/x/oauth2 v0.22.0
golang.org/x/oauth2 v0.23.0
)

require (
Expand All @@ -24,4 +24,4 @@ require (
google.golang.org/protobuf v1.31.0 // indirect
)

replace github.com/exzz/netatmo-api-go => github.com/xperimental/netatmo-api-go v0.0.0-20220927234935-2a059c20f221
replace github.com/exzz/netatmo-api-go => github.com/xperimental/netatmo-api-go v0.0.0-20241013210700-5e7f333f57e8
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xperimental/netatmo-api-go v0.0.0-20220927234935-2a059c20f221 h1:LUYfr5cWfISCy/zxiz3vabUkH6iLSRTkRtsRDma607Q=
github.com/xperimental/netatmo-api-go v0.0.0-20220927234935-2a059c20f221/go.mod h1:6OMZxfbY7EZh+Q5qn/cPrRgCp+HRyBld5w05JzX8ddA=
golang.org/x/oauth2 v0.22.0 h1:BzDx2FehcG7jJwgWLELCdmLuxk2i+x9UDpSiss2u0ZA=
golang.org/x/oauth2 v0.22.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
github.com/xperimental/netatmo-api-go v0.0.0-20241013210700-5e7f333f57e8 h1:U7Kjez/H8bg/1/IqKcSF7ypfTw/nfO19H4chk8Z7sRo=
github.com/xperimental/netatmo-api-go v0.0.0-20241013210700-5e7f333f57e8/go.mod h1:+Vj12rSUvfxn8lgFGlxHmymmLdUR/3qkp6fG9r2UHGk=
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
Expand Down
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"syscall"
"time"

"github.com/exzz/netatmo-api-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"golang.org/x/oauth2"

"github.com/exzz/netatmo-api-go"
"github.com/xperimental/netatmo-exporter/v2/internal/collector"
"github.com/xperimental/netatmo-exporter/v2/internal/config"
"github.com/xperimental/netatmo-exporter/v2/internal/logger"
Expand Down Expand Up @@ -45,7 +45,7 @@ func main() {
log.SetLevel(logrus.Level(cfg.LogLevel))

log.Infof("netatmo-exporter %s (commit: %s)", Version, GitCommit)
client := netatmo.NewClient(cfg.Netatmo)
client := netatmo.NewClient(cfg.Netatmo, tokenUpdated(cfg.TokenFile))

if cfg.TokenFile != "" {
token, err := loadToken(cfg.TokenFile)
Expand Down Expand Up @@ -128,6 +128,20 @@ func registerSignalHandler(client *netatmo.Client, fileName string) {
}()
}

func tokenUpdated(fileName string) netatmo.TokenUpdateFunc {
if fileName == "" {
return nil
}

return func(token *oauth2.Token) {
log.Debugf("Token updated. Expires: %s", token.Expiry)

if err := saveTokenFile(fileName, token); err != nil {
log.Errorf("Error saving token: %s", err)
}
}
}

func saveToken(client *netatmo.Client, fileName string) error {
token, err := client.CurrentToken()
switch {
Expand All @@ -139,6 +153,10 @@ func saveToken(client *netatmo.Client, fileName string) error {
}

log.Infof("Saving token to %s ...", fileName)
return saveTokenFile(fileName, token)
}

func saveTokenFile(fileName string, token *oauth2.Token) error {
data, err := json.Marshal(token)
if err != nil {
return fmt.Errorf("error marshalling token: %w", err)
Expand Down
Loading