Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pmundt committed Jun 17, 2020
0 parents commit 6f9a740
Show file tree
Hide file tree
Showing 11 changed files with 716 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2019 Adaptant Solutions AG
Copyright (c) 2019 Paul Mundt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Go SDK for Mercedes-Benz Connected Vehicle API

## Overview

The [Mercedes-Benz Connected Vehicle API][connected_vehicle_api] is an experimental API for accessing connected vehicle
data and for prototyping connected vehicle services. It provides extensive information about the vehicle itself (vehicle
information, tire pressure, door status, location & heading, odometer, fuel level, as well as the state of battery
charge for electric vehicles), while also providing limited actuation controls (door lock / unlock).

[connected_vehicle_api]: https://developer.mercedes-benz.com/products/connected_vehicle/

## Installation

If not using Go modules, the SDK can be installed the same way as for other Go projects:

```
$ go get github.com/adaptant-labs/mercedes-connectedvehicle-go
```

## Getting Started

1\. Initiate a new client connection with your API key
```
// Initiate a new client connection. Set 'false' for tryout, 'true' for production API.
client := mercedes.NewClient(<API Key>, false)
```

2\. Obtain a list of vehicles
```
vehicles, err := client.GetVehicles(context.TODO())
```

3\. Choose a vehicle to perform operations on
```
vehicle := client.NewVehicle(vehicles[0].Id)
```

4\. Carry out vehicle-specific operations
```
// Vehicle APIs
detail, err := vehicle.GetVehicleDetail(context.TODO())
tires, err := vehicle.GetTirePressure(context.TODO())
err := vehicle.LockDoors(context.TODO())
err := vehicle.UnockDoors(context.TODO())
doors, err := vehicle.GetDoorStatus(context.TODO())
location, err := vehicle.GetLocation(context.TODO())
distance, err := vehicle.GetDistanceDriven(context.TODO())
level, err := vehicle.GetFuelLevel(context.TODO())
charge, err := vehicle.GetStateOfCharge(context.TODO())
```

## Testing with the Mercedes-Benz Car Simulator

The SDK itself can be used together with the [Mercedes-Benz Car Simulator][simulator], but must go through the
appropriate Oauth2 authentication flows in order to become accessible from the Connected Vehicle API. Note that in this
case, the client must be configured for using the production API, and initiate the connection with the exchanged access
token. A simple example of this is included in the `examples` directory.

[simulator]: https://car-simulator.developer.mercedes-benz.com/

## Features and bugs

Please file feature requests and bugs concerning the SDK itself in the [issue tracker][tracker]. Note that as this is a
third-party SDK and we have no direct affiliation with Mercedes-Benz, we are unable to handle feature requests for the
REST API itself.

[tracker]: https://github.com/adaptant-labs/mercedes-connectedvehicle-go/issues

## License

`mercedes-connectedvehicle-go` is released under the terms of the MIT license, the full
version of which can be found in the LICENSE file included in the distribution.
11 changes: 11 additions & 0 deletions authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mercedes

import "golang.org/x/oauth2"

var (
DefaultScopes = []string{"mb:user:pool:reader", "mb:vehicle:status:general"}
Endpoint = oauth2.Endpoint{
AuthURL: "https://api.secure.mercedes-benz.com/oidc10/auth/oauth/v2/authorize",
TokenURL: "https://api.secure.mercedes-benz.com/oidc10/auth/oauth/v2/token",
}
)
88 changes: 88 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package mercedes

import (
"bytes"
"context"
"encoding/json"
"net/http"
)

var (
apiTryoutBase = "https://api.mercedes-benz.com/experimental/connectedvehicle_tryout/v1"
apiProductionBase = "https://api.mercedes-benz.com/experimental/connectedvehicle/v1"
)

// Provide a default HTTP transport mechanism that wraps requests with the API key / Bearer token.
type apiKeyHttpTransport struct {
apiKey string
}

func (a *apiKeyHttpTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("Authorization", "Bearer "+a.apiKey)
return http.DefaultTransport.RoundTrip(r)
}

type ConnectedVehicleClient struct {
apiBase string
httpClient *http.Client
}

func (m *ConnectedVehicleClient) getJson(ctx context.Context, path string, target interface{}) error {
req, _ := http.NewRequestWithContext(ctx, "GET", m.apiBase+path, nil)
r, err := m.httpClient.Do(req)
if err != nil {
return err
}
defer r.Body.Close()

if r.StatusCode == http.StatusUnauthorized {
println("unauthorized")
return err
}
return json.NewDecoder(r.Body).Decode(target)
}

func (m *ConnectedVehicleClient) postJson(ctx context.Context, path string, payload interface{}) error {
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(payload)
if err != nil {
return err
}

req, _ := http.NewRequestWithContext(ctx, "POST", m.apiBase+path, b)
req.Header.Add("Content-Type", "application/json")

_, err = m.httpClient.Do(req)
if err != nil {
return err
}

return nil
}

func NewClient(apiKey string, productionMode bool) *ConnectedVehicleClient {
m := &ConnectedVehicleClient{
httpClient: &http.Client{Transport: &apiKeyHttpTransport{apiKey: apiKey}},
}

if productionMode == true {
m.apiBase = apiProductionBase
} else {
m.apiBase = apiTryoutBase
}

return m
}

