Enhance documentation for API client and vehicle structures; improve test function naming for clarity
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 24s

This commit is contained in:
2025-07-06 15:15:12 -04:00
parent d8cf2c3fd7
commit 3c927fd83b
7 changed files with 61 additions and 64 deletions

View File

@ -12,7 +12,7 @@ import (
"resty.dev/v3"
)
// Client .
// Client represents a MySubaru API client that interacts with the MySubaru API.
type Client struct {
credentials config.Credentials
httpClient *resty.Client
@ -28,7 +28,7 @@ type Client struct {
sync.RWMutex
}
// New function creates a New MySubaru client
// New function creates a New MySubaru API client
func New(config *config.Config) (*Client, error) {
client := &Client{
@ -89,7 +89,7 @@ func New(config *config.Config) (*Client, error) {
return client, nil
}
// SelectVehicle .
// SelectVehicle selects a vehicle by its VIN. If no VIN is provided, it uses the current VIN.
func (c *Client) SelectVehicle(vin string) (*VehicleData, error) {
if vin == "" {
vin = c.currentVin
@ -117,7 +117,7 @@ func (c *Client) SelectVehicle(vin string) (*VehicleData, error) {
return &vd, nil
}
// GetVehicles .
// GetVehicles retrieves a list of vehicles associated with the client's account.
func (c *Client) GetVehicles() ([]*Vehicle, error) {
var vehicles []*Vehicle
for _, vin := range c.listOfVins {
@ -131,7 +131,7 @@ func (c *Client) GetVehicles() ([]*Vehicle, error) {
return vehicles, nil
}
// GetVehicleByVIN .
// GetVehicleByVin retrieves a vehicle by its VIN from the client's list of vehicles.
func (c *Client) GetVehicleByVin(vin string) (*Vehicle, error) {
var vehicle *Vehicle
if slices.Contains(c.listOfVins, vin) {
@ -203,15 +203,15 @@ func (c *Client) GetVehicleByVin(vin string) (*Vehicle, error) {
// func getRemoteStartStatus() {}
// func getSafetyStatus() {}
// func getSubscriptionStatus() {}
// func getClimateData() {}
// func saveClimateSettings() {}
// IsAlive checks if the Client instance is alive
func (c *Client) IsAlive() bool {
return c.isAlive
}
// Exec method executes a Client instance with the API URL
// execute executes an HTTP request based on the method, URL, and parameters provided.
func (c *Client) execute(method string, url string, params map[string]string, j bool) (*Response, error) {
c.Lock()
// defer timeTrack("[TIMETRK] Executing HTTP Request")
var resp *resty.Response
var err error
@ -263,6 +263,7 @@ func (c *Client) execute(method string, url string, params map[string]string, j
if r, ok := c.parseResponse(resBytes); ok {
c.isAlive = true
c.Unlock()
return &r, nil
} else {
if r.DataName == "errorResponse" {
@ -275,16 +276,18 @@ func (c *Client) execute(method string, url string, params map[string]string, j
c.logger.Error("request got an error", "request", "execute", "method", method, "url", url, "label", er.ErrorLabel, "descrip[tion", er.ErrorDescription)
}
c.logger.Error("request got an unknown error", "request", "execute", "method", method, "url", url, "label", er.ErrorLabel, "descrip[tion", er.ErrorDescription)
c.Unlock()
return nil, errors.New("request is not successfull, HTTP code: " + resp.Status())
}
c.logger.Error("request is not successfull", "request", "execute", "method", method, "url", url, "error", err.Error())
}
}
c.isAlive = false
c.Unlock()
return nil, errors.New("request is not successfull, HTTP code: " + resp.Status())
}
// auth .
// auth authenticates the client with the MySubaru API using the provided credentials.
func (c *Client) auth() (*Response, error) {
params := map[string]string{
"env": "cloudprod",
@ -306,7 +309,7 @@ func (c *Client) auth() (*Response, error) {
return resp, nil
}
// parseResponse .
// parseResponse parses the JSON response from the MySubaru API into a Response struct.
func (c *Client) parseResponse(b []byte) (Response, bool) {
var r Response
err := json.Unmarshal(b, &r)
@ -317,7 +320,7 @@ func (c *Client) parseResponse(b []byte) (Response, bool) {
return r, true
}
// ValidateSession .
// ValidateSession checks if the current session is valid by making a request to the vehicle status API.
func (c *Client) ValidateSession() bool {
reqURL := MOBILE_API_VERSION + apiURLs["API_VEHICLE_STATUS"]
resp, err := c.execute(GET, reqURL, map[string]string{}, false)