-
-
Notifications
You must be signed in to change notification settings - Fork 807
/
Copy pathapi.go
84 lines (71 loc) · 2.03 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
package porsche
import (
"fmt"
"net/http"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"golang.org/x/oauth2"
)
const (
ApiURI = "https://api.porsche.com"
PairingComplete = "PAIRINGCOMPLETE"
PairingInProcess = "INPROCESS"
)
func IsPaired(status string) bool {
return status == PairingComplete || status == PairingInProcess
}
// API is an api.Vehicle implementation for Porsche PHEV cars
type API struct {
*request.Helper
}
// NewAPI creates a new vehicle
func NewAPI(log *util.Logger, identity oauth2.TokenSource) *API {
v := &API{
Helper: request.NewHelper(log),
}
v.Client.Transport = &transport.Decorator{
Base: &oauth2.Transport{
Source: identity,
Base: v.Client.Transport,
},
Decorator: transport.DecorateHeaders(map[string]string{
"apikey": OAuth2Config.ClientID,
}),
}
return v
}
// Vehicles implements the vehicle list response
func (v *API) Vehicles() ([]Vehicle, error) {
var res []Vehicle
uri := fmt.Sprintf("%s/core/api/v3/de/de_DE/vehicles", ApiURI)
err := v.GetJSON(uri, &res)
return res, err
}
// PairingStatus implements the vehicle pairing status response
func (v *API) PairingStatus(vin string) (VehiclePairingResponse, error) {
var res VehiclePairingResponse
uri := fmt.Sprintf("%s/core/api/v3/de/de_DE/vehicles/%s/pairing", ApiURI, vin)
err := v.GetJSON(uri, &res)
return res, err
}
// Status implements the vehicle status response
func (v *API) Status(vin string) (StatusResponse, error) {
var res StatusResponse
uri := fmt.Sprintf("%s/vehicle-data/de/de_DE/status/%s", ApiURI, vin)
err := v.GetJSON(uri, &res)
return res, err
}
// WakeUp tries to wakeup the vehicle by requesting the current vehicle overview
func (v *API) WakeUp(vin string) error {
uri := fmt.Sprintf("%s/service-vehicle/de/de_DE/vehicle-data/%s/current/request", ApiURI, vin)
req, err := request.New(http.MethodPost, uri, nil, request.AcceptJSON)
if err != nil {
return err
}
resp, err := v.Do(req)
if err == nil {
resp.Body.Close()
}
return err
}