Skip to content

Commit 7e9fa3b

Browse files
committed
Async API call
1 parent d4452da commit 7e9fa3b

File tree

4 files changed

+157
-108
lines changed

4 files changed

+157
-108
lines changed

GeolocationParams.js

100644100755
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module.exports = class GeolocationParams {
22

33
constructor() {
4-
var ip = "";
5-
var fields = "";
6-
var ips = "";
4+
var ip = "";
5+
var fields = "";
6+
var ips = "";
77
}
88

9-
setIp(ip = "") {
10-
this.ip = ip;
9+
setIP(ip = "") {
10+
this.ip = ip;
1111
}
1212

13-
getIp() {
13+
getIP() {
1414
return this.ip;
1515
}
1616

@@ -22,15 +22,15 @@ module.exports = class GeolocationParams {
2222
return this.fields;
2323
}
2424

25-
setIps(ips = "") {
25+
setIPList(ips = []) {
2626
if(ips.length > 50) {
2727
console.log("Max. number of IP addresses cannot be more than 50.");
2828
} else {
2929
this.ips = ips;
3030
}
3131
}
3232

33-
getIps() {
33+
getIPList() {
3434
return this.ips;
3535
}
3636
}

IPGeolocationAPI.js

100644100755
Lines changed: 110 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -3,112 +3,133 @@ var TimezoneParams = require('./TimezoneParams.js');
33

44
module.exports = class IPGeolocationAPI {
55

6-
constructor(apiKey = "") {
6+
constructor(apiKey = "") {
77
this.apiKey = apiKey;
88
}
99

1010
getApiKey() {
1111
return this.apiKey;
1212
}
1313

14-
getGeolocation(params = null) {
15-
if(params.getIps()) {
16-
return postRequest("ipgeo-bulk", params, this.apiKey);
14+
getGeolocation(params = null, callback) {
15+
if(params && params.getIPList()) {
16+
return postRequest("ipgeo-bulk", params, this.apiKey, callback);
1717
} else {
18-
return getRequest("ipgeo", buildGeolocationUrlParams(params, this.apiKey));
19-
}
18+
return getRequest("ipgeo", buildGeolocationUrlParams(params, this.apiKey), callback);
19+
}
2020
}
2121

22-
getTimezone(params = null) {
23-
return getRequest("timezone", buildTimezoneUrlParams(params, this.apiKey));
22+
getTimezone(params = null, callback) {
23+
return getRequest("timezone", buildTimezoneUrlParams(params, this.apiKey), callback);
2424
}
2525
}
2626

27-
function buildTimezoneUrlParams(params=null, apiKey="") {
28-
var urlParams = "apiKey=" + apiKey;
29-
30-
if(params != null) {
31-
var param = params.getIp();
32-
if(param && param != "") {
33-
urlParams = urlParams + "&ip=" + param;
34-
}
35-
36-
param = params.getTimezone();
37-
if(param && param != "") {
38-
urlParams = urlParams + "&tz=" + param;
39-
}
40-
41-
var latitude = params.getLatitude();
42-
var longitude = params.getLongitude();
43-
if(latitude && latitude != 1000.0 && longitude && longitude != 1000.0) {
44-
urlParams = urlParams + "&lat=" + latitude + "&long=" + longitude;
45-
}
46-
}
47-
return urlParams;
27+
function buildTimezoneUrlParams(params = null, apiKey = "") {
28+
var urlParams = "";
29+
30+
if(apiKey) {
31+
urlParams = urlParams.concat("apiKey=", apiKey);
32+
}
33+
34+
if(params) {
35+
var ip = params.getIP();
36+
37+
if(ip) {
38+
if(urlParams) {
39+
urlParams = urlParams.concat("&");
40+
}
41+
urlParams = urlParams.concat("ip=", ip);
42+
}
43+
44+
var tz = params.getTimezone();
45+
46+
if(tz) {
47+
if(urlParams) {
48+
urlParams = urlParams.concat("&");
49+
}
50+
urlParams = urlParams.concat("tz=", tz);
51+
}
52+
53+
var latitude = params.getLatitude();
54+
var longitude = params.getLongitude();
55+
56+
if(latitude && latitude !== 1000.0 && longitude && longitude !== 1000.0) {
57+
if(urlParams) {
58+
urlParams = urlParams.concat("&");
59+
}
60+
urlParams = urlParams.concat("lat=", latitude, "&long=", longitude);
61+
}
62+
}
63+
return urlParams;
4864
}
4965

50-
function buildGeolocationUrlParams(params=null, apiKey="") {
51-
var urlParams = "apiKey=" + apiKey;
52-
53-
if(params != null) {
54-
var param = params.getIp();
55-
56-
if(param && param != "") {
57-
urlParams = urlParams + "&ip=" + param;
58-
}
59-
param = params.getFields();
60-
if(param && param != "") {
61-
urlParams = urlParams + "&fields=" + param;
62-
}
63-
}
64-
return urlParams;
66+
function buildGeolocationUrlParams(params = null, apiKey = "") {
67+
var urlParams = "";
68+
69+
if(apiKey) {
70+
urlParams = urlParams.concat("apiKey=", apiKey);
71+
}
72+
73+
if(params) {
74+
var ip = params.getIP();
75+
76+
if(ip) {
77+
if(urlParams) {
78+
urlParams = urlParams.concat("&");
79+
}
80+
urlParams = urlParams.concat("ip=", ip);
81+
}
82+
83+
var fields = params.getFields();
84+
85+
if(fields) {
86+
if(urlParams) {
87+
urlParams = urlParams.concat("&");
88+
}
89+
urlParams = urlParams.concat("fields=", fields);
90+
}
91+
}
92+
return urlParams;
6593
}
6694

67-
function getRequest(subUrl="", params="") {
68-
var jsonData = null;
69-
var data = null;
70-
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
71-
var xhr = new XMLHttpRequest();
72-
xhr.withCredentials = true;
73-
74-
xhr.addEventListener("readystatechange", function () {
75-
if(this.readyState === 4) {
76-
if(this.status === 0) {
77-
jsonData = {
78-
"message": "Internet is not connected!"
79-
};
80-
} else {
81-
jsonData = JSON.parse(this.responseText);
82-
}
83-
}
84-
});
85-
xhr.open("GET", "https://api.ipgeolocation.io/"+subUrl+"?"+params+"", false);
86-
xhr.send(data);
87-
return jsonData;
95+
function getRequest(subUrl = "", params = "", callback) {
96+
var jsonData = null;
97+
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
98+
var xhttp = new XMLHttpRequest();
99+
100+
xhttp.withCredentials = true;
101+
xhttp.onreadystatechange = function() {
102+
if (this.readyState === 4) {
103+
jsonData = JSON.parse(this.responseText);
104+
105+
if (callback && typeof(callback) === typeof(Function)) {
106+
callback(jsonData);
107+
}
108+
}
109+
};
110+
xhttp.open("GET", "https://api.ipgeolocation.io/".concat(subUrl, "?", params, ""), true);
111+
xhttp.send();
88112
}
89113

90-
function postRequest(subUrl="", params="", apiKey="") {
91-
var jsonData = null;
92-
var data = JSON.stringify({
93-
"ips": params.getIps()
94-
});
95-
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
96-
var xhr = new XMLHttpRequest();
97-
xhr.withCredentials = true;
98-
99-
xhr.addEventListener("readystatechange", function () {
100-
if(this.readyState === 4) {
101-
if(this.status === 0) {
102-
jsonData = {
103-
"message": "Internet is not connected!"
104-
};
105-
} else {
106-
jsonData = JSON.parse(this.responseText);
107-
}
108-
}
109-
});
110-
xhr.open("POST", "https://api.ipgeolocation.io/"+subUrl+"?apiKey="+apiKey+"", false);
111-
xhr.setRequestHeader("Content-Type", "application/json");
112-
xhr.send(data);
113-
return jsonData;
114+
function postRequest(subUrl = "", params = "", apiKey = "", callback) {
115+
var jsonData = null;
116+
var data = JSON.stringify({
117+
"ips": params.getIPList()
118+
});
119+
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
120+
var xhttp = new XMLHttpRequest();
121+
122+
xhttp.withCredentials = true;
123+
xhttp.onreadystatechange = function() {
124+
if (this.readyState === 4) {
125+
jsonData = JSON.parse(this.responseText);
126+
127+
if (callback && typeof(callback) === typeof(Function)) {
128+
callback(jsonData);
129+
}
130+
}
131+
};
132+
xhttp.open("POST", "https://api.ipgeolocation.io/".concat(subUrl, "?apiKey=", apiKey, ""), true);
133+
xhttp.setRequestHeader("Content-Type", "application/json");
134+
xhttp.send(data);
114135
}

TimezoneParams.js

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ module.exports = class TimezoneParams {
1515
return this.timezone;
1616
}
1717

18-
setIp(ip = "") {
18+
setIP(ip = "") {
1919
this.ip = ip;
2020
}
2121

22-
getIp() {
22+
getIP() {
2323
return this.ip;
2424
}
2525

package.json

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
{
2-
"name": "ip-geolocation-api-javascript-sdk",
3-
"version": "1.0.3",
4-
"description": "",
5-
"main": "IPGeolocationAPI.js",
2+
"_from": "ip-geolocation-api-javascript-sdk",
3+
"_id": "ip-geolocation-api-javascript-sdk@1.0.5",
4+
"_inBundle": false,
5+
"_integrity": "sha512-hzaB5USO7B3XltJTckXK9FtC+OpSiGx4h8/nV3eskFhmmTBC1YVTd9WnlJTUQm3sb9e+V3pzu8coNbXC3ZrRPg==",
6+
"_location": "/ip-geolocation-api-javascript-sdk",
7+
"_phantomChildren": {},
8+
"_requested": {
9+
"type": "tag",
10+
"registry": true,
11+
"raw": "ip-geolocation-api-javascript-sdk",
12+
"name": "ip-geolocation-api-javascript-sdk",
13+
"escapedName": "ip-geolocation-api-javascript-sdk",
14+
"rawSpec": "",
15+
"saveSpec": null,
16+
"fetchSpec": "latest"
17+
},
18+
"_requiredBy": [
19+
"#USER",
20+
"/"
21+
],
22+
"_resolved": "https://registry.npmjs.org/ip-geolocation-api-javascript-sdk/-/ip-geolocation-api-javascript-sdk-1.0.5.tgz",
23+
"_shasum": "0273f7c043749ce91a586388edf35319d2c52818",
24+
"_spec": "ip-geolocation-api-javascript-sdk",
25+
"_where": "/home/developer/jvs/new-npm",
26+
"author": {
27+
"name": "ipgeolocation"
28+
},
29+
"bundleDependencies": false,
630
"dependencies": {
731
"xmlhttprequest": "^1.8.0"
832
},
9-
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
11-
},
33+
"deprecated": false,
34+
"description": "## Installation ```cli npm i ip-geolocation-api-javascript-sdk ```",
1235
"keywords": [
1336
"ip",
1437
"ipgeo",
@@ -21,6 +44,11 @@
2144
"geoip api",
2245
"ip location api"
2346
],
24-
"author": "ipgeolocation",
25-
"license": "ISC"
47+
"license": "ISC",
48+
"main": "IPGeolocationAPI.js",
49+
"name": "ip-geolocation-api-javascript-sdk",
50+
"scripts": {
51+
"test": "echo \"Error: no test specified\" && exit 1"
52+
},
53+
"version": "1.0.5"
2654
}

0 commit comments

Comments
 (0)