This repository was archived by the owner on Mar 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapiCaller.html
More file actions
213 lines (179 loc) · 5.89 KB
/
apiCaller.html
File metadata and controls
213 lines (179 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="author" content="">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Hello, art project!</p>
<p>In this file you can test your module with a custom JSON.</p>
<form>
<p><input type="text" name="url"> The url you wish to call</p>
<div class="errorDivurl"></div>
<p><input type="text" name="urlImg"> The url of the image you want to use</p>
<input type="hidden" name="urlImgB64">
<div class="errorDivurlImg"></div>
<p><input list="callMethods" name="callMethod"> The Method of the call</p>
<div class="errorDivcallMethod"></div>
<datalist id="callMethods">
<option value="POST">
<option value="GET">
</datalist>
<p><input list="uType" name="uType">User type</p>
<div class="errorDivuType"></div>
<datalist id="uType">
<option value="child">Child</option>
<option value="novice">Novice</option>
<option value="knowledgable">Knowledgable</option>
<option value="expert">Expert</option>
</datalist>
<p><input list="uLang" name="uLang">User language</p>
<div class="errorDivuLang"></div>
<datalist id="uLang">
<option value="en_US">English</option>
<option value="it">Italian</option>
<option value="fr">French</option>
<option value="ru">Russian</option>
</datalist>
<p><select multiple>
<option value="history">History</option>
<option value="science">Science</option>
<option value="biography">Biography</option>
<option value="fun_facts">Fun facts</option>
<option value="art_movements">Art movements</option>
<option value="techniques">Techniques</option>
</select>
User preferences</p>
<div class="errorDivpref"></div>
<p><input type="text" name="location"> The location of the user (It will produce lat ang long)</p>
<input type="hidden" name="lat">
<input type="hidden" name="lng">
<div class="errorDivloc"></div>
<a>Submit</a>
<div class="errorDiv"></div>
</form>
<div class="responseDiv"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$('a').on('click', function(){
$('.errorDiv').html('');
$url = $('input[name="url"]').val();
$imgUrl = $('input[name="urlImg"]').val();
$callMethod = $('input[name="callMethod"]').val();
$uType = $('input[name="uType"]').val();
$uLang = $('input[name="uLang"]').val();
$uPref = $('select').val();
$uLoc = $('input[name="location"]').val();
console.log($url );
console.log($uLang );
console.log($uPref );
console.log($uType );
if($uLoc == '' ) {
$('.errorDivloc').html('Lat & Long will be empty');
$lat = ' ';
$lng = ' ';
} else {
$.get( "https://eu1.locationiq.com/v1/search.php?key=654fa264772868&q="+$uLoc+"&format=json", function( data ) {
console.log(data);
$lat = data[0].lat;
$lng = data[0].lon;
$('input[name="lat"]').val($lat);
$('input[name="lng"]').val($lng);
$( ".errorDivloc" ).html( 'Lat: '+$lat+', Long: '+$lng );
});
}
if($imgUrl == '') {
$('.errorDivurlImg').html('Image cannot be empty');
$imgValid = false;
} else {
const toDataURL = url => fetch(url)
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
}))
toDataURL($imgUrl)
.then(dataUrl => {
$('.errorDivurlImg').html('RESULT:'+dataUrl);
$('input[name="urlImgB64"]').val(dataUrl);
})
$imgValid = true;
}
if($url == '') {
$('.errorDivurl').html('Url cannot be empty');
$urlValid = false;
} else {
$urlValid = true;
}
if($callMethod == '') {
$('.errorDivcallMethod').html('callMethod cannot be empty');
$callMethodValid = false;
} else {
$callMethodValid = true;
}
if($uType == '') {
$('.errorDivuType').html('caluTypelMethod cannot be empty');
$uTypeValid = false;
} else {
$uTypeValid = true;
}
if($uLang == '') {
$('.errorDivuLang').html('uLang cannot be empty');
$uLangValid = false;
} else {
$uLangValid = true;
}
if($uPref == '') {
$('.errorDivpref').html('uPref cannot be empty');
$uPrefValid = false;
} else {
$uPrefValid = true;
}
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
// Usage!
sleep(1000).then(() => {
if($imgValid && $urlValid && $callMethodValid && $uTypeValid && $uPrefValid && $uLangValid) {
$base64IMG = $('input[name="urlImgB64"]').val();
$lat = $('input[name="lat"]').val();
$lng = $('input[name="lng"]').val();
$jsonObj = {
image: $base64IMG,
location: {
lat: $lat,
lng: $lng,
},
userdata: {
type: $uType,
preferences: $uPref,
language: $uLang,
},
}
$JSON = JSON.stringify($jsonObj);
console.log($JSON);
if($callMethod == 'POST') {
$.post( $url, {json:$JSON})
.done(function( data ) {
console.log(data);
$( ".responseDiv" ).html( '<pre>'+data+'</pre>' );
});
} else {
$.get( $url, {json:$JSON})
.done(function( data ) {
console.log(data);
});
}
} else {
$('.errorDiv').html('Check the errors');
}
});
});
</script>
</body>
</html>