diff --git a/client.go b/client.go index d01cd02..f24c42f 100644 --- a/client.go +++ b/client.go @@ -28,8 +28,8 @@ type Client struct { ts oauth2.TokenSource } -// NewClient creates a new Tesla API client. You must provided one of WithToken or WithTokenFile -// functional options to initialize the client with an OAuth token. +// NewClient creates a new Tesla API client. You must provided a WithTokenSource +// option to initialize the client with an OAuth token. func NewClient(ctx context.Context, options ...ClientOption) (*Client, error) { client := &Client{ baseURL: FleetAudienceNA + "api/1", diff --git a/examples/manage_car.go b/examples/manage_car.go index 5e9e2f8..5d0ded5 100644 --- a/examples/manage_car.go +++ b/examples/manage_car.go @@ -2,15 +2,37 @@ package main import ( "context" + "encoding/json" + "flag" "fmt" "os" - "github.com/bogosj/tesla" + "golang.org/x/oauth2" + + tesla "github.com/evcc-io/tesla-proxy-client" ) +var tokenPath = flag.String("token", "", "path to token file") + func main() { + flag.Parse() + + if *tokenPath == "" { + fmt.Println("--token must be specified") + os.Exit(1) + } + ctx := context.Background() - client, err := tesla.NewClient(ctx, tesla.WithTokenFile("/file/path/to/token.json")) + b, err := os.ReadFile(*tokenPath) + if err != nil { + panic(err) + } + var tok *oauth2.Token + if err := json.Unmarshal(b, &tok); err != nil { + panic(err) + } + + client, err := tesla.NewClient(ctx, tesla.WithTokenSource(oauth2.StaticTokenSource(tok))) if err != nil { panic(err) } diff --git a/examples/set_battery_reserve/main.go b/examples/set_battery_reserve/main.go index 9d104da..5d8c3b0 100644 --- a/examples/set_battery_reserve/main.go +++ b/examples/set_battery_reserve/main.go @@ -2,11 +2,14 @@ package main import ( "context" + "encoding/json" "flag" "fmt" "os" - "github.com/bogosj/tesla" + "golang.org/x/oauth2" + + tesla "github.com/evcc-io/tesla-proxy-client" ) var tokenPath = flag.String("token", "", "path to token file") @@ -27,7 +30,16 @@ func main() { } func run(ctx context.Context, tokenPath string) error { - c, err := tesla.NewClient(ctx, tesla.WithTokenFile(tokenPath)) + b, err := os.ReadFile(tokenPath) + if err != nil { + return err + } + var tok *oauth2.Token + if err := json.Unmarshal(b, &tok); err != nil { + return err + } + + c, err := tesla.NewClient(ctx, tesla.WithTokenSource(oauth2.StaticTokenSource(tok))) if err != nil { return err } diff --git a/examples/show_energy_site_status/main.go b/examples/show_energy_site_status/main.go index b1dda25..304a058 100644 --- a/examples/show_energy_site_status/main.go +++ b/examples/show_energy_site_status/main.go @@ -2,11 +2,14 @@ package main import ( "context" + "encoding/json" "flag" "fmt" "os" - "github.com/bogosj/tesla" + "golang.org/x/oauth2" + + tesla "github.com/evcc-io/tesla-proxy-client" ) var tokenPath = flag.String("token", "", "path to token file") @@ -28,7 +31,16 @@ func main() { } func run(ctx context.Context, tokenPath string, reservePercentage int64) error { - c, err := tesla.NewClient(ctx, tesla.WithTokenFile(tokenPath)) + b, err := os.ReadFile(tokenPath) + if err != nil { + return err + } + var tok *oauth2.Token + if err := json.Unmarshal(b, &tok); err != nil { + return err + } + + c, err := tesla.NewClient(ctx, tesla.WithTokenSource(oauth2.StaticTokenSource(tok))) if err != nil { return err } diff --git a/examples/show_vehicle_info/main.go b/examples/show_vehicle_info/main.go index 7122d00..d354a36 100644 --- a/examples/show_vehicle_info/main.go +++ b/examples/show_vehicle_info/main.go @@ -2,11 +2,14 @@ package main import ( "context" + "encoding/json" "flag" "fmt" "os" - "github.com/bogosj/tesla" + "golang.org/x/oauth2" + + tesla "github.com/evcc-io/tesla-proxy-client" ) var tokenPath = flag.String("token", "", "path to token file") @@ -26,7 +29,16 @@ func main() { } func run(ctx context.Context, tokenPath string) error { - c, err := tesla.NewClient(ctx, tesla.WithTokenFile(tokenPath)) + b, err := os.ReadFile(tokenPath) + if err != nil { + return err + } + var tok *oauth2.Token + if err := json.Unmarshal(b, &tok); err != nil { + return err + } + + c, err := tesla.NewClient(ctx, tesla.WithTokenSource(oauth2.StaticTokenSource(tok))) if err != nil { return err } diff --git a/go.mod b/go.mod index af3e8d5..28e4f80 100644 --- a/go.mod +++ b/go.mod @@ -3,19 +3,12 @@ module github.com/evcc-io/tesla-proxy-client go 1.18 require ( - github.com/PuerkitoBio/goquery v1.9.2 - github.com/manifoldco/promptui v0.9.0 - github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 github.com/smartystreets/goconvey v1.8.1 golang.org/x/oauth2 v0.23.0 ) require ( - github.com/andybalholm/cascadia v1.3.2 // indirect - github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect github.com/gopherjs/gopherjs v1.17.2 // indirect github.com/jtolds/gls v4.20.0+incompatible // indirect github.com/smarty/assertions v1.15.0 // indirect - golang.org/x/net v0.24.0 // indirect - golang.org/x/sys v0.19.0 // indirect ) diff --git a/go.sum b/go.sum index 4bc02d2..f687ca3 100644 --- a/go.sum +++ b/go.sum @@ -1,64 +1,11 @@ -github.com/PuerkitoBio/goquery v1.9.2 h1:4/wZksC3KgkQw7SQgkKotmKljk0M6V8TUvA8Wb4yPeE= -github.com/PuerkitoBio/goquery v1.9.2/go.mod h1:GHPCaP0ODyyxqcNoFGYlAprUFH81NuRPd0GX3Zu2Mvk= -github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss= -github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= -github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g= github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA= -github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= -github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= -github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY= github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec= github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY= github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= -golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= 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-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= -golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=