Changed passing way of the logger
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 22s

This commit is contained in:
2025-05-27 23:05:29 -04:00
parent ea31ffb651
commit 0fb4996334
2 changed files with 64 additions and 64 deletions

View File

@ -24,7 +24,7 @@ type Client struct {
listOfVins []string
isAuthenticated bool
isRegistered bool
log *slog.Logger
logger *slog.Logger
sync.RWMutex
}
@ -42,7 +42,7 @@ func (c *Client) auth() []byte {
reqURL := MOBILE_API_VERSION + apiURLs["API_LOGIN"]
resp := c.execute(reqURL, POST, params, "", false)
c.log.Debug("AUTH HTTP OUTPUT", "body", string([]byte(resp)))
c.logger.Debug("AUTH HTTP OUTPUT", "body", string([]byte(resp)))
return resp
}
@ -151,14 +151,14 @@ func (c *Client) listDevices() {
fmt.Println(" RequestAttempt:", ti.RequestAttempt)
fmt.Println(" RemoteAddr :", ti.RemoteAddr)
// c.log.Debug("LIST DEVICES OUTPUT", "body", string([]byte(resp.Body())))
// c.logger.Debug("LIST DEVICES OUTPUT", "body", string([]byte(resp.Body())))
// c.httpClient.SetBaseURL(WEB_API_SERVER[c.country]).SetCookies(c.cookies)
// reqURL := apiURLs["WEB_API_LIST_DEVICES"]
// resp := c.execute(reqURL, GET, map[string]string{}, "", false)
// if isResponseSuccessfull(resp) {
// log.Debugf("LIST DEVICES OUTPUT >> %v\n", string(resp))
// logger.Debugf("LIST DEVICES OUTPUT >> %v\n", string(resp))
// }
}
@ -194,20 +194,20 @@ func (c *Client) validateSession() bool {
// if await self._select_vehicle(vin):
// result = True
c.log.Debug("session validation", "body", string([]byte(resp)))
c.logger.Debug("session validation", "body", string([]byte(resp)))
return true
}
// New function creates a New MySubaru client
func New(logger *slog.Logger, config *config.MySubaru) (*Client, error) {
func New(config *config.Config) (*Client, error) {
client := &Client{
credentials: config.Credentials,
country: config.Region,
credentials: config.MySubaru.Credentials,
country: config.MySubaru.Region,
updateInterval: 7200,
fetchInterval: 360,
log: logger,
logger: config.Logger,
}
httpClient := resty.New()
@ -230,20 +230,20 @@ func New(logger *slog.Logger, config *config.MySubaru) (*Client, error) {
}
if client.isResponseSuccessfull(resp) {
logger.Debug("Client authentication successful", "isRegistered", respParsed.Path("data.deviceRegistered").Data().(bool))
client.logger.Debug("Client authentication successful", "isRegistered", respParsed.Path("data.deviceRegistered").Data().(bool))
client.isAuthenticated = true
client.isRegistered = respParsed.Path("data.deviceRegistered").Data().(bool)
} else {
error, _ := respParsed.Path("errorCode").Data().(string)
switch {
case error == apiErrors["ERROR_INVALID_ACCOUNT"]:
logger.Debug("Invalid account")
client.logger.Debug("Invalid account")
case error == apiErrors["ERROR_INVALID_CREDENTIALS"]:
logger.Debug("Client authentication failed")
client.logger.Debug("Client authentication failed")
case error == apiErrors["ERROR_PASSWORD_WARNING"]:
logger.Debug("Multiple Password Failures.")
client.logger.Debug("Multiple Password Failures.")
default:
logger.Debug("Uknown error")
client.logger.Debug("Uknown error")
}
}
@ -286,7 +286,7 @@ func (c *Client) SelectVehicle(vin string) VehicleData {
json.Unmarshal([]byte(vdString), &vData)
// resp := c.execute(reqURL, GET, params, "", false)
// log.Debugf("SELECT VEHICLE OUTPUT >> %v\n", string([]byte(resp)))
// logger.Debugf("SELECT VEHICLE OUTPUT >> %v\n", string([]byte(resp)))
// ERRORS
// {"success":false,"errorCode":"vehicleNotInAccount","dataName":null,"data":null}
@ -323,7 +323,7 @@ func (c *Client) GetVehicles() []*Vehicle {
respParsed, err := gabs.ParseJSON([]byte(resp))
if err != nil {
c.log.Error("error which parsing json", "request", "GetVehicles", "error", err.Error())
c.logger.Error("error which parsing json", "request", "GetVehicles", "error", err.Error())
}
vData := VehicleData{}
@ -375,7 +375,7 @@ func (c *Client) GetVehicleByVIN(vin string) *Vehicle {
respParsed, err := gabs.ParseJSON([]byte(resp))
if err != nil {
c.log.Error("error which parsing json", "request", "GetVehicleByVIN", "error", err.Error())
c.logger.Error("error which parsing json", "request", "GetVehicleByVIN", "error", err.Error())
}
vData := VehicleData{}
@ -454,9 +454,9 @@ func (c *Client) GetVehicleStatus() {
respParsed, err := gabs.ParseJSON(resp)
if err != nil {
c.log.Error("error which parsing json", "request", "GetVehicleStatus", "error", err.Error())
c.logger.Error("error which parsing json", "request", "GetVehicleStatus", "error", err.Error())
}
c.log.Debug("GET VEHICLE STATUS OUTPUT", "body", respParsed)
c.logger.Debug("GET VEHICLE STATUS OUTPUT", "body", respParsed)
success, ok := respParsed.Path("success").Data().(bool)
// value == string, ok == false
@ -490,9 +490,9 @@ func (c *Client) GetClimateSettings() {
respParsed, err := gabs.ParseJSON(resp)
if err != nil {
c.log.Error("error which parsing json", "request", "GetClimateSettings", "error", err.Error())
c.logger.Error("error which parsing json", "request", "GetClimateSettings", "error", err.Error())
}
c.log.Debug("CLIMATE SETTINGS OUTPUT", "response", respParsed)
c.logger.Debug("CLIMATE SETTINGS OUTPUT", "response", respParsed)
// ONLY FOR THAT REQUEST BECAUSE OF API SENDS BACK ESCAPING DATA IN DATA FIELD
data, ok := respParsed.Path("data").Data().(string)
@ -507,7 +507,7 @@ func (c *Client) GetClimateSettings() {
// TODO: Work with errorCode
panic(data)
}
c.log.Debug("CLIMATE SETTINGS OUTPUT", "body", data)
c.logger.Debug("CLIMATE SETTINGS OUTPUT", "body", data)
}
// func isPINRequired() {}
@ -559,9 +559,9 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str
// }
respParsed, err := gabs.ParseJSON(resBytes)
if err != nil {
c.log.Error("error which parsing json", "request", "execute", "method", method, "url", requestUrl, "error", err.Error())
c.logger.Error("error which parsing json", "request", "execute", "method", method, "url", requestUrl, "error", err.Error())
}
c.log.Debug("HTTP OUTPUT", "body", string(resBytes))
c.logger.Debug("HTTP OUTPUT", "body", string(resBytes))
_, ok := respParsed.Path("success").Data().(bool)
// value == string, ok == false
@ -587,7 +587,7 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str
}).
Get(pollingUrl)
resBytes, _ := io.ReadAll(resp.Body)
c.log.Debug("POLLING HTTP OUTPUT", "body", string(resBytes))
c.logger.Debug("POLLING HTTP OUTPUT", "body", string(resBytes))
// {"success":false,"errorCode":"404-soa-unableToParseResponseBody","dataName":"errorResponse","data":{"errorLabel":"404-soa-unableToParseResponseBody","errorDescription":null}}
respParsed, err := gabs.ParseJSON(resBytes)
@ -603,13 +603,13 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str
status, _ := respParsed.Path("data.remoteServiceState").Data().(string)
switch {
case status == "finished":
c.log.Debug("Remote service request completed successfully", "request id", serviceRequestId)
c.logger.Debug("Remote service request completed successfully", "request id", serviceRequestId)
break poolingLoop
case status == "started":
c.log.Debug("Subaru API reports remote service request is in progress", "request id", serviceRequestId)
c.logger.Debug("Subaru API reports remote service request is in progress", "request id", serviceRequestId)
}
} else {
c.log.Debug("Backend session expired, please try again")
c.logger.Debug("Backend session expired, please try again")
break poolingLoop
}
attempts--
@ -625,12 +625,12 @@ func (c *Client) execute(requestUrl string, method string, params map[string]str
func (c *Client) isResponseSuccessfull(resp []byte) bool {
respParsed, err := gabs.ParseJSON(resp)
if err != nil {
c.log.Debug("error while parsing json response", "error", err)
c.logger.Debug("error while parsing json response", "error", err)
}
success, ok := respParsed.Path("success").Data().(bool)
if !ok {
c.log.Debug("response is not successful", "error", resp)
c.logger.Debug("response is not successful", "error", resp)
}
// ERRORS FROM CLIENT CREATION AFTER AUTH