Skip to content

Commit

Permalink
Implement token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed May 14, 2020
1 parent 7b8a579 commit 77de965
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
8 changes: 8 additions & 0 deletions util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type HTTPHelper struct {
Log *Logger
Client *http.Client
last *http.Response // last response
}

// NewHTTPHelper creates http helper for simplified PUT GET logic
Expand All @@ -23,8 +24,14 @@ func NewHTTPHelper(log *Logger) *HTTPHelper {
return r
}

// LastResponse returns last http.Response that was read without error
func (r *HTTPHelper) LastResponse() *http.Response {
return r.last
}

// Response codes other than HTTP 200 or 204 are raised as error
func (r *HTTPHelper) readBody(resp *http.Response, err error) ([]byte, error) {
r.last = nil
if err != nil {
return []byte{}, err
}
Expand All @@ -39,6 +46,7 @@ func (r *HTTPHelper) readBody(resp *http.Response, err error) ([]byte, error) {
r.Log.TRACE.Printf("%s\n%s", resp.Request.URL.String(), string(b))
}

r.last = resp
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return b, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, string(b))
}
Expand Down
45 changes: 33 additions & 12 deletions vehicle/renault.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,6 @@ func (v *Renault) authFlow() error {
return errors.New("missing vin")
}
}

if err == nil {
v.kamereonAccessToken, err = v.kamereonToken(v.accountID)
if v.kamereonAccessToken == "" {
return errors.New("missing kamereon access token")
}
}
}
}
}
Expand Down Expand Up @@ -323,18 +316,46 @@ func (v *Renault) kamereonToken(accountID string) (string, error) {
return kr.AccessToken, err
}

// chargeState implements the Vehicle.ChargeState interface
func (v *Renault) chargeState() (float64, error) {
var kr kamereonResponse
uri := fmt.Sprintf("%s/commerce/v1/accounts/%s/kamereon/kca/car-adapter/v1/cars/%s/battery-status", v.kamereon.Target, v.accountID, v.vin)
func (v *Renault) kamereonRequest(uri string) (*http.Request, error) {
if v.kamereonAccessToken == "" {
token, err := v.kamereonToken(v.accountID)
if err != nil || token == "" {
return nil, fmt.Errorf("refreshing kamereon access token failed: %v", err)
}
v.kamereonAccessToken = token
}

data := url.Values{"country": []string{"DE"}}
headers := v.kamereonHeaders(map[string]string{"x-kamereon-authorization": "Bearer " + v.kamereonAccessToken})

req, err := v.request(uri, data, headers)
return v.request(uri, data, headers)
}

// doJSONRequest executes request and handles token expiry
func (v *Renault) doKamereonRequest(uri string, kr interface{}) error {
req, err := v.kamereonRequest(uri)
if err == nil {
_, err = v.RequestJSON(req, &kr)
if err != nil {
if resp := v.LastResponse(); resp != nil && resp.StatusCode == http.StatusUnauthorized {
v.kamereonAccessToken = ""
}
}
}
return err
}

// chargeState implements the Vehicle.ChargeState interface
func (v *Renault) chargeState() (float64, error) {
var kr kamereonResponse
uri := fmt.Sprintf("%s/commerce/v1/accounts/%s/kamereon/kca/car-adapter/v1/cars/%s/battery-status", v.kamereon.Target, v.accountID, v.vin)

// do request with retry
err := v.doKamereonRequest(uri, &kr)
if err != nil && v.kamereonAccessToken == "" {
err = v.doKamereonRequest(uri, &kr)
}

return float64(kr.Data.Attributes.BatteryLevel), err
}

Expand Down

0 comments on commit 77de965

Please sign in to comment.