From f4fa996d536f3a91244380451f00971bdb745c52 Mon Sep 17 00:00:00 2001 From: Alex Savin Date: Sun, 1 Jun 2025 20:32:17 -0400 Subject: [PATCH] More changes --- client.go | 128 ++++++++++++++++++++++++---------------------------- mysubaru.go | 12 ++--- 2 files changed, 65 insertions(+), 75 deletions(-) diff --git a/client.go b/client.go index f3502ef..7f92c58 100644 --- a/client.go +++ b/client.go @@ -236,7 +236,6 @@ func (c *Client) GetVehicleByVIN(vin string) *Vehicle { // Exec method executes a Client instance with the API URL func (c *Client) execute(requestUrl string, method string, params map[string]string, pollingUrl string, j bool) []byte { defer timeTrack("[TIMETRK] Executing Get Request") - var resp *resty.Response // GET Requests @@ -249,14 +248,12 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str // POST Requests if method == "POST" { - if j { - // POST > JSON Body + if j { // POST > JSON Body resp, _ = c.httpClient. R(). SetBody(params). Post(requestUrl) - } else { - // POST > Form Data + } else { // POST > Form Data resp, _ = c.httpClient. R(). SetFormData(params). @@ -271,84 +268,77 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str if r, ok := c.parseResponse(resBytes); ok { c.logger.Debug("parsed http request output", "data", r.Data) + // dataName field has the list of the states [ remoteServiceStatus | errorResponse ] if r.DataName == "remoteServiceStatus" { var sr ServiceRequest err := json.Unmarshal(r.Data, &sr) if err != nil { c.logger.Error("error while parsing json", "request", "HTTP POLLING", "error", err.Error()) } + // {"success":false,"errorCode":"404-soa-unableToParseResponseBody","dataName":"errorResponse","data":{"errorLabel":"404-soa-unableToParseResponseBody","errorDescription":null}} if pollingUrl != "" { - time.Sleep(3 * time.Second) - attempts := 20 - - poolingLoop: - for attempts > 0 { - resp, _ = c.httpClient. - SetBaseURL(MOBILE_API_SERVER[c.country]). - R(). - SetQueryParams(map[string]string{ - "serviceRequestId": sr.ServiceRequestID, - }). - Get(pollingUrl) - resBytes, _ := io.ReadAll(resp.Body) - c.logger.Debug("POLLING HTTP OUTPUT", "body", string(resBytes)) - // {"success":false,"errorCode":"404-soa-unableToParseResponseBody","dataName":"errorResponse","data":{"errorLabel":"404-soa-unableToParseResponseBody","errorDescription":null}} - - var r Response - err := json.Unmarshal(resBytes, &r) - if err != nil { - c.logger.Error("error while parsing json", "request", "HTTP POLLING", "error", err.Error()) - } - c.logger.Debug("parsed loop http request output", "request", "HTTP POLLING", "data", r.Data) - - if r.Success { - var sr ServiceRequest - err := json.Unmarshal(r.Data, &sr) - if err != nil { - c.logger.Error("error while parsing json", "request", "HTTP POLLING", "error", err.Error()) - } - switch { - case sr.RemoteServiceState == "finished": - c.logger.Debug("Remote service request completed successfully", "request id", sr.ServiceRequestID) - break poolingLoop - case sr.RemoteServiceState == "started": - c.logger.Debug("Subaru API reports remote service request is in progress", "request id", sr.ServiceRequestID) - } - } else { - c.logger.Debug("Backend session expired, please try again") - break poolingLoop - } - attempts-- + switch { + case sr.RemoteServiceState == "finished": + // Finished RemoteServiceState Service Request does not include Service Request ID + c.logger.Debug("Remote service request completed successfully") + case sr.RemoteServiceState == "started": time.Sleep(3 * time.Second) + c.logger.Debug("Subaru API reports remote service request (started) is in progress", "id", sr.ServiceRequestID) + c.execute(pollingUrl, GET, map[string]string{"serviceRequestId": sr.ServiceRequestID}, pollingUrl, false) + case sr.RemoteServiceState == "stopping": + time.Sleep(3 * time.Second) + c.logger.Debug("Subaru API reports remote service request (stopping) is in progress", "id", sr.ServiceRequestID) + c.execute(pollingUrl, GET, map[string]string{"serviceRequestId": sr.ServiceRequestID}, pollingUrl, false) + default: + time.Sleep(3 * time.Second) + c.logger.Debug("Subaru API reports remote service request (stopping) is in progress") + c.execute(pollingUrl, GET, map[string]string{"serviceRequestId": sr.ServiceRequestID}, pollingUrl, false) } } - // for { - // if attempt >= attempts { - // break - // } - // resp := c.execute(pollingUrl, GET, map[string]string{"serviceRequestId": sr.ServiceRequestID}, pollingUrl, false) - // var r Response - // err = json.Unmarshal(resp, &r) - // if err != nil { - // c.logger.Error("error while parsing json", "request", "execute", "method", method, "url", requestUrl, "error", err.Error()) - // } - // c.logger.Debug("parsed http request output", "request", "HTTP POLLING", "data", r.Data) - // var sr ServiceRequest - // err := json.Unmarshal(r.Data, &sr) - // if err != nil { - // c.logger.Error("error while parsing json", "request", "HTTP POLLING", "error", err.Error()) - // } + // if pollingUrl != "" { + // time.Sleep(3 * time.Second) + // attempts := 20 - // switch { - // case sr.RemoteServiceState == "finished": - // c.logger.Debug("Remote service request completed successfully", "request id", sr.ServiceRequestID) - // break - // case sr.RemoteServiceState == "started": - // c.logger.Debug("Subaru API reports remote service request is in progress", "request id", sr.ServiceRequestID) + // poolingLoop: + // for attempts > 0 { + // resp, _ = c.httpClient. + // R(). + // SetQueryParams(map[string]string{ + // "serviceRequestId": sr.ServiceRequestID, + // }). + // Get(pollingUrl) + // resBytes, _ := io.ReadAll(resp.Body) + // c.logger.Debug("POLLING HTTP OUTPUT", "body", string(resBytes)) + + // var r Response + // err := json.Unmarshal(resBytes, &r) + // if err != nil { + // c.logger.Error("error while parsing json", "request", "HTTP POLLING", "error", err.Error()) + // } + // c.logger.Debug("parsed loop http request output", "request", "HTTP POLLING", "data", r.Data) + + // if r.Success { + // var sr ServiceRequest + // err := json.Unmarshal(r.Data, &sr) + // if err != nil { + // c.logger.Error("error while parsing json", "request", "HTTP POLLING", "error", err.Error()) + // } + // switch { + // case sr.RemoteServiceState == "finished": + // c.logger.Debug("Remote service request completed successfully", "request id", sr.ServiceRequestID) + // break poolingLoop + // case sr.RemoteServiceState == "started": + // c.logger.Debug("Subaru API reports remote service request is in progress", "request id", sr.ServiceRequestID) + // } + // } else { + // c.logger.Debug("Backend session expired, please try again") + // break poolingLoop + // } + // attempts-- + // time.Sleep(3 * time.Second) // } - // attempt++ // } } } else { diff --git a/mysubaru.go b/mysubaru.go index 6031963..45f7fc7 100644 --- a/mysubaru.go +++ b/mysubaru.go @@ -9,7 +9,7 @@ import ( type Response struct { Success bool `json:"success"` // true | false ErrorCode string `json:"errorCode,omitempty"` // string | Error message if Success is false - DataName string `json:"dataName"` // string | Describes the structure which is incleded in Data field + DataName string `json:"dataName,omitempty"` // string | Describes the structure which is included in Data field Data json.RawMessage `json:"data"` // Data struct } @@ -170,7 +170,7 @@ type VehicleStatus struct { DistanceToEmptyFuelKilometers10s int `json:"distanceToEmptyFuelKilometers10s"` // + 340 AvgFuelConsumptionMpg float64 `json:"avgFuelConsumptionMpg"` // + 18.4 AvgFuelConsumptionLitersPer100Kilometers float64 `json:"avgFuelConsumptionLitersPer100Kilometers"` // + 12.8 - RemainingFuelPercent int `json:"remainingFuelPercent"` // + 82 + RemainingFuelPercent int `json:"remainingFuelPercent,string"` // + "82" TirePressureFrontLeft int `json:"tirePressureFrontLeft,string"` // + "2275" TirePressureFrontRight int `json:"tirePressureFrontRight,string"` // + "2344" TirePressureRearLeft int `json:"tirePressureRearLeft,string"` // + "2413" @@ -267,9 +267,9 @@ type VehicleCondition struct { // "dataName": "remoteServiceStatus" type ServiceRequest struct { ServiceRequestID string `json:"serviceRequestId,omitempty"` // 4S4BTGND8L3137058_1640294426029_19_@NGTP - Success bool `json:"success"` // false | true + Success bool `json:"success"` // false | true // Could be in the false state while the executed request in the progress Cancelled bool `json:"cancelled"` // false | true - RemoteServiceType string `json:"remoteServiceType"` // unlock | lock | locate | vehicleStatus | lightsOnly | engineStart | engineStop | phevChargeNow | condition + RemoteServiceType string `json:"remoteServiceType"` // vehicleStatus | condition | locate | unlock | lock | lightsOnly | engineStart | engineStop | phevChargeNow RemoteServiceState string `json:"remoteServiceState"` // started | finished | stopping SubState string `json:"subState,omitempty"` // null ErrorCode string `json:"errorCode,omitempty"` // null:null @@ -281,8 +281,8 @@ type ServiceRequest struct { // ErrorResponse . // "dataName":"errorResponse" type ErrorResponse struct { - ErrorLabel string `json:"errorLabel"` // "404-soa-unableToParseResponseBody" - ErrorDescription *string `json:"errorDescription,omitempty"` // null + ErrorLabel string `json:"errorLabel"` // "404-soa-unableToParseResponseBody" + ErrorDescription string `json:"errorDescription,omitempty"` // null } // climateSettings: [ climateSettings ]