func (m *ConnectedVehicleClient) NewVehicle(vehicleId string) *ConnectedVehicle {
return &ConnectedVehicle{
VehicleID: vehicleId,
client: m,
}
}

func (m *ConnectedVehicleClient) GetVehicles(ctx context.Context) ([]Vehicle, error) {
var vehicles []Vehicle
ret := m.getJson(ctx, "/vehicles", &vehicles)
return vehicles, ret
}
34 changes: 34 additions & 0 deletions examples/car-simulator-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# car-simulator-auth

This is a simple example of negotiating the Oauth2 login and consent flows described by the
[Mercedes-Benz OAuth Authentication Documentation][oauth2-docs] in order to access vehicle data, to be used in
conjunction with the [Mercedes-Benz Simulator][simulator]. Once consent has been given by the user, the door locks of the
simulated vehicle will be toggled, which can be observed directly in the simulator:

```
11:16:01 - Mercedes-Benz Simulator - Starting initialization
11:16:01 - Mercedes-Benz Simulator - Initialized capabilities
11:16:02 - Mercedes-Benz Simulator - Car with serial number 7410ED2522A7AA3256 initialized
11:19:55 - Sandbox device - CapabilityManager - Incoming telematics message for Door Locks - ACASAQAEAQABAQ==
11:19:55 - Sandbox device - Door Locks - Lock doors
11:20:01 - Sandbox device - CapabilityManager - Incoming telematics message for Door Locks - ACAA
11:20:01 - Sandbox device - Door Locks - Get Lock State
11:20:07 - Sandbox device - CapabilityManager - Incoming telematics message for Door Locks - ACASAQAEAQABAA==
11:20:07 - Sandbox device - Door Locks - Unlock doors
```

# Environment Variables

In order to carry out the OAuth flow, the client ID and secret must be provided. These are to be set in the following
environment variables:

```
MERCEDES_CLIENT_ID
MERCEDES_CLIENT_SECRET
```

These are available from the [Mercedes-Benz Developer Console][console].

[oauth2-docs]: https://developer.mercedes-benz.com/content-page/oauth-documentation
[simulator]: https://car-simulator.developer.mercedes-benz.com/
[console]: https://developer.mercedes-benz.com/console
126 changes: 126 additions & 0 deletions examples/car-simulator-auth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package main

import (
"context"
"errors"
"fmt"
"github.com/adaptant-labs/mercedes-connectedvehicle-go"
"golang.org/x/oauth2"
"log"
"math/rand"
"net/http"
"os"
"time"
)

var (
oauth2Config = oauth2.Config{
ClientID: os.Getenv("MERCEDES_CLIENT_ID"),
ClientSecret: os.Getenv("MERCEDES_CLIENT_SECRET"),
RedirectURL: "http://localhost:8080/callback",
Scopes: mercedes.DefaultScopes,
Endpoint: mercedes.Endpoint,
}

oauthStateString string
)

func randomString(n int) string {
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

b := make([]rune, n)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
return string(b)
}

func handleIndex(w http.ResponseWriter, r *http.Request) {
var html = `<html>
<body>
<a href="/login">Mercedes Log in</a>
</body>
</html>
`
fmt.Fprintf(w, html)
}

func handleLogin(w http.ResponseWriter, r *http.Request) {
url := oauth2Config.AuthCodeURL(oauthStateString)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func handleCallback(w http.ResponseWriter, r *http.Request) {
err := toggleDoorLocks(r.FormValue("state"), r.FormValue("code"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}

func toggleDoorLocks(state string, code string) error {
// Make sure we have a valid request
if state != oauthStateString {
return errors.New("oauth state strings do not match")
}

// Exchange code for access token
token, err := oauth2Config.Exchange(context.TODO(), code)
if err != nil {
return err
}

// Use the new access token for subsequent API calls
client := mercedes.NewClient(token.AccessToken, true)
vehicles, err := client.GetVehicles(context.TODO())
if err != nil {
panic(err)
}

// Make sure there are vehicles available to the user
if len(vehicles) == 0 {
return errors.New("no vehicles discovered")
}

// Fetch a vehicle to operate on
vehicle := client.NewVehicle(vehicles[0].Id)

// Obtain more detailed information about the vehicle
detail, err := vehicle.GetVehicleDetail(context.TODO())
if err != nil {
return err
}

go func() {
// Lock doors
fmt.Printf("Locking doors for vehicle %s (VIN %s)...\n", vehicle.VehicleID, detail.VIN)
_ = vehicle.LockDoors(context.TODO())

time.Sleep(5 * time.Second)

// Fetch door lock status
doors, _ := vehicle.GetDoorStatus(context.TODO())

println("Front left door is now", doors.DoorLockStatusFrontLeft.Value)
time.Sleep(5 * time.Second)

// Unlock doors
println("Unlocking...")
_ = vehicle.UnlockDoors(context.TODO())
}()

return nil
}

func main() {
// Generate a random state string for validation
rand.Seed(time.Now().UnixNano())
oauthStateString = randomString(16)

// Handlers for Oauth2 login and consent flows
http.HandleFunc("/", handleIndex)
http.HandleFunc("/login", handleLogin)
http.HandleFunc("/callback", handleCallback)

println("Awaiting completion of login and consent flow at http://localhost:8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/adaptant-labs/mercedes-connectedvehicle-go

go 1.13

require golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Loading

0 comments on commit 6f9a740

Please sign in to comment.