-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
230 lines (214 loc) · 8.48 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package mercedes
import (
"context"
"fmt"
"github.com/carlmjohnson/requests"
"github.com/google/uuid"
"net/url"
)
const (
CommandNameAuxHeatConfigure = "AUXHEAT_CONFIGURE"
CommandNameAuxHeatStart = "AUXHEAT_START"
CommandNameAuxHeatStop = "AUXHEAT_STOP"
CommandNameBatteryChargeProgramConfigure = "BATTERY_CHARGE_PROGRAM_CONFIGURE"
CommandNameBatteryMaxSocConfigure = "BATTERY_MAX_SOC_CONFIGURE"
CommandNameChargeOptConfigure = "CHARGE_OPT_CONFIGURE"
CommandNameChargeOptStart = "CHARGE_OPT_START"
CommandNameChargeOptStop = "CHARGE_OPT_STOP"
CommandNameChargeProgramConfigure = "CHARGE_PROGRAM_CONFIGURE"
CommandNameChildPresenceDetectionDeactivateAlarm = "CHILDPRESENCEDETECTION_DEACTIVATEALARM"
CommandNameRemoteUpdateStart = "REMOTE_UPDATE_START"
CommandNameDoorsLock = "DOORS_LOCK"
CommandNameDoorsUnlock = "DOORS_UNLOCK"
CommandNameEngineStart = "ENGINE_START"
CommandNameEngineStop = "ENGINE_STOP"
CommandNameTheftAlarmDeselectInterior = "THEFTALARM_DESELECT_INTERIOR"
CommandNameTheftAlarmDeselectTow = "THEFTALARM_DESELECT_TOW"
CommandNameTheftAlarmSelectInterior = "THEFTALARM_SELECT_INTERIOR"
CommandNameTheftAlarmSelectTow = "THEFTALARM_SELECT_TOW"
CommandNameTheftAlarmStart = "THEFTALARM_START"
CommandNameTheftAlarmStop = "THEFTALARM_STOP"
CommandNameTheftAlarmConfirmDamagedetection = "THEFTALARM_CONFIRM_DAMAGEDETECTION"
CommandNameTheftAlarmDeselectDamagedetection = "THEFTALARM_DESELECT_DAMAGEDETECTION"
CommandNameTheftAlarmSelectDamagedetection = "THEFTALARM_SELECT_DAMAGEDETECTION"
CommandNameTheftAlarmSelectPictureTaking = "THEFTALARM_SELECT_PICTURE_TAKING"
CommandNameTheftAlarmDeselectPictureTaking = "THEFTALARM_DESELECT_PICTURE_TAKING"
CommandNameSunroofOpen = "SUNROOF_OPEN"
CommandNameSunroofLift = "SUNROOF_LIFT"
CommandNameSunroofClose = "SUNROOF_CLOSE"
CommandNameSpeedAlertStart = "SPEEDALERT_START"
CommandNameSpeedAlertStop = "SPEEDALERT_STOP"
CommandNameWeekProfileConfigure = "WEEK_PROFILE_CONFIGURE"
CommandNameWeekProfileConfigureV2 = "WEEK_PROFILE_CONFIGURE_V2"
CommandNameChargingClockTimerConfigure = "CHARGING_CLOCK_TIMER_CONFIGURE"
CommandNameChargingConfigure = "CHARGING_CONFIGURE"
CommandNameWindowsOpen = "WINDOWS_OPEN"
CommandNameWindowsClose = "WINDOWS_CLOSE"
CommandNameWindowsVentilate = "WINDOWS_VENTILATE"
CommandNameWiperHealthReset = "WIPER_HEALTH_RESET"
CommandNameZevPreconditionConfigure = "ZEV_PRECONDITION_CONFIGURE"
CommandNameZevPreconditionConfigureSeats = "ZEV_PRECONDITION_CONFIGURE_SEATS"
CommandNameZevPreconditioningStart = "ZEV_PRECONDITIONING_START"
CommandNameZevPreconditioningStop = "ZEV_PRECONDITIONING_STOP"
CommandNameSigposStart = "SIGPOS_START"
CommandNameTemperatureConfigure = "TEMPERATURE_CONFIGURE"
)
type API struct {
region Region
}
func NewAPI(opts ...APIOption) *API {
api := &API{
region: RegionChina,
}
for _, opt := range opts {
opt.apply(api)
}
return api
}
func (a *API) Config(ctx context.Context) (*ConfigResponse, error) {
var res ConfigResponse
err := requests.
URL(RegionProviders[a.region].ConfigURL).
Header("X-TrackingId", uuid.New().String()).
Header("ris-os-name", "ios").
Header("ris-os-version", "17.5.1").
Header("ris-sdk-version", "2.113.0").
Header("X-Locale", RegionProviders[a.region].XLocal).
Header("ris-application-version", "1.41.2 (2167)").
Header("X-SessionId", uuid.New().String()).
Header("X-ApplicationName", "mycar-store-cn").
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func (a *API) Login(ctx context.Context, emailOrPhoneNumber, nonce string) (*LoginResponse, error) {
var res LoginResponse
err := requests.
URL(RegionProviders[a.region].LoginURL).
Post().
Header("X-TrackingId", uuid.New().String()).
Header("ris-os-name", "ios").
Header("ris-os-version", "17.5.1").
Header("ris-sdk-version", "2.113.0").
Header("X-Locale", RegionProviders[a.region].XLocal).
Header("X-SessionId", uuid.New().String()).
BodyJSON(map[string]any{
"emailOrPhoneNumber": emailOrPhoneNumber,
"countryCode": RegionProviders[a.region].CountryCode,
"nonce": nonce,
}).
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func (a *API) OAuth2(ctx context.Context, email, nonce, password string) (*OAuth2Response, error) {
var res OAuth2Response
err := requests.
URL(RegionProviders[a.region].OAuth2URL).
Post().
BodyForm(url.Values{
"client_id": {RegionProviders[a.region].ClientId},
"grant_type": {"password"},
"password": {fmt.Sprintf("%s:%s", nonce, password)},
"scope": {"openid email phone profile offline_access ciam-uid"},
"username": {email},
"Stage": {"prod"},
"X-Device-Id": {uuid.New().String()},
"X-Request-Id": {uuid.New().String()},
}).
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func (a *API) RefreshToken(ctx context.Context, refreshToken string) (*RefreshTokenResponse, error) {
var res RefreshTokenResponse
err := requests.
URL(RegionProviders[a.region].OAuth2URL).
Post().
ContentType("application/x-www-form-urlencoded").
BodyForm(url.Values{
"grant_type": {"refresh_token"},
"refresh_token": {refreshToken},
"X-Device-Id": {uuid.New().String()},
"X-Request-Id": {uuid.New().String()},
}).
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func (a *API) GetVehicles(ctx context.Context, token string) (*GetVehiclesResponse, error) {
var res GetVehiclesResponse
err := requests.
URL(RegionProviders[a.region].ResetURL).
Path("/v2/vehicles").
Header("Authorization", "Bearer "+token).
Header("X-TrackingId", uuid.New().String()).
Header("ris-os-name", "ios").
Header("ris-os-version", "17.5.1").
Header("ris-sdk-version", "2.113.0").
Header("X-Locale", RegionProviders[a.region].XLocal).
Header("X-SessionId", uuid.New().String()).
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func (a *API) GetUserDetail(ctx context.Context, token string) (*GetUserDetailResponse, error) {
var res GetUserDetailResponse
err := requests.
URL(RegionProviders[a.region].ResetURL).
Path("/v1/user").
Header("Authorization", "Bearer "+token).
Header("X-TrackingId", uuid.New().String()).
Header("ris-os-name", "ios").
Header("ris-os-version", "17.5.1").
Header("ris-sdk-version", "2.113.0").
Header("X-Locale", RegionProviders[a.region].XLocal).
Header("X-SessionId", uuid.New().String()).
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func (a *API) GetCapabilities(ctx context.Context, token, vin string) (*GetCapabilitiesResponse, error) {
var res GetCapabilitiesResponse
err := requests.
URL(RegionProviders[a.region].ResetURL).
Pathf("/v1/vehicle/%s/capabilities", vin).
Header("Authorization", "Bearer "+token).
Header("X-TrackingId", uuid.New().String()).
Header("ris-os-name", "ios").
Header("ris-os-version", "17.5.1").
Header("ris-sdk-version", "2.113.0").
Header("X-Locale", RegionProviders[a.region].XLocal).
Header("X-SessionId", uuid.New().String()).
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func (a *API) GetCommandCapabilities(ctx context.Context, token, vin string) (*GetCommandCapabilitiesResponse, error) {
var res GetCommandCapabilitiesResponse
err := requests.
URL(RegionProviders[a.region].ResetURL).
Pathf("/v1/vehicle/%s/capabilities/commands", vin).
Header("Authorization", "Bearer "+token).
Header("X-TrackingId", uuid.New().String()).
Header("ris-os-name", "ios").
Header("ris-os-version", "17.5.1").
Header("ris-sdk-version", "2.113.0").
Header("X-Locale", RegionProviders[a.region].XLocal).
Header("X-SessionId", uuid.New().String()).
ToJSON(&res).
Fetch(ctx)
return &res, err
}
func WithAPIRegion(region Region) APIOption {
return APIOptionFun(func(api *API) {
api.region = region
})
}
type APIOption interface {
apply(*API)
}
type APIOptionFun func(*API)
func (f APIOptionFun) apply(api *API) {
f(api)
}