enertalk-api-client
v0.6.6
Published
The EnerTalk API Wrapper for convenience
Readme
EnerTalk API Client
The EnerTalk API Wrapper for convenience
Requirements
Authentication must be perform by EnerTalk tokens based on OAuth2.
You can find a way to get tokens on our developer site.
Getting Started
Install Package
> npm install enertalk-api-clientImport Package
const EnerTalkAPI = require('enertalk-api-client');
or
import EnerTalkAPI from 'enertalk-api-client';NOTE:
There's a compatibility issue.
If you're using webpack based project like create-react-app,
please use the build file as below:
const EnerTalkAPI = require('enertalk-api-client/build');
or
import EnerTalkAPI from 'enertalk-api-client/build';Make an instance
const api = new EnerTalkAPI(authConfig, options);Use methods with promise
api.getUser()
.then(response => console.log(response.data))
.catch(error => console.log(error.response.data));Auth Config
Set the authConfig at the point of initialization.
const api = new EnerTalkAPI({
accessToken: 'yourAccessToken',
refreshToken: 'yourRefreshToken',
clientId: 'yourClientId',
clientSecret: 'yourClientSecret',
domain: 'yourCustomAuthServerDomain', // Default domain will be override
tokenUpdateFn: (tokenObject) => {
// The logic that update your token storage
// `tokenObject`has fields below:
// - `accessToken`
// - `refreshToken`
// - `expiresIn` (unit: second)
},
});Or you can update the authConfig at any time.
api.updateAuthConfig({
accessToken: 'yourAccessToken',
refreshToken: 'yourRefreshToken',
// ...otherFields,
});NOTES:
- The prameters
refreshToken,clientId,clientSecret,domainare used to issue new access token.tokenUpdateFnwill be called when a new token issued. You can sync the token object with your token storage.
Request Options
This option follows axios request config.
For example,
const api = new EnerTalkAPI(authConfig, {
baseURL: 'yourCustomResourceServerDomain',
timeout: 10000,
});Pick API Category
You can pick specific categories by second or third option when construct instance.
For example,
const api = new EnerTalkAPI(authConfig, {
baseURL: 'yourCustomResourceServerDomain',
timeout: 10000,
}, ['bills', 'devices', 'usages']);or apiConfig options can be omitted.
const api = new EnerTalkAPI(authConfig, ['bills', 'devices', 'usages']);It's possible to pass single API Category
const api = new EnerTalkAPI(authConfig, 'bills');Supported API Methods
Clicking on a category takes you to the api document.
These will continue to be added.
