Skip to content

Commit

Permalink
BREAKING CHANGE: Update and streamline SDKs
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Update and streamline SDKs
This is a major release consisting of multiple usability improvements and additional features.

We have aimed to streamline the SDK with the Smartcar API interfaces in a way that the objects returned are closer to the API interface documented in [API Docs](https://smartcar.com/docs/api#smartcar-api-reference). For ex. `vehicle.odometer()` will now return an object that looks like the [response in the documentation](https://smartcar.com/docs/api#get-odometer) and additionally the body will contain response headers [defined in the documentation](https://smartcar.com/docs/api#headers) as a part of a `meta` attribute.

The methods to be used are broadly divided into three namespaces :

- `AuthClient` - This class is used for all OAuth related operations
- `Smartcar` - This class is used for all application level operations
- `Vehicle` - This class is used for all vehicle operations/actions.

**NOTE - According to the changes made to the terms of [nodejs versions support](https://github.com/smartcar/node-sdk#supported-nodejs-versions), NodeJS 6 and 8 are no longer supported.**

- Environment variables - The SDK now supports usage of environment variables for client id (`SMARTCAR_CLIENT_ID`), client secret(`SMARTCAR_CLIENT_SECRET`) and redirect URL(`SMARTCAR_REDIRECT_URL`). These can be used instead of having to pass these as arguments.
- `hashChallenge` - Additional utility method defined in `Smartcar` namespace to generate hash challenge for webhooks.
- `verifyPayload` - Additional utility method defined in `Smartcar` namespace to verify the payload returned by webhooks.
- `getApiVersion` - Method defined in `Smartcar` namespace to return the api version set globally.
- `subscribe` - Additional method defined in `Vehicle` namespace to subscribe to a webhook.
- `unsubscribe` - Additional method defined in `Vehicle` namespace to unsubscribe from a webhook.
- Default API version to 2.0 - The default version for the APIs is now `2.0` instead of `1.0` . This can be overridden globally by using the `setApiVersion` method or by using optional arguments in different methods.

Following are the improvements made to the interfaces by namespace. For in-depth details of the interface please refer to the [SDK documentation](https://github.com/smartcar/node-sdk/tree/master/doc).

- `Constructor` - This only requires the set of parameters required by all of the functions defined in the class. Previously this was also taking in all the arguments used for generating auth URL. This has not been separated and streamlined.
- `getAuthUrl` - Takes in scope as required argument and all the other optional arguments required to generate the Smartcar Connect URL as defined in the [docs](https://smartcar.com/docs/api?version=v2.0&language=cURL#smartcar-connect)
- `exchangeCode` - Added additional support for optional flags parameter for future usage.
- `exchangeRefreshToken` - Added additional support for optional flags parameter for future usage.

- `getVehicles` - Renamed from `getVehicleIds` and changes in interface.
- `getUser` - Moved and renamed from `Vehicle#getUserIds` and changes in interface.
- `getCompatibility` - Moved and renamed from `AuthClient#isCompatible` and changes in interface.

- `Constructor` - Updated to now support a version parameter. Look at the interface for more details.
- `attributes` - Renamed from `info` .
- `batch` - The return value of the method has been changed. This now returns a function for each attributes requested that either returns an object of the requested attribute OR throws an error if the attribute returned an error.

All the errors have been converged to a single `SmartcarError` class. This class can now support the error fields returned by v2.0 and v1.0. For detailed breakdown of both the error types, refer to the to the [API Reference Errors section](https://smartcar.com/docs/api/#errors).
  • Loading branch information
s-ashwinkumar committed Jul 12, 2021
1 parent ad46f07 commit f740585
Show file tree
Hide file tree
Showing 24 changed files with 4,270 additions and 6,070 deletions.
8 changes: 4 additions & 4 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"lines": 98,
"branches": 98,
"functions": 98,
"statements": 98,
"lines": 95,
"branches": 95,
"functions": 95,
"statements": 95,
"all" : false,
"cache": true,
"reporter": [
Expand Down
17 changes: 6 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ language:
- node_js

node_js:
- "10"
- "8"
- "6"
- '14'
- '12'
- '10'

cache:
directories:
- "$HOME/.npm"
- ".eslintcache"
- '$HOME/.npm'
- '.eslintcache'

if: tag IS blank # do not build tags

Expand All @@ -26,10 +26,6 @@ notifications:
secure: "CHS39tAvw6WiIN8cYSyE3QT2PVeFdrHszT/00kU3pb1BH5Ec5JYx5U7bMrVf3yq3GQC/wxBhXs4E119LwZM0n04CGUOshShqSkLeUf9RfWYlqx2Qw2+tT8sOi+OXCzE+yaG3Yt+TWFqKyC5t0A9jZX6cdJbcBaKX2wJiOyI/HiTpXHXrJgeeflxRe03KDpIfpmWgpMjnouZ6rKMnP30H+CG4Ya5uouM/Sv5flgJ+1VnZo/kB89hQ4CELr3bBfxSW4lCS1Tmg/z8w059D2nsn7wiMols3Qgw4FJu773K03fyLGoV4JshxA9lvnLt/Vy+azDNEBP5drQeQ7l8GMrLAPIEN8oGbuH9+TyYoxj0P38Kx4hzlW3owGs1U2+wCuYCq2b58oGTYonKnynFV4Pi8f94uBWd6ziIJoKhwx6MsJzKIt/6T91QWjWozOpF9uGy81ZfR3WHU/gnIyWDTJsLnB7nFA5z9V3/K3Orj5tlVr7iZbCUhA9v6XMYTpyyxjoMtBjVad2IztaWXZIZ97Xx7WBkGS9lvFZIqgHMYERb/On/4bEEXdPlzyJxwwlPBNvPv7enVAsYjPJJ58CQ42fuYkMYZlNcTGfYz9Nw/K64ocMidfdMKvdFD1w6Cw/U1HkQ0SZssy6yEb1BpzdBfMbsUDnh0OJffUpeGp402XOwUNT4="


before_install:
# npm@^7.0.0 requires Node.js >= 10
- dpkg --compare-versions `npm -v` ge 6.0 || npm i -g npm@^6.0.0

install:
- npm ci
- firefox -headless &
Expand All @@ -40,8 +36,7 @@ script:
- test -z "$(git diff --name-only | grep '^doc/readme.md$')"

- npm run test:unit
# e2e tests use async/await
- if test "$TRAVIS_NODE_VERSION" -gt 6; then npm run test:e2e; fi
- npm run test:e2e
- npm run cover

after_success:
Expand Down
73 changes: 38 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ not have access to the dashboard, please
### Flow

- Create a new `AuthClient` object with your `clientId`, `clientSecret`,
`redirectUri`, and required `scope`.
- Redirect the user to Smartcar Connect using `getAuthUrl` or one
`redirectUri`.
- Redirect the user to Smartcar Connect using `getAuthUrl` with required `scope` or with one
of our frontend SDKs.
- The user will login, and then accept or deny your `scope`'s permissions.
- Handle the get request to `redirectUri`.
Expand All @@ -44,7 +44,7 @@ not have access to the dashboard, please
to `"access_denied"`.
- If you passed a state parameter to `getAuthUrl`, `req.query.state` will
contain the state value.
- Get the user's vehicles with `getVehicleIds`.
- Get the user's vehicles with `getVehicles`.
- Create a new `Vehicle` object using a `vehicleId` from the previous response,
and the `access_token`.
- Make requests to the Smartcar API.
Expand All @@ -70,23 +70,22 @@ const app = express();
const port = 4000;

const client = new smartcar.AuthClient({
clientId: 'SMARTCAR_CLIENT_ID',
clientSecret: 'SMARTCAR_CLIENT_SECRET',
redirectUri: 'YOUR_CALLBACK_URI',
scope: ['read_vehicle_info'],
clientId: '<Smartcar Client Id>', // fallback to SMARTCAR_CLIENT_ID ENV variable
clientSecret: '<Smartcar Client Secret>', // fallback to SMARTCAR_CLIENT_SECRET ENV variable
redirectUri: '<Your callback URI>', // fallback to SMARTCAR_REDIRECT_URI ENV variable
testMode: true, // launch Smartcar Connect in test mode
});

// Redirect to Smartcar Connect
app.get('/login', function(req, res) {
const link = client.getAuthUrl();
const link = client.getAuthUrl(['read_vehicle_info']);

// redirect to the link
res.redirect(link);
});

// Handle Smartcar callback with auth code
app.get('/callback', function(req, res, next) {
app.get('/callback', async function(req, res, next) {
let access;

if (req.query.error) {
Expand All @@ -95,32 +94,23 @@ app.get('/callback', function(req, res, next) {
}

// exchange auth code for access token
return client
.exchangeCode(req.query.code)
.then(function(_access) {
// in a production app you'll want to store this in some kind of persistent storage
access = _access;
// get the user's vehicles
return smartcar.getVehicleIds(access.accessToken);
})
.then(function(res) {
// instantiate first vehicle in vehicle list
const vehicle = new smartcar.Vehicle(res.vehicles[0], access.accessToken);
// get identifying information about a vehicle
return vehicle.info();
})
.then(function(data) {
console.log(data);
// {
// "id": "36ab27d0-fd9d-4455-823a-ce30af709ffc",
// "make": "TESLA",
// "model": "Model S",
// "year": 2014
// }

// json response will be sent to the user
res.json(data);
});
const tokens = await client.exchangeCode(req.query.code)
// get the user's vehicles
const vehicles = await smartcar.getVehicles(tokens.accessToken);
// instantiate first vehicle in vehicle list
const vehicle = new smartcar.Vehicle(vehicles.vehicles[0], tokens.accessToken);
// get identifying information about a vehicle
const attributes = await vehicle.attributes();
console.log(attributes);
// {
// "id": "36ab27d0-fd9d-4455-823a-ce30af709ffc",
// "make": "TESLA",
// "model": "Model S",
// "year": 2014
// "meta": {
// "requestId": "ada7207c-3c0a-4027-a47f-6215ce6f7b93"
// }
// }
});

app.listen(port, () => console.log(`Listening on port ${port}`));
Expand All @@ -145,7 +135,20 @@ To test:
npm run test
```

Note: In order to run tests locally the following environment variables would have to be set :
- `E2E_SMARTCAR_CLIENT_ID` - Client ID to be used.
- `E2E_SMARTCAR_CLIENT_SECRET` - Client secret to be used.
- `E2E_SMARTCAR_REDIRECT_URI` - Redirect URI for the auth flow.
- `E2E_SMARTCAR_AMT` - AMT from dashboard for webhooks tests.
- `E2E_SMARTCAR_WEBHOOK_ID` - Webhook ID use in the webhook tests success case.

[ci-url]: https://travis-ci.com/smartcar/node-sdk
[ci-image]: https://travis-ci.com/smartcar/node-sdk.svg?token=jMbuVtXPGeJMPdsn7RQ5&branch=master
[npm-url]: https://badge.fury.io/js/smartcar
[npm-image]: https://badge.fury.io/js/smartcar.svg

## Supported Node.js Versions

Smartcar aims to support the SDK on all Node.js versions that have a status of "Maintenance" or "Active LTS" as defined in the [Node.js Release schedule](https://github.com/nodejs/Release#release-schedule).

In accordance with the Semantic Versioning specification, the addition of support for new Node.js versions would result in a MINOR version bump and the removal of support for Node.js versions would result in a MAJOR version bump.
Loading

0 comments on commit f740585

Please sign in to comment.