try {
	var creds = JSON.parse(value);
}
catch (error) {
	throw 'Failed to parse authentication data from input value.';
}

var request = new HttpRequest(),
	hosts = [],
	error_msg = '',
	url = '{$PURE.FLASHARRAY.API.URL}',
	version = '{$PURE.FLASHARRAY.API.VERSION}',
	http_proxy = '{$PURE.FLASHARRAY.HTTP_PROXY}';

function getHttpData(url) {
	response = request.get(url);
	Zabbix.log(4, '[ Pure Storage API ] [ ' + url + ' ] Received response with status code ' + request.getStatus() + ': ' + response);

	if (request.getStatus() !== 200) {
		throw 'Failed to receive data: invalid response status code.';
	}

	if (response !== null) {
		try {
			response = JSON.parse(response)['items'];
		}
		catch (error) {
			throw 'Failed to parse response received from Pure Storage API. Check debug log for more information.';
		}
	}

	if (typeof response !== 'object' || response === null) {
		throw 'Cannot process response data: received data is not an object.';
	}

	return response;
};

try {

	['token'].forEach(function (field) {
		if (creds[field] === null || creds[field] == '' || typeof creds[field] === 'undefined') {
			throw 'Unexpected session token value has been received'
			+ ((typeof creds.error !== 'undefined' && creds.error != '') ? ': ' + creds.error : '');
		}
	});

	if (url === '{' + '$PURE.FLASHARRAY.API.URL}') {
		throw 'Please change {' + '$PURE.FLASHARRAY.API.URL} macro to the proper value.';
	}

	if (url.indexOf('http://') === -1 && url.indexOf('https://') === -1) {
		url = 'https://' + url + (!url.endsWith('/') ? '/' : '');
	}

	if (http_proxy !== '{' + '$PURE.FLASHARRAY.HTTP_PROXY}' && http_proxy !== '') {
		request.setProxy(http_proxy);
	}

	request.addHeader('x-auth-token: ' + creds.token);
	request.addHeader('Accept: application/json');

	hosts = getHttpData(url + 'api/' + version + '/hosts');

	getHttpData(
		url + 'api/' + version + '/hosts/performance'
	).forEach(function (item) {
		hosts.forEach(function (host, index) {
			if (host['name'] && item['name'] && host['name'] == item['name']) {
				hosts[index]['performance'] = item;
			}
		});
	});

} catch (err) {
	error_msg = err;
};

return JSON.stringify({
	'hosts': hosts,
	'error': error_msg.toString()
});