Doodle3D-API/src/rest.js

85 lines
1.4 KiB
JavaScript
Raw Normal View History

import $ from 'jquery';
2015-10-12 13:06:20 +02:00
const GET_TIMEOUT = 5000;
const POST_TIMEOUT = 10000;
2016-04-21 15:44:20 +02:00
export function get(url) {
2016-04-21 15:52:23 +02:00
return send(url, 'GET');
}
2016-04-21 15:44:20 +02:00
export function post(url, data) {
2016-04-21 15:52:23 +02:00
return send(url, 'POST', data);
2016-04-18 10:19:51 +02:00
}
2016-04-21 15:01:56 +02:00
function send(url, type, data) {
2016-04-21 15:52:23 +02:00
const timeout = (type === 'GET') ? GET_TIMEOUT : POST_TIMEOUT;
return new Promise((resolve, reject) => {
$.ajax({
url, type, data, timeout, dataType: 'json',
success: (response) => {
if (response.status === 'success') {
resolve(response.data);
}
else {
reject(response.msg);
}
}
}).fail(reject);
});
}
/*
import 'github/fetch';
export function get (url) {
2016-04-21 15:52:23 +02:00
return new Promise((resolve, reject) => {
2016-04-21 15:52:23 +02:00
fetch(url).then((response) => {
2016-04-21 15:52:23 +02:00
return response.json();
2016-04-21 15:52:23 +02:00
}).then((json) => {
2016-04-21 15:52:23 +02:00
if (json.status === 'success') {
resolve(json.data);
}
else {
reject(json.msg);
}
2016-04-21 15:52:23 +02:00
}).catch(reject);
2016-04-21 15:52:23 +02:00
});
}
export function post (url, data) {
2016-04-21 15:52:23 +02:00
return new Promise((resolve, reject) => {
2016-04-21 15:52:23 +02:00
fetch(url, {
method: 'post',
enctype: 'x-www-form-urlencoded',
headers: {
'Accept': 'application/json'
},
body: JSON.stringify(data)
}).then((response) => {
2016-04-21 15:52:23 +02:00
return response.json();
2016-04-21 15:52:23 +02:00
}).then((json) => {
2016-04-21 15:52:23 +02:00
if (json.status === 'success') {
resolve(json.data);
}
else {
reject(json.msg);
}
2016-04-21 15:52:23 +02:00
}).catch(reject);
});
}
2016-04-18 09:30:19 +02:00
*/