diff --git a/client.go b/client.go index edc17d4..979b496 100644 --- a/client.go +++ b/client.go @@ -7,6 +7,7 @@ import ( "log/slog" "slices" "sync" + "time" "git.savin.nyc/alex/mysubaru/config" "resty.dev/v3" @@ -196,8 +197,81 @@ func (c *Client) GetVehicleByVin(vin string) (*Vehicle, error) { return nil, errors.New("vin code is not in the list of the available vin codes") } +// RefreshVehicles refreshes the list of vehicles associated with the client's account. +func (c *Client) RefreshVehicles() error { + params := map[string]string{} + reqURL := MOBILE_API_VERSION + apiURLs["API_REFRESH_VEHICLES"] + resp, err := c.execute(GET, reqURL, params, false) + if err != nil { + c.logger.Error("error while executing RefreshVehicles request", "request", "RefreshVehicles", "error", err.Error()) + return errors.New("error while executing RefreshVehicles request: " + err.Error()) + } + c.logger.Debug("http request output", "request", "RefreshVehicles", "body", resp) + + // var vd VehicleData + // err = json.Unmarshal(resp.Data, &vd) + // if err != nil { + // c.logger.Error("error while parsing json", "request", "SelectVehicle", "error", err.Error()) + // return errors.New("error while parsing json while vehicle selection") + // } + // c.logger.Debug("http request output", "request", "SelectVehicle", "body", resp) + return nil +} + +// RequestAuthCode requests an authentication code for two-factor authentication (2FA). +func (c *Client) RequestAuthCode() error { + params := map[string]string{ + "contactMethod": "0", + "languagePreference": "EN"} + reqUrl := MOBILE_API_VERSION + apiURLs["API_2FA_SEND_VERIFICATION"] + resp, err := c.execute(POST, reqUrl, params, false) + if err != nil { + c.logger.Error("error while executing RequestAuthCode request", "request", "RequestAuthCode", "error", err.Error()) + return errors.New("error while executing RequestAuthCode request: " + err.Error()) + } + c.logger.Debug("http request output", "request", "RequestAuthCode", "body", resp) + + return nil +} + +// SubmitAuthCode submits the authentication code received from the RequestAuthCode method. +func (c *Client) SubmitAuthCode(code string, permanent bool) error { + params := map[string]string{ + "deviceId": c.credentials.DeviceID, + "deviceName": c.credentials.DeviceName, + "verificationCode": code} + if permanent { + params["rememberDevice"] = "on" + } + + reqUrl := MOBILE_API_VERSION + apiURLs["API_2FA_AUTH_VERIFY"] + resp, err := c.execute(POST, reqUrl, params, false) + if err != nil { + c.logger.Error("error while executing SubmitAuthCode request", "request", "SubmitAuthCode", "error", err.Error()) + return errors.New("error while executing SubmitAuthCode request: " + err.Error()) + } + // Device registration does not always immediately take effect + time.Sleep(time.Second * 3) + c.logger.Debug("http request output", "request", "SubmitAuthCode", "body", resp) + + return nil +} + +// getContactMethods retrieves the available contact methods for two-factor authentication (2FA). +func (c *Client) GetContactMethods() error { + params := map[string]string{} + reqUrl := MOBILE_API_VERSION + apiURLs["API_2FA_CONTACT"] + resp, err := c.execute(POST, reqUrl, params, false) + if err != nil { + c.logger.Error("error while executing GetContactMethods request", "request", "GetContactMethods", "error", err.Error()) + return errors.New("error while executing GetContactMethods request: " + err.Error()) + } + c.logger.Debug("http request output", "request", "GetContactMethods", "body", resp) + + return nil +} + // func isPINRequired() {} -// func getVehicles() {} // func getEVStatus() {} // func getRemoteOptionsStatus() {} // func getRemoteStartStatus() {}