Refactor code structure and remove redundant sections for improved readability and maintainability
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 26s
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 26s
This commit is contained in:
32
client.go
32
client.go
@ -83,7 +83,7 @@ func New(config *config.Config) (*Client, error) {
|
||||
}
|
||||
client.currentVin = client.listOfVins[0]
|
||||
} else {
|
||||
client.logger.Error("there no cars assigned to the account")
|
||||
client.logger.Error("there are no cars assigned to the account")
|
||||
return nil, err
|
||||
}
|
||||
return client, nil
|
||||
@ -94,19 +94,21 @@ func (c *Client) SelectVehicle(vin string) (*VehicleData, error) {
|
||||
if vin == "" {
|
||||
vin = c.currentVin
|
||||
}
|
||||
|
||||
vinCheck(vin)
|
||||
|
||||
params := map[string]string{
|
||||
"vin": vin,
|
||||
"_": timestamp()}
|
||||
reqURL := MOBILE_API_VERSION + apiURLs["API_SELECT_VEHICLE"]
|
||||
// TODO: Add error handling
|
||||
resp, _ := c.execute(GET, reqURL, params, false)
|
||||
resp, err := c.execute(GET, reqURL, params, false)
|
||||
if err != nil {
|
||||
c.logger.Error("error while executing SelectVehicle request", "request", "SelectVehicle", "error", err.Error())
|
||||
return nil, errors.New("error while executing SelectVehicle request: " + err.Error())
|
||||
}
|
||||
// c.logger.Debug("http request output", "request", "SelectVehicle", "body", resp)
|
||||
|
||||
var vd VehicleData
|
||||
err := json.Unmarshal(resp.Data, &vd)
|
||||
err = json.Unmarshal(resp.Data, &vd)
|
||||
if err != nil {
|
||||
c.logger.Error("error while parsing json", "request", "SelectVehicle", "error", err.Error())
|
||||
return nil, errors.New("error while parsing json while vehicle selection")
|
||||
@ -116,32 +118,36 @@ func (c *Client) SelectVehicle(vin string) (*VehicleData, error) {
|
||||
}
|
||||
|
||||
// GetVehicles .
|
||||
func (c *Client) GetVehicles() []*Vehicle {
|
||||
func (c *Client) GetVehicles() ([]*Vehicle, error) {
|
||||
var vehicles []*Vehicle
|
||||
for _, vin := range c.listOfVins {
|
||||
vehicle, err := c.GetVehicleByVIN(vin)
|
||||
vehicle, err := c.GetVehicleByVin(vin)
|
||||
if err != nil {
|
||||
c.logger.Error("cannot get vehile data", "request", "SelectVehicle", "error", err.Error())
|
||||
c.logger.Error("cannot get vehicle data", "request", "GetVehicles", "error", err.Error())
|
||||
return nil, errors.New("cannot get vehicle data: " + err.Error())
|
||||
}
|
||||
vehicles = append(vehicles, vehicle)
|
||||
}
|
||||
return vehicles
|
||||
return vehicles, nil
|
||||
}
|
||||
|
||||
// GetVehicleByVIN .
|
||||
func (c *Client) GetVehicleByVIN(vin string) (*Vehicle, error) {
|
||||
func (c *Client) GetVehicleByVin(vin string) (*Vehicle, error) {
|
||||
var vehicle *Vehicle
|
||||
if slices.Contains(c.listOfVins, vin) {
|
||||
params := map[string]string{
|
||||
"vin": vin,
|
||||
"_": timestamp()}
|
||||
reqURL := MOBILE_API_VERSION + apiURLs["API_SELECT_VEHICLE"]
|
||||
// TODO: Add error handling
|
||||
resp, _ := c.execute(GET, reqURL, params, false)
|
||||
resp, err := c.execute(GET, reqURL, params, false)
|
||||
if err != nil {
|
||||
c.logger.Error("error while executing GetVehicleByVIN request", "request", "GetVehicleByVIN", "error", err.Error())
|
||||
return nil, errors.New("error while executing GetVehicleByVIN request: " + err.Error())
|
||||
}
|
||||
// c.logger.Debug("http request output", "request", "GetVehicleByVIN", "body", resp)
|
||||
|
||||
var vd VehicleData
|
||||
err := json.Unmarshal(resp.Data, &vd)
|
||||
err = json.Unmarshal(resp.Data, &vd)
|
||||
if err != nil {
|
||||
c.logger.Error("error while parsing json", "request", "GetVehicleByVIN", "error", err.Error())
|
||||
}
|
||||
|
Reference in New Issue
Block a user