This example shows how to retrieve all available IP intelligence properties from the 51Degrees cloud service.
This example shows how to retrieve all available IP intelligence properties from the 51Degrees cloud service.You will learn:
To run this example, you will eventually need to create a resource key. A resource key is used as shorthand to store the particular set of properties you are interested in as well as any associated license keys that entitle you to increased request limits and/or paid-for properties, but it is not yet available for IP intelligence. For now, you can run the onpremise/mixed/gettingstarted-api example to simulate a custom hosted cloud service, and point this example to it by setting the '_51DEGREES_CLOUD_ENDPOINT' environment variable to 'http://localhost:5225/'. Any value can then be used for the resource key.
const path = require('path');
const require51 = (requestedPackage) => {
try {
return require(path.join(__dirname, '/../../../node_modules/', requestedPackage));
} catch (e) {
return require(requestedPackage);
}
};
const fs = require('fs');
const PipelineBuilder = require51(
'fiftyone.pipeline.core').PipelineBuilder;
const ExampleUtils = require(path.join(__dirname,
'/../exampleUtils'));
const OptionsExtension = require51(
'fiftyone.ipintelligence.shared').optionsExtension;
const DataExtension = require51(
'fiftyone.ipintelligence.shared').dataExtension;
const SOME_IP_ADDRESS = '8.8.8.8';
const getValueToOutput = function (ip, propertyName) {
if (value !== undefined && value !== null && value.length > 200) {
value = value.substring(0, 200) + '...';
}
return value;
};
const analyse = async function (ipAddress, pipeline, output) {
const data = pipeline.createFlowData();
data.evidence.add('query.client-ip', ipAddress);
await data.process();
const ip = data.ip;
output.write('What property values are associated with ' +
`the IP '${ipAddress}'?\n`);
for (const propertyName of Object.keys(ip.contents).sort()) {
output.write(
`${propertyName} = ${getValueToOutput(ip, propertyName)}\n`);
}
};
const run = async function (options, output) {
if (!resourceKey) {
console.log(
'No resource key specified in the configuration file ' +
'\'51d.json\' or the environment variable ' +
`'${ExampleUtils.RESOURCE_KEY_VARIABLE}'. The 51Degrees cloud ` +
'service is accessed using a \'ResourceKey\'. For more information ' +
'see ' +
'/documentation/_services__cloud__resource_keys.html?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-getallproperties-console-getallproperties.js&utm_term=resource-key-required. ' +
'A resource key with everything this example displays can be ' +
'created at https://configure.51degrees.com/hYzn3TV3?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-getallproperties-console-getallproperties.js&utm_term=resource-key-required with a paid ' +
'subscription. A free key created at ' +
'https://configure.51degrees.com/Wkqxf3Bs?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-getallproperties-console-getallproperties.js&utm_term=resource-key-required populates only the ' +
'Country, LocationConfidence, Ip and IpV6 properties. See ' +
'/pricing?utm_source=code&utm_medium=example&utm_campaign=51degrees-node&utm_content=fiftyone.ipintelligence.cloud-examples-cloud-getallproperties-console-getallproperties.js&utm_term=resource-key-required to get a paid subscription with ' +
'more properties. Once complete, populate the config file or ' +
'environment variable mentioned at the start of this message ' +
'with the key.'
);
return;
}
pipeline.on('error', console.error);
await analyse(SOME_IP_ADDRESS, pipeline, output);
};
if (process.env.JEST_WORKER_ID === undefined) {
const args = process.argv.slice(2);
const resourceKey = args.length > 0 ? args[0] :
ExampleUtils.getResourceKeyFromEnv();
const options = JSON.parse(fs.readFileSync(path.join(__dirname, '/51d.json')), 'utf8');
if (!resourceKeyFromConfig || resourceKeyFromConfig.startsWith('!!')) {
}
run(options, process.stdout);
}
module.exports = {
run
};
Definition dataExtension.js:23
static getValueHelper(elementData, propertyName)
Helper to get a human readable value of an aspect property value.
Definition dataExtension.js:55
Definition fiftyone.ipintelligence.onpremise/examples/onpremise/exampleUtils.js:203
Definition optionsExtension.js:27
static setResourceKey(options, resourceKey)
Set the resource key in the pipeline options.
Definition optionsExtension.js:123
static getResourceKey(options)
Get the resource key from the pipeline options.
Definition optionsExtension.js:107
buildFromConfiguration(config)