Skip to content

Commit adecf98

Browse files
author
doripjonov
committed
added try catch blocks to ajax requests
1 parent d3126b3 commit adecf98

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-50
lines changed

endpoints/recognition_endpoints.js

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,21 @@ const recognition_endpoints = {
2828
async recognize_face_request(image_path, url, api_key ){
2929
var bodyFormData = new FormData();
3030
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
31+
return new Promise( async (resolve, reject) => {
32+
try {
33+
const response = await axios.post( url, bodyFormData, {
34+
headers: {
35+
...bodyFormData.getHeaders(),
36+
"Content-Length": bodyFormData.getLengthSync(),
37+
"x-api-key": api_key
38+
},
39+
})
3140

32-
const response = await axios.post( url, bodyFormData, {
33-
headers: {
34-
...bodyFormData.getHeaders(),
35-
"Content-Length": bodyFormData.getLengthSync(),
36-
"x-api-key": api_key
37-
},
41+
resolve(response)
42+
} catch (error) {
43+
reject(error)
44+
}
3845
})
39-
40-
return response;
4146
},
4247

4348
/**
@@ -50,16 +55,23 @@ const recognition_endpoints = {
5055
async verify_face_request(image_path, url, api_key ){
5156
var bodyFormData = new FormData();
5257
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
58+
59+
return new Promise( async (resolve, reject) => {
60+
try {
61+
const response = await axios.post( url, bodyFormData, {
62+
headers: {
63+
...bodyFormData.getHeaders(),
64+
"Content-Length": bodyFormData.getLengthSync(),
65+
"x-api-key": api_key
66+
},
67+
})
5368

54-
const response = await axios.post( url, bodyFormData, {
55-
headers: {
56-
...bodyFormData.getHeaders(),
57-
"Content-Length": bodyFormData.getLengthSync(),
58-
"x-api-key": api_key
59-
},
69+
resolve(response)
70+
} catch (error) {
71+
reject(error)
72+
}
73+
6074
})
61-
62-
return response;
6375
},
6476

6577
/**
@@ -72,15 +84,21 @@ const recognition_endpoints = {
7284
var bodyFormData = new FormData();
7385
bodyFormData.append('file', fs.createReadStream(image_path), { knownLength: fs.statSync(image_path).size });
7486

75-
const response = await axios.post( url, bodyFormData, {
76-
headers: {
77-
...bodyFormData.getHeaders(),
78-
"Content-Length": bodyFormData.getLengthSync(),
79-
"x-api-key": api_key
80-
},
81-
})
87+
return new Promise( async (resolve, reject) => {
88+
try {
89+
const response = await axios.post( url, bodyFormData, {
90+
headers: {
91+
...bodyFormData.getHeaders(),
92+
"Content-Length": bodyFormData.getLengthSync(),
93+
"x-api-key": api_key
94+
},
95+
})
8296

83-
return response;
97+
resolve(response)
98+
} catch (error) {
99+
reject(error)
100+
}
101+
})
84102
},
85103

86104
/**
@@ -112,7 +130,7 @@ const recognition_endpoints = {
112130
}
113131
})
114132
.catch(error => {
115-
return reject(error)
133+
reject(error)
116134
})
117135
})
118136
},
@@ -124,12 +142,19 @@ const recognition_endpoints = {
124142
* @returns {Promise}
125143
*/
126144
async list_request(url, api_key){
127-
const response = await axios.get(url, {
128-
headers: {
129-
"x-api-key": api_key
130-
}})
145+
return new Promise( async (resolve, reject) => {
146+
try {
147+
const response = await axios.get(url, {
148+
headers: {
149+
"x-api-key": api_key
150+
}
151+
})
131152

132-
return response;
153+
resolve(response)
154+
} catch (error) {
155+
reject(error)
156+
}
157+
})
133158
},
134159

135160
/**
@@ -139,12 +164,19 @@ const recognition_endpoints = {
139164
* @returns {Promise}
140165
*/
141166
async delete_request(url, api_key){
142-
const response = await axios.delete(url, {
143-
headers: {
144-
"x-api-key": api_key
145-
}})
146-
147-
return response;
167+
return new Promise( async (resolve, reject) => {
168+
try {
169+
const response = await axios.delete(url, {
170+
headers: {
171+
"x-api-key": api_key
172+
}
173+
})
174+
175+
resolve(response)
176+
} catch (error) {
177+
reject(error)
178+
}
179+
})
148180
}
149181
}
150182

index.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,4 @@
1515
*/
1616
import { CompreFace } from './core/compre_face.js';
1717

18-
let server = "http://localhost";
19-
let port = 8000;
20-
let image_url = "https://www.youtube.com/watch?v=Bs65qyxglHg";
21-
let key = "00000000-0000-0000-0000-000000000002";
22-
let image_id = '0ef8edcf-42fa-42ff-b6f0-6c15bd27782d';
23-
let options = { limit: 0, det_prob_threshold: 0, prediction_count: 1 }
24-
25-
let core = new CompreFace(server, port, options);
26-
let recognition_service = core.initFaceRecognitionService(key);
27-
let faceCollection = recognition_service.getFaceCollection();
28-
29-
faceCollection.add(image_url, "Vimeo")
30-
.then(response => console.log(response))
31-
.catch(error => console.log(error))
18+
export { CompreFace }

0 commit comments

Comments
 (0)