-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudsight.js
45 lines (39 loc) · 1.29 KB
/
cloudsight.js
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
var axios = require('axios');
const key = process.env.CLOUDSIGHT_API_KEY
function startPolling(token, success, fail) {
function imageReponse() {
axios.request({
url: 'http://api.cloudsight.ai/image_responses/'+token,
method: 'GET',
headers : {
Authorization: 'CloudSight '+CLOUDSIGHT_API_KEY
}
}).then(function(response) {
if(response.data.status == 'completed') {
success(response.data.name);
} else if(response.data.status == 'not completed') {
setTimeout(imageReponse, 600);
} else {
fail(response.data);
}
}).catch(fail);
}
setTimeout(imageReponse, 600);
}
module.exports = function(url, success, fail) {
if(!url) { return '' }
axios.request({
url: 'http://api.cloudsight.ai/image_requests',
method: 'POST',
headers : {
Authorization: 'CloudSight '+CLOUDSIGHT_API_KEY
},
params:{
'image_request[remote_image_url]' : url,
'image_request[language]' : 'fr',
'image_request[locale]': 'fr-FR'
}
}).then(function(response) {
startPolling(response.data.token, success, fail);
}).catch(fail)
}