diff --git a/client.go b/client.go index e512bee..8ceeef1 100644 --- a/client.go +++ b/client.go @@ -268,24 +268,61 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str c.logger.Error("error while getting body", "error", err.Error()) } - var r Response - err = json.Unmarshal(resBytes, &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) + if r, ok := c.parseResponse(resBytes); ok { + c.logger.Debug("parsed 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()) - } + 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()) + } - if pollingUrl != "" { - time.Sleep(3 * time.Second) - attempts := 20 + 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-- + time.Sleep(3 * time.Second) + } + } // for { // if attempt >= attempts { // break @@ -313,47 +350,6 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str // } // attempt++ // } - - 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-- - time.Sleep(3 * time.Second) - } } } else { c.logger.Error("request is not successfull", "request", "execute", "method", method, "url", requestUrl, "error", err.Error()) diff --git a/mysubaru.go b/mysubaru.go index ddff729..d3ac28c 100644 --- a/mysubaru.go +++ b/mysubaru.go @@ -2,6 +2,7 @@ package mysubaru import ( "encoding/json" + "log/slog" "time" ) @@ -13,6 +14,19 @@ type Response struct { Data json.RawMessage `json:"data"` // Data struct } +// parse . +func (r *Response) parse(b []byte, logger *slog.Logger) bool { + err := json.Unmarshal(b, &r) + if err != nil { + logger.Error("error while parsing json", "error", err.Error()) + return false + } + return true +} + +// Unmarshal . +// func (r *Response) Unmarshal(b []byte) {} + // Account . type Account struct { MarketID int `json:"marketId"` diff --git a/vehicle.go b/vehicle.go index eba2435..9d729cd 100644 --- a/vehicle.go +++ b/vehicle.go @@ -775,6 +775,7 @@ func (v *Vehicle) GetVehicleCondition() { val.Field(i).Interface() == "-64" || val.Field(i).Interface() == "" || val.Field(i).Interface() == 0 || + val.Field(i).Interface() == float64(0) || val.Field(i).Interface() == nil { continue }