From 5f0702e7243971c4dd56b63d7cc114f051f7b762 Mon Sep 17 00:00:00 2001 From: Dylan Moreland Date: Sat, 7 Jun 2025 12:48:41 -0400 Subject: [PATCH] Library function for Tesla option codes --- internal/services/tesla_fleet_api_service.go | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/services/tesla_fleet_api_service.go b/internal/services/tesla_fleet_api_service.go index 939b67b9..1c5e751f 100644 --- a/internal/services/tesla_fleet_api_service.go +++ b/internal/services/tesla_fleet_api_service.go @@ -471,6 +471,39 @@ func (t *teslaFleetAPIService) GetTelemetrySubscriptionStatus(ctx context.Contex }, nil } +type TeslaOptionsResponse struct { + Codes []TeslaOptionCode `json:"codes"` +} + +type TeslaOptionCode struct { + Code string `json:"code"` + DisplayName string `json:"displayName"` + IsActive bool `json:"isActive"` +} + +// GetOptions returns a list of option codes for the vehicle with the given VIN. +// https://developer.tesla.com/docs/fleet-api/endpoints/vehicle-endpoints#options +func (t *teslaFleetAPIService) GetOptions(ctx context.Context, token, vin string) ([]TeslaOptionCode, error) { + u := t.FleetBase.JoinPath("api/1/dx/vehicles/options") + + q := u.Query() + q.Set("vin", vin) + u.RawQuery = q.Encode() + + body, err := t.performRequest(ctx, u, token, http.MethodGet, nil) + if err != nil { + return nil, err + } + + var optionsResp TeslaResponseWrapper[TeslaOptionsResponse] + err = json.Unmarshal(body, &optionsResp) + if err != nil { + return nil, err + } + + return optionsResp.Response.Codes, nil +} + var ErrUnauthorized = errors.New("unauthorized") // performRequest a helper function for making http requests, it adds a timeout context and parses error response