|
1 | 1 | import {GeolocationParams} from './GeolocationParams';
|
2 | 2 | import {TimezoneParams} from './TimezoneParams';
|
3 |
| -import {XMLHttpRequest} from './node_modules/xmlhttprequest'; |
4 |
| - |
| 3 | +import {XMLHttpRequest} from 'xmlhttprequest'; |
5 | 4 | export class IPGeolocationAPI {
|
| 5 | + |
6 | 6 | apiKey: string;
|
7 | 7 |
|
8 |
| - constructor(apiKey: string) { |
| 8 | + constructor(apiKey: string = null) { |
9 | 9 | this.apiKey = apiKey;
|
10 | 10 | }
|
11 | 11 |
|
12 | 12 | public getApiKey() {
|
13 | 13 | return this.apiKey;
|
14 | 14 | }
|
15 | 15 |
|
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); |
21 | 21 | }
|
22 | 22 | }
|
23 | 23 |
|
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); |
26 | 26 | }
|
27 |
| - |
28 |
| - private buildTimezoneUrlParams(params=null, apiKey="") { |
29 |
| - var urlParams = "apiKey=" + apiKey; |
30 | 27 |
|
31 |
| - if(params != null) { |
32 |
| - var param = params.getIp(); |
| 28 | + private buildTimezoneUrlParams(params=null, apiKey="") { |
| 29 | + var urlParams = "apiKey=" + apiKey; |
33 | 30 |
|
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 | + } |
37 | 36 |
|
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 | + } |
42 | 41 |
|
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; |
48 | 46 | }
|
49 |
| - return urlParams; |
| 47 | + } |
| 48 | + return urlParams; |
50 | 49 | }
|
51 | 50 |
|
52 | 51 | private buildGeolocationUrlParams(params=null, apiKey="") {
|
53 |
| - var urlParams = "apiKey=" + apiKey; |
54 | 52 |
|
| 53 | + var urlParams = "apiKey=" + apiKey; |
55 | 54 | if(params != null) {
|
56 |
| - var param = params.getIp(); |
| 55 | + var param = params.getIP(); |
57 | 56 |
|
58 | 57 | if(param && param != "") {
|
59 | 58 | urlParams = urlParams + "&ip=" + param;
|
60 | 59 | }
|
61 | 60 |
|
62 | 61 | param = params.getFields();
|
| 62 | + |
63 | 63 | if(param && param != "") {
|
64 | 64 | urlParams = urlParams + "&fields=" + param;
|
65 | 65 | }
|
66 | 66 | }
|
| 67 | + |
67 | 68 | return urlParams;
|
68 | 69 | }
|
69 | 70 |
|
70 | 71 |
|
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 | + |
94 | 99 | }
|
95 | 100 |
|
96 |
| - private postRequest(subUrl="", params: GeolocationParams=null, apiKey="") { |
| 101 | + private postRequest(subUrl = "", params : GeolocationParams = null, apiKey="", callback){ |
| 102 | + |
97 | 103 | var jsonData = null;
|
98 | 104 | var data = JSON.stringify({
|
99 |
| - "ips": params.getIps() |
| 105 | + "ips": params.getIPList() |
100 | 106 | });
|
101 | 107 | var xhr = new XMLHttpRequest();
|
102 |
| - |
103 | 108 | xhr.withCredentials = true;
|
104 | 109 | xhr.addEventListener("readystatechange", function () {
|
105 | 110 | if (this.readyState === 4) {
|
106 |
| - if(this.status == 0) { |
| 111 | + |
| 112 | + if(this.status == 0){ |
107 | 113 | jsonData = {
|
108 |
| - "message": "Internet is not connected!" |
| 114 | + "message": "Internet is not connected!" |
109 | 115 | };
|
110 |
| - } else { |
111 |
| - jsonData = JSON.parse(this.responseText); |
| 116 | + }else{ |
| 117 | + jsonData = JSON.parse(this.responseText); |
112 | 118 | }
|
| 119 | + |
| 120 | + if (callback && typeof(callback) === typeof(Function)) { |
| 121 | + callback(jsonData); |
| 122 | + } |
| 123 | + |
113 | 124 | }
|
114 | 125 | });
|
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); |
117 | 127 | xhr.setRequestHeader("Content-Type", "application/json");
|
118 | 128 | xhr.send(data);
|
119 | 129 |
|
120 | 130 | return jsonData;
|
| 131 | + |
121 | 132 | }
|
| 133 | + |
122 | 134 | }
|
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments