diff --git a/GeolocationParams.js b/GeolocationParams.js new file mode 100644 index 0000000..b70cf36 --- /dev/null +++ b/GeolocationParams.js @@ -0,0 +1,37 @@ +module.exports = class GeolocationParams { + + constructor() { + var ip = ""; + var fields = ""; + var ips = ""; + } + + setIp(ip = "") { + this.ip = ip; + } + + getIp() { + return this.ip; + } + + setFields(fields = "") { + this.fields = fields; + } + + getFields() { + return this.fields; + } + + setIps(ips = "") { + if(ips.length > 50) { + console.log("Max. number of IP addresses cannot be more than 50."); + } else { + this.ips = ips; + } + } + + getIps() { + return this.ips; + } +} + diff --git a/IPGeolocationAPI.js b/IPGeolocationAPI.js new file mode 100644 index 0000000..646faa7 --- /dev/null +++ b/IPGeolocationAPI.js @@ -0,0 +1,127 @@ +var GeolocationParams = require('./GeolocationParams.js'); +var TimezoneParams = require('./TimezoneParams.js'); + +module.exports = class IPGeolocationAPI { + + + constructor(apiKey = "") { + this.apiKey = apiKey; + } + + getApiKey() { + return this.apiKey; + } + + getGeolocation(params = null) { + if(params.getIps()) + { return postRequest("ipgeo-bulk", params, this.apiKey); + }else{ + return getRequest("ipgeo", buildGeolocationUrlParams(params, this.apiKey)); + } + } + + getTimezone(params = null) { + return getRequest("timezone", buildTimezoneUrlParams(params, this.apiKey)); + } + + +} + + +function buildTimezoneUrlParams(params=null, apiKey="") { + var urlParams = "apiKey=" + apiKey; + + if(params != null) { + var param = params.getIp(); + if(param && param != "") { + urlParams = urlParams + "&ip=" + param; + } + + param = params.getTimezone(); + if(param && param != "") { + urlParams = urlParams + "&tz=" + param; + } + + var latitude = params.getLatitude(); + var longitude = params.getLongitude(); + if(latitude && latitude != 1000.0 && longitude && longitude != 1000.0) { + urlParams = urlParams + "&lat=" + latitude + "&long=" + longitude; + } + } + return urlParams; +} + + + +function buildGeolocationUrlParams(params=null, apiKey="") { + + var urlParams = "apiKey=" + apiKey; + if(params != null) { + var param = params.getIp(); + + if(param && param != "") { + urlParams = urlParams + "&ip=" + param; + } + + param = params.getFields(); + + if(param && param != "") { + urlParams = urlParams + "&fields=" + param; + } + } + + return urlParams; +} + + + + + +function getRequest(subUrl = "", params = ""){ + +var jsonData = null; +var data = null; + + +var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +var xhr = new XMLHttpRequest(); +xhr.withCredentials = true; +xhr.addEventListener("readystatechange", function () { +if (this.readyState === 4) { + + jsonData = JSON.parse(this.responseText); +} +}); +console.log("https://api.ipgeolocation.io/"+subUrl+"?"+params+""); +xhr.open("GET", "https://api.ipgeolocation.io/"+subUrl+"?"+params+"", false); +xhr.send(data); + +return jsonData; + +} + + + + +function postRequest(subUrl = "", params = "", apiKey=""){ + +var jsonData = null; +var data = JSON.stringify({ + "ips": params.getIps() +}); +var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; +var xhr = new XMLHttpRequest(); +xhr.withCredentials = true; +xhr.addEventListener("readystatechange", function () { +if (this.readyState === 4) { + jsonData = JSON.parse(this.responseText); +} +}); +xhr.open("POST", "https://api.ipgeolocation.io/"+subUrl+"?apiKey="+apiKey+"", false); +xhr.setRequestHeader("Content-Type", "application/json"); + +xhr.send(data); +return jsonData; + +} + diff --git a/TimezoneParams.js b/TimezoneParams.js new file mode 100644 index 0000000..231bab2 --- /dev/null +++ b/TimezoneParams.js @@ -0,0 +1,43 @@ +module.exports = class TimezoneParams { + + + constructor() { + var timezone = ""; + var ip = ""; + var latitude = 1000.0; + var latitude = 1000.0; + } + + setTimezone(timezone = "") { + this.timezone = timezone; + } + + getTimezone() { + return this.timezone; + } + + setIp(ip = "") { + this.ip = ip; + } + + getIp() { + return this.ip; + } + + setLocation(latitude = "", longitude = "") { + this.latitude = latitude; + this.longitude = longitude; + } + + getLatitude() { + return this.latitude; + } + + getLongitude() { + return this.longitude; + } + + +} + +