-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvehicle.go
64 lines (52 loc) · 1.7 KB
/
vehicle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package tesla
import (
"fmt"
"net/http"
)
// Vehicle represents the basic data about the vehicle.
type Vehicle struct {
ID int `json:"id"`
VehicleID int `json:"vehicle_id"`
VIN string `json:"vin"`
DisplayName string `json:"display_name"`
OptionCodes string `json:"option_codes"`
Color *string `json:"color"`
Tokens []string `json:"tokens"`
State string `json:"state"`
InService bool `json:"in_service"`
CalendarEnabled bool `json:"calendar_enabled"`
APIVersion int `json:"api_version"`
BackseatToken *string `json:"backseat_token"`
BackseatTokenUpdatedAt *int `json:"backseat_token_updated_at"`
}
// GetVehicles retrieves a list of vehicles for the currently authenticated account.
func (c *Conn) GetVehicles() ([]Vehicle, error) {
if c.accessToken == "" {
return nil, fmt.Errorf("%w", ErrMissingAccessToken)
}
type response struct {
Response []Vehicle `json:"response"`
Count int `json:"count"`
}
var respBody response
err := c.doRequest(http.MethodGet, "/api/1/vehicles", nil, &respBody)
if err != nil {
return nil, err
}
return respBody.Response, nil
}
// GetVehicle returns a vehicle by id.
func (c *Conn) GetVehicle(id int) (*Vehicle, error) {
if c.accessToken == "" {
return nil, fmt.Errorf("%w", ErrMissingAccessToken)
}
type response struct {
Response Vehicle `json:"response"`
}
var respBody response
err := c.doRequest(http.MethodGet, fmt.Sprintf("/api/1/vehicles/%d", id), nil, &respBody)
if err != nil {
return nil, err
}
return &respBody.Response, nil
}