forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
160 lines (123 loc) Β· 3.5 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
package fiat
import (
"encoding/base64"
"fmt"
"io"
"net/http"
"strings"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/samber/lo"
)
const (
ApiURI = "https://channels.sdpr-01.fcagcv.com"
ApiKey = "3_mOx_J2dRgjXYCdyhchv3b5lhi54eBcdCTX4BI8MORqmZCoQWhA0mV2PTlptLGUQI"
XApiKey = "qLYupk65UU1tw2Ih1cJhs4izijgRDbir2UFHA3Je"
AuthURI = "https://mfa.fcl-01.fcagcv.com"
XAuthApiKey = "JWRYW7IYhW9v0RqDghQSx4UcRYRILNmc8zAuh5ys"
)
// API is an api.Vehicle implementation for Fiat cars
type API struct {
identity *Identity
*request.Helper
}
func NewAPI(log *util.Logger, identity *Identity) *API {
api := &API{
identity: identity,
Helper: request.NewHelper(log),
}
return api
}
func (v *API) request(method, uri string, body io.ReadSeeker) (*http.Request, error) {
headers := map[string]string{
"Content-Type": "application/json",
"Accept": "application/json",
"X-Clientapp-Version": "1.0",
"ClientrequestId": lo.RandomString(16, lo.LettersCharset),
"X-Api-Key": XApiKey,
"X-Originator-Type": "web",
"locale": "de_de", // only required for pinAuth
}
req, err := request.New(method, uri, body, headers)
if err == nil {
// hack for pinAuth method
if strings.HasPrefix(uri, AuthURI) {
req.Header.Set("X-Api-Key", XAuthApiKey)
}
err = v.identity.Sign(req, body)
}
return req, err
}
func (v *API) Vehicles() ([]string, error) {
var res VehiclesResponse
uri := fmt.Sprintf("%s/v4/accounts/%s/vehicles?stage=ALL", ApiURI, v.identity.UID())
req, err := v.request(http.MethodGet, uri, nil)
if err == nil {
err = v.DoJSON(req, &res)
}
var vehicles []string
if err == nil {
for _, v := range res.Vehicles {
vehicles = append(vehicles, v.VIN)
}
}
return vehicles, err
}
func (v *API) Status(vin string) (StatusResponse, error) {
var res StatusResponse
uri := fmt.Sprintf("%s/v2/accounts/%s/vehicles/%s/status", ApiURI, v.identity.UID(), vin)
req, err := v.request(http.MethodGet, uri, nil)
if err == nil {
err = v.DoJSON(req, &res)
}
return res, err
}
func (v *API) Location(vin string) (LocationResponse, error) {
var res LocationResponse
uri := fmt.Sprintf("%s/v1/accounts/%s/vehicles/%s/location/lastknown", ApiURI, v.identity.UID(), vin)
req, err := v.request(http.MethodGet, uri, nil)
if err == nil {
err = v.DoJSON(req, &res)
}
return res, err
}
func (v *API) pinAuth(pin string) (string, error) {
var res PinAuthResponse
uri := fmt.Sprintf("%s/v1/accounts/%s/ignite/pin/authenticate", AuthURI, v.identity.UID())
data := struct {
PIN string `json:"pin"`
}{
PIN: base64.StdEncoding.EncodeToString([]byte(pin)),
}
req, err := v.request(http.MethodPost, uri, request.MarshalJSON(data))
if err == nil {
err = v.DoJSON(req, &res)
}
if err == nil && res.Message != "" {
err = fmt.Errorf("pin auth: %s", res.Message)
}
return res.Token, err
}
func (v *API) Action(vin, pin, action, cmd string) (ActionResponse, error) {
var res ActionResponse
token, err := v.pinAuth(pin)
if err != nil {
return res, err
}
uri := fmt.Sprintf("%s/v1/accounts/%s/vehicles/%s/%s", ApiURI, v.identity.UID(), vin, action)
data := struct {
Command string `json:"command"`
PinAuth string `json:"pinAuth"`
}{
Command: cmd,
PinAuth: token,
}
req, err := v.request(http.MethodPost, uri, request.MarshalJSON(data))
if err == nil {
err = v.DoJSON(req, &res)
}
if err == nil && res.Message != "" {
err = fmt.Errorf("action %s: %s", action, res.Message)
}
return res, err
}