Enode
In order to register Enode vehicle, you need to:
- Initialize SDK client.
- Fetch vehicle vendors.
- Register a vehicle.
- Get devices list.
Step 1: Initialize SDK Client
Initialize Client using the Hiven apiUrl (optional, defaults to https://api.hiven.energy) and headersGetter (optional, defaults to async () => ({})) providing custom authorization headers.
import { createClient } from '@hiven-energy/hiven-client';
const client = createClient({
apiUrl: '<apiUrl>',
headersGetter: async () => ({
'X-Api-Key': '<apiKey>',
'X-User-Id': '<userId>',
}),
});
Step 2: Fetch Vehicle Vendors
Before starting registering vehicle, you need to fetch vehicle vendors that are currently enabled:
client.getEnodeVehicleVendors();
Step 3: Register a Vehicle
Register a vehicle by retrieving authorization url and using Link SDK to redirect users to it:
import { VehicleManufacturer } from '@hiven-energy/hiven-client';
import * as LinkSDK from '@youssefhenna/expo-enode-link-sdk';
useEffect(() => {
const subscription = LinkSDK.listenToResult((resultCode, errorMessage) => {
switch (resultCode) {
case 'success':
console.log('Your account was linked successfully!');
break;
case 'cancelledByUser':
console.log('You cancelled the linking flow.');
break;
default:
console.log(`Linking failed: ${resultCode}, ${errorMessage}`);
break;
}
});
return () => {
subscription.remove();
};
}, []);
// ...
export const getApplicationRedirectUri = (): string => {
// Return redirect url to your application
};
const handleAuthorize = async (vendor: VehicleManufacturer) => {
// ...
const { linkToken } = await client.getEnodeAuthorizationUrl(vendor, getApplicationRedirectUri());
LinkSDK.show(linkToken);
};
Let the user provide credentials and finish flow.
Step 4: Get Devices List
As soon as 200 response is received after sending authorization code to Hiven, the vehicle should already be added.
Validate it by fetching the list of vehicles:
client.getDevicesList(userId: string, options?: Options);
Important note: The userId is a unique identifier of a user in your system. Read more here.
Important note[2]: Hiven stores access and refresh tokens in Hiven Backend, so you don't have to worry about it.
Options parameter
Every SDK method is shipped with an optional options?: Options parameter which allows to overwrite any header, set in the headersGetter function during client creation:
interface Options {
headers: Record<string, any>;
}