Skip to content

Commit 0931364

Browse files
committed
Async API call
1 parent 225c78f commit 0931364

File tree

4 files changed

+170
-113
lines changed

4 files changed

+170
-113
lines changed

GeolocationParams.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
export class GeolocationParams {
2-
ip: string;
3-
fields: string;
4-
ips: any;
5-
6-
constructor() {
7-
this.ip = "";
8-
this.fields = "";
9-
this.ips = "";
10-
}
112

12-
setIp(ip = "") {
13-
this.ip = ip;
14-
}
3+
ip: string;
4+
fields: string;
5+
ips: any;
6+
7+
constructor() {
8+
this.ip = "";
9+
this.fields = "";
10+
this.ips = "";
11+
}
1512

16-
getIp() {
17-
return this.ip;
18-
}
13+
setIP(ip = "") {
14+
this.ip = ip;
15+
}
1916

20-
setFields(fields = "") {
21-
this.fields = fields;
22-
}
17+
getIP() {
18+
return this.ip;
19+
}
2320

24-
getFields() {
25-
return this.fields;
26-
}
21+
setFields(fields = "") {
22+
this.fields = fields;
23+
}
2724

28-
setIps(ips : any = null) {
29-
if(ips.length > 50) {
30-
console.log("Max. number of IP addresses cannot be more than 50.");
31-
} else {
32-
this.ips = ips;
33-
}
34-
}
25+
getFields() {
26+
return this.fields;
27+
}
3528

36-
getIps() {
37-
return this.ips;
38-
}
29+
setIPList(ips : any = null) {
30+
if(ips.length > 50) {
31+
console.log("Max. number of IP addresses cannot be more than 50.");
32+
} else {
33+
this.ips = ips;
34+
}
35+
}
36+
37+
getIPList() {
38+
return this.ips;
39+
}
3940
}

IPGeolocationAPI.ts

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,149 @@
11
import {GeolocationParams} from './GeolocationParams';
22
import {TimezoneParams} from './TimezoneParams';
3-
import {XMLHttpRequest} from './node_modules/xmlhttprequest';
4-
3+
import {XMLHttpRequest} from 'xmlhttprequest';
54
export class IPGeolocationAPI {
5+
66
apiKey: string;
77

8-
constructor(apiKey: string) {
8+
constructor(apiKey: string = null) {
99
this.apiKey = apiKey;
1010
}
1111

1212
public getApiKey() {
1313
return this.apiKey;
1414
}
1515

16-
public getGeolocation(params : GeolocationParams = null) {
17-
if(params.getIps()) {
18-
return this.postRequest("ipgeo-bulk", params, this.apiKey);
19-
} else {
20-
return this.getRequest("ipgeo", this.buildGeolocationUrlParams(params, this.apiKey));
16+
public getGeolocation(params : GeolocationParams = null, callback) {
17+
if(params && params.getIPList())
18+
{ return this.postRequest("ipgeo-bulk", params, this.apiKey, callback);
19+
}else {
20+
return this.getRequest("ipgeo", this.buildGeolocationUrlParams(params, this.apiKey), callback);
2121
}
2222
}
2323

24-
public getTimezone(params : TimezoneParams = null) {
25-
return this.getRequest("timezone", this.buildTimezoneUrlParams(params, this.apiKey));
24+
public getTimezone(params : TimezoneParams = null, callback) {
25+
return this.getRequest("timezone", this.buildTimezoneUrlParams(params, this.apiKey), callback);
2626
}
27-
28-
private buildTimezoneUrlParams(params=null, apiKey="") {
29-
var urlParams = "apiKey=" + apiKey;
3027

31-
if(params != null) {
32-
var param = params.getIp();
28+
private buildTimezoneUrlParams(params=null, apiKey="") {
29+
var urlParams = "apiKey=" + apiKey;
3330

34-
if(param && param != "") {
35-
urlParams = urlParams + "&ip=" + param;
36-
}
31+
if(params != null) {
32+
var param = params.getIP();
33+
if(param && param != "") {
34+
urlParams = urlParams + "&ip=" + param;
35+
}
3736

38-
param = params.getTimezone();
39-
if(param && param != "") {
40-
urlParams = urlParams + "&tz=" + param;
41-
}
37+
param = params.getTimezone();
38+
if(param && param != "") {
39+
urlParams = urlParams + "&tz=" + param;
40+
}
4241

43-
var latitude = params.getLatitude();
44-
var longitude = params.getLongitude();
45-
if(latitude && latitude != 1000.0 && longitude && longitude != 1000.0) {
46-
urlParams = urlParams + "&lat=" + latitude + "&long=" + longitude;
47-
}
42+
var latitude = params.getLatitude();
43+
var longitude = params.getLongitude();
44+
if(latitude && latitude != 1000.0 && longitude && longitude != 1000.0) {
45+
urlParams = urlParams + "&lat=" + latitude + "&long=" + longitude;
4846
}
49-
return urlParams;
47+
}
48+
return urlParams;
5049
}
5150

5251
private buildGeolocationUrlParams(params=null, apiKey="") {
53-
var urlParams = "apiKey=" + apiKey;
5452

53+
var urlParams = "apiKey=" + apiKey;
5554
if(params != null) {
56-
var param = params.getIp();
55+
var param = params.getIP();
5756

5857
if(param && param != "") {
5958
urlParams = urlParams + "&ip=" + param;
6059
}
6160

6261
param = params.getFields();
62+
6363
if(param && param != "") {
6464
urlParams = urlParams + "&fields=" + param;
6565
}
6666
}
67+
6768
return urlParams;
6869
}
6970

7071

71-
private getRequest(subUrl = "", params = ""){
72-
var jsonData = null;
73-
var data = null;
74-
var xhr = new XMLHttpRequest();
75-
76-
xhr.withCredentials = true;
77-
xhr.addEventListener("readystatechange", function () {
78-
if(this.readyState === 4) {
79-
if(this.status == 0) {
80-
jsonData = {
81-
"message": "Internet is not connected!"
82-
};
83-
} else {
84-
jsonData = JSON.parse(this.responseText);
85-
}
86-
}
87-
});
88-
89-
// console.log("https://api.ipgeolocation.io/" + subUrl + "?" + params + "");
90-
xhr.open("GET", "https://api.ipgeolocation.io/" + subUrl + "?" + params + "", false);
91-
xhr.send(data);
92-
93-
return jsonData;
72+
private getRequest(subUrl = "", params = "", callback){
73+
74+
var jsonData = null;
75+
var data = null;
76+
var xhr = new XMLHttpRequest();
77+
xhr.withCredentials = true;
78+
xhr.addEventListener("readystatechange", function () {
79+
if (this.readyState === 4) {
80+
81+
if(this.status == 0){
82+
jsonData = {
83+
"message": "Internet is not connected!"
84+
};
85+
}else{
86+
jsonData = JSON.parse(this.responseText);
87+
}
88+
89+
if (callback && typeof(callback) === typeof(Function)) {
90+
callback(jsonData);
91+
}
92+
93+
}
94+
});
95+
xhr.open("GET", "https://api.ipgeolocation.io/"+subUrl+"?"+params+"", true);
96+
xhr.send(data);
97+
return jsonData;
98+
9499
}
95100

96-
private postRequest(subUrl="", params: GeolocationParams=null, apiKey="") {
101+
private postRequest(subUrl = "", params : GeolocationParams = null, apiKey="", callback){
102+
97103
var jsonData = null;
98104
var data = JSON.stringify({
99-
"ips": params.getIps()
105+
"ips": params.getIPList()
100106
});
101107
var xhr = new XMLHttpRequest();
102-
103108
xhr.withCredentials = true;
104109
xhr.addEventListener("readystatechange", function () {
105110
if (this.readyState === 4) {
106-
if(this.status == 0) {
111+
112+
if(this.status == 0){
107113
jsonData = {
108-
"message": "Internet is not connected!"
114+
"message": "Internet is not connected!"
109115
};
110-
} else {
111-
jsonData = JSON.parse(this.responseText);
116+
}else{
117+
jsonData = JSON.parse(this.responseText);
112118
}
119+
120+
if (callback && typeof(callback) === typeof(Function)) {
121+
callback(jsonData);
122+
}
123+
113124
}
114125
});
115-
116-
xhr.open("POST", "https://api.ipgeolocation.io/" + subUrl + "?apiKey=" + apiKey + "", false);
126+
xhr.open("POST", "https://api.ipgeolocation.io/"+subUrl+"?apiKey="+apiKey+"", true);
117127
xhr.setRequestHeader("Content-Type", "application/json");
118128
xhr.send(data);
119129

120130
return jsonData;
131+
121132
}
133+
122134
}
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+

TimezoneParams.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export class TimezoneParams {
2-
timezone: string;
3-
ip: string;
4-
latitude: number;
5-
longitude: number;
2+
3+
timezone :string;
4+
ip :string;
5+
latitude :number;
6+
longitude :number;
67

78
constructor() {
89
this.timezone = "";
@@ -19,11 +20,11 @@ export class TimezoneParams {
1920
return this.timezone;
2021
}
2122

22-
setIp(ip = "") {
23+
setIP(ip = "") {
2324
this.ip = ip;
2425
}
2526

26-
getIp() {
27+
getIP() {
2728
return this.ip;
2829
}
2930

@@ -39,4 +40,6 @@ export class TimezoneParams {
3940
getLongitude() {
4041
return this.longitude;
4142
}
43+
4244
}
45+

package.json

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
{
2-
"name": "ip-geolocation-api-sdk-typescript",
3-
"version": "1.0.2",
4-
"description": "",
5-
"main": "IPGeolocationAPI.ts",
6-
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
8-
},
9-
"repository": {
10-
"type": "git",
11-
"url": "git+ssh://git@gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript.git"
2+
"_from": "ip-geolocation-api-sdk-typescript",
3+
"_id": "ip-geolocation-api-sdk-typescript@1.0.4",
4+
"_inBundle": false,
5+
"_integrity": "sha512-3H57yHooCwH3glQLZskv8fw1PYFB9+95PZzsFO89acrzIeGcUsbUzd91f4uM8OJIODPXYP+UQEBajjIJWIOWeg==",
6+
"_location": "/ip-geolocation-api-sdk-typescript",
7+
"_phantomChildren": {},
8+
"_requested": {
9+
"type": "tag",
10+
"registry": true,
11+
"raw": "ip-geolocation-api-sdk-typescript",
12+
"name": "ip-geolocation-api-sdk-typescript",
13+
"escapedName": "ip-geolocation-api-sdk-typescript",
14+
"rawSpec": "",
15+
"saveSpec": null,
16+
"fetchSpec": "latest"
1217
},
18+
"_requiredBy": [
19+
"#USER",
20+
"/"
21+
],
22+
"_resolved": "https://registry.npmjs.org/ip-geolocation-api-sdk-typescript/-/ip-geolocation-api-sdk-typescript-1.0.4.tgz",
23+
"_shasum": "247858c28360a16ba09569b2e8501385c9d05356",
24+
"_spec": "ip-geolocation-api-sdk-typescript",
25+
"_where": "/home/developer/test-ts2",
1326
"author": "",
14-
"license": "ISC",
1527
"bugs": {
1628
"url": "https://gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript/issues"
1729
},
30+
"bundleDependencies": false,
31+
"dependencies": {
32+
"xmlhttprequest": "^1.8.0"
33+
},
34+
"deprecated": false,
35+
"description": "## Installation ```cli npm i ip-geolocation-api-sdk-typescript ```",
1836
"homepage": "https://gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript#README",
1937
"keywords": [
2038
"ip",
@@ -28,7 +46,15 @@
2846
"geoip api",
2947
"ip location api"
3048
],
31-
"dependencies": {
32-
"xmlhttprequest": "^1.8.0"
33-
}
49+
"license": "ISC",
50+
"main": "IPGeolocationAPI.ts",
51+
"name": "ip-geolocation-api-sdk-typescript",
52+
"repository": {
53+
"type": "git",
54+
"url": "git+ssh://git@gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript.git"
55+
},
56+
"scripts": {
57+
"test": "echo \"Error: no test specified\" && exit 1"
58+
},
59+
"version": "1.0.4"
3460
}

0 commit comments

Comments
 (0)