-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
163 lines (130 loc) · 3.99 KB
/
api.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package jlr
import (
"errors"
"fmt"
"net/http"
"net/url"
"github.com/thommyho/robotui/util"
"github.com/thommyho/robotui/util/request"
"github.com/thommyho/robotui/util/transport"
"golang.org/x/oauth2"
)
const (
IF9_BASE_URL = "https://if9.prod-row.jlrmotor.com/if9/jlr"
IFOP_BASE_URL = "https://ifop.prod-row.jlrmotor.com/ifop/jlr"
)
// API is the Jaguar/Landrover api client
type API struct {
*request.Helper
}
// NewAPI creates a new api client
func NewAPI(log *util.Logger, device string, ts oauth2.TokenSource) *API {
v := &API{
Helper: request.NewHelper(log),
}
v.Client.Transport = &transport.Decorator{
Decorator: func(req *http.Request) error {
token, err := ts.Token()
if err == nil {
for k, v := range map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", token.AccessToken),
"X-Device-Id": device,
"x-telematicsprogramtype": "jlrpy",
} {
req.Header.Set(k, v)
}
}
return err
},
Base: v.Client.Transport,
}
return v
}
func (v *API) User(name string) (User, error) {
var res User
uri := fmt.Sprintf("%s/users?loginName=%s", IF9_BASE_URL, url.QueryEscape(name))
req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"Content-Type": request.JSONContent,
"Accept": "application/vnd.wirelesscar.ngtp.if9.User-v3+json",
})
if err == nil {
err = v.DoJSON(req, &res)
}
return res, err
}
func (v *API) Vehicles(user string) ([]string, error) {
var vehicles []string
var resp VehiclesResponse
uri := fmt.Sprintf("%s/users/%s/vehicles?primaryOnly=true", IF9_BASE_URL, user)
err := v.GetJSON(uri, &resp)
if err == nil {
for _, v := range resp.Vehicles {
vehicles = append(vehicles, v.VIN)
}
}
return vehicles, nil
}
// Status returns the vehicle status
func (v *API) Status(vin string) (StatusResponse, error) {
var status StatusResponse
uri := fmt.Sprintf("%s/vehicles/%s/status?includeInactive=true", IF9_BASE_URL, vin)
req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"Content-Type": "application/json",
"Accept": "application/vnd.ngtp.org.if9.healthstatus-v3+json",
})
if err == nil {
err = v.DoJSON(req, &status)
}
return status, err
}
// Position returns the vehicle position
func (v *API) Position(vin string) (PositionResponse, error) {
var status PositionResponse
uri := fmt.Sprintf("%s/vehicles/%s/position", IF9_BASE_URL, vin)
req, err := request.New(http.MethodGet, uri, nil, map[string]string{
"Content-Type": "application/json",
})
if err == nil {
err = v.DoJSON(req, &status)
}
return status, err
}
func (v *API) AuthenticateVinService(vin, user, service string) (PinResponse, error) {
pin := vin[len(vin)-4:]
data := map[string]string{
"serviceName": service,
"pin": pin,
}
uri := fmt.Sprintf("%s/vehicles/%s/users/%s/authenticate", IF9_BASE_URL, vin, user)
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), map[string]string{
"Content-Type": "application/vnd.wirelesscar.ngtp.if9.AuthenticateRequest-v2+json",
})
var res PinResponse
if err == nil {
err = v.DoJSON(req, &res)
}
return res, err
}
func (v *API) ChargeAction(vin, user string, start bool) error {
var data map[string]interface{}
pin, err := v.AuthenticateVinService(vin, user, "CP")
if err == nil {
onoff := map[bool]string{false: "FORCE_OFF", true: "FORCE_ON"}
data = map[string]interface{}{
"token": pin.Token,
"serviceParameters": []KeyValue{{"CHARGE_NOW_SETTING", onoff[start]}},
}
}
uri := fmt.Sprintf("%s/vehicles/%s/chargeProfile", IF9_BASE_URL, vin)
req, err := request.New(http.MethodPost, uri, request.MarshalJSON(data), map[string]string{
"Content-Type": "application/vnd.wirelesscar.ngtp.if9.PhevService-v1+json",
"Accept": "application/vnd.wirelesscar.ngtp.if9.ServiceStatus-v5+json",
})
var res ActionResponse
if err == nil {
if err = v.DoJSON(req, &res); err == nil && res.FailureDescription != "" {
err = errors.New(res.FailureDescription)
}
}
return err
}