//Global imports
const axios = require('axios');
const FormData = require('form-data');
//Disable SSL certificate chain verification
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0;
//Global variable that will store the token used in the samples
let token = '';
//API username for accessing the REST services
let api_user = "apiuser";
//API password or key for accessing the REST services
let api_key = "********";
//private token to access client details
let loginToken = '';
//REST endpoint base url
let api_url = "http://localhost:8888";
let client_username = '';
/**
This function will generate an access token using the provided API keys.
The generated bearer token is then stored in a global variable and used for all the other API requests
* @returns {Promise<void>}
*/
async function getAccessToken() {
let data = new FormData();
data.append('username', api_user);
data.append('password', api_key);
let config = {
method: 'post',
url: api_url + '/api/v1/security/request/access-token',
headers: {...data.getHeaders()},
data: data
};
await axios(config)
.then(function (response) {
token = response.data.access_token;
console.log("Generated token ->", token);
return token;
})
.catch(function (error) {
console.log(error);
})
}
/**
Here a new client record is created using the client_username and client_password specified above.
The application will return an ID for the new client record and this can be used to retrieve the details of the record
* @returns {Promise<void>}
*/
async function createCustomer() {
client_username = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
console.log("Creating client with username => ", client_username)
let data = {
"active": true,
"channel": "WEB",
"firstName": "John",
"lastName": "Smith",
"cellPhone": "08012345678",
"emailAddress1": "john.smith@mail.com",
"portalUserName": client_username,
"portalPassword": '********',
"partnerType": "INDIVIDUAL",
// "customerGroupName": "PLATINUM",//# The default value configured in application will be used if not specified
// "businessOfficeName": "LAGOS"//The default value configured in application will be used if not specified
};
let config = {
method: 'post',
url: api_url + '/api/v1/partner/customer/create',
headers: {'Authorization': 'Bearer ' + token},
data: data
};
let customerId =
await axios(config)
.then(function (response) {
console.log(response.data)
let customerId = response.data['msgCode'];
console.log("Created new client with ID ->[", response.data['msgCode'], "] & Request Status ->[",
response.data['success'], "]");
return customerId;
})
.catch(function (error) {
console.log(error);
});
return customerId;
}
/**
After the client record is created, the username and password specified during the setup can be used to login
If the login is successful, the application returns a token.
This token has to be included in the 'X-Auth-Token' header when retrieving the client's details.
* @returns {Promise<void>}
*/
async function loginCustomer() {
let data = new FormData();
data.append('username', client_username);
data.append('password', '********');
let config = {
method: 'post',
url: api_url + '/api/v1/security/login/customer',
headers: {...data.getHeaders(), 'Authorization': 'Bearer ' + token},
data: data
};
await axios(config)
.then(function (response) {
console.log(response.data);
//retrieve the client details using the private token from the login step
let loginToken = response.data['msgCode'];
let loginResult = response.data['success'];
console.log("Login success ->[" , loginResult , "] & Private Key ->[" , loginToken , "]")
retrieveCustomerDetails(loginToken);
})
.catch(function (error) {
console.log(error);
});
}
/**
* This function accepts an integer value for the client identifier and retrieves the record.
* @param customerId
* @returns {Promise<void>}
*/
async function findCustomerById(customerId) {
let data = ''
config = {
method: 'get',
url: api_url + '/api/v1/partner/customer/id/' + customerId,
headers: {'Authorization': 'Bearer ' + token},
data: data
};
await axios(config)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}
/**
*
* @param username
* @returns {Promise<void>}
*/
async function retrieveCustomerDetails(loginToken) {
let data = ''
config = {
method: 'get',
url: api_url + "/api/v1/security/customer/username/" + client_username,
headers: {'Authorization': 'Bearer ' + token, 'X-Auth-Token': loginToken},
data: data
};
await axios(config)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});
}
/**
Retrieve an access token
Create a new client
Login with the client's credentials.
* @returns {Promise<void>}
*/
const runApiSamples = async () => {
console.log("Starting execution ... ");
//Get the access token
await getAccessToken();
//Create a client record
let customerId = await createCustomer();
//Retrieve the client record with the ID returned by the API
await findCustomerById(customerId);
//Login the client with the generated token
await loginCustomer();
console.log("Finished execution ... ");
}
runApiSamples();