From 0dab96da2778d7ecfecb6ac6c71126b8ef04bc90 Mon Sep 17 00:00:00 2001 From: ahsan nawaz Date: Mon, 13 Aug 2018 19:01:44 +0500 Subject: [PATCH 1/2] updated --- GeolocationParams.js | 37 +++++++++++++ IPGeolocationAPI.js | 127 +++++++++++++++++++++++++++++++++++++++++++ TimezoneParams.js | 42 ++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 GeolocationParams.js create mode 100644 IPGeolocationAPI.js create mode 100644 TimezoneParams.js 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..b0214a3 --- /dev/null +++ b/TimezoneParams.js @@ -0,0 +1,42 @@ +module.exports = class TimezoneParams { + + + constructor() { + var timezone = ""; + var ip = ""; + var latitude = 1000.0; + var longitude = 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; + } + +} + + From 51d75ae9b9824d40305b8095a0a65d8d8348010e Mon Sep 17 00:00:00 2001 From: ahsannawaz111 <41775747+ahsannawaz111@users.noreply.github.com> Date: Wed, 15 Aug 2018 12:15:06 +0500 Subject: [PATCH 2/2] Update README.md --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e76f62..44da8c1 100644 --- a/README.md +++ b/README.md @@ -1 +1,55 @@ -# ip-geolocation-api-javascript-sdk \ No newline at end of file +# ip-geolocation-api-javascript-sdk + +## Installation +------------ + npm i ip-geolocation-api-javascript-sdk +------------ + +## Usage +```js +var IPGeolocationAPI = require('ip-geolocation-api-javascript-sdk'); +var GeolocationParams = require('ip-geolocation-api-javascript-sdk/GeolocationParams.js'); +var TimezoneParams = require('ip-geolocation-api-javascript-sdk/TimezoneParams.js'); +``` +### Setup API +```js +var api = new IPGeolocationAPI("YOUR_API_KEY"); +``` + +### Geolocation Lookup +```js +// Query geolocation for IP address (1.1.1.1) and fields (geo, time_zone and currency) +var geolocationParams = new GeolocationParams(); +geolocationParams.setIp("1.1.1.1"); +geolocationParams.setFields("geo,time_zone,currency"); + +console.log(api.getGeolocation(geolocationParams)); + +// Query geolocation for IP address (1.1.1.1) and all fields +GeolocationParams geoParams = new GeolocationParams(); +geoParams.SetIp("1.1.1.1"); + +console.log(api.getGeolocation(geolocationParams)); + +// Query geolocation for the calling machine's IP address for all fields +console.log(api.getGeolocation()); +``` + +### Bulk Geolocations Lookup +```js +// Query geolocations for multiple IP addresses and all fields +var geolocationParams = new GeolocationParams(); +geolocationParams.setIps(['1.1.1.1','2.2.22.2','34.1.1.3']); + +console.log(api.getGeolocation(geolocationParams)); +``` + +### Time Zone API +```js +// Query time zone information by time zone ID +var tzParams = new TimezoneParams(); + +tzParams.setIp("1.1.1.1"); +console.log(obj.getTimezone()); +``` +