Skip to content

Commit

Permalink
Mini: upgrade api
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Nov 16, 2021
1 parent d1f78ba commit 6849ca3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
16 changes: 13 additions & 3 deletions vehicle/bmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ type BMW struct {

func init() {
registry.Add("bmw", NewBMWFromConfig)
registry.Add("mini", NewBMWFromConfig)
registry.Add("mini", NewMiniFromConfig)
}

// NewBMWFromConfig creates a new vehicle
func NewBMWFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return NewBMWMiniFromConfig("bmw", other)
}

// NewMiniFromConfig creates a new vehicle
func NewMiniFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return NewBMWMiniFromConfig("mini", other)
}

// NewBMWMiniFromConfig creates a new vehicle
func NewBMWMiniFromConfig(brand string, other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
embed `mapstructure:",squash"`
User, Password, VIN string
Expand All @@ -38,14 +48,14 @@ func NewBMWFromConfig(other map[string]interface{}) (api.Vehicle, error) {
embed: &cc.embed,
}

log := util.NewLogger("bmw").Redact(cc.User, cc.Password, cc.VIN)
log := util.NewLogger(brand).Redact(cc.User, cc.Password, cc.VIN)
identity := bmw.NewIdentity(log)

if err := identity.Login(cc.User, cc.Password); err != nil {
return nil, err
}

api := bmw.NewAPI(log, identity)
api := bmw.NewAPI(log, brand, identity)

var err error
if cc.VIN == "" {
Expand Down
9 changes: 5 additions & 4 deletions vehicle/bmw/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ import (
const (
ApiURI = "https://b2vapi.bmwgroup.com/webapi/v1"
CocoApiURI = "https://cocoapi.bmwgroup.com"
XUserAgent = "android(v1.07_20200330);bmw;1.7.0(11152)"
)

// API is an api.Vehicle implementation for BMW cars
type API struct {
*request.Helper
xUserAgent string
}

// NewAPI creates a new vehicle
func NewAPI(log *util.Logger, identity oauth2.TokenSource) *API {
func NewAPI(log *util.Logger, brand string, identity oauth2.TokenSource) *API {
v := &API{
Helper: request.NewHelper(log),
Helper: request.NewHelper(log),
xUserAgent: fmt.Sprintf("android(v1.07_20200330);%s;1.7.0(11152)", brand),
}

// replace client transport with authenticated transport
Expand Down Expand Up @@ -64,7 +65,7 @@ func (v *API) Status(vin string) (VehicleStatus, error) {
uri := fmt.Sprintf("%s/eadrax-vcs/v1/vehicles?apptimezone=60&appDateTime=%d&vin=%s", CocoApiURI, time.Now().Unix(), vin)

req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"X-User-Agent": XUserAgent,
"X-User-Agent": v.xUserAgent,
})
if err == nil {
err = v.DoJSON(req, &resp)
Expand Down

0 comments on commit 6849ca3

Please sign in to comment.