-
Notifications
You must be signed in to change notification settings - Fork 27
/
api.js
71 lines (64 loc) · 2.3 KB
/
api.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
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
const API_URL = "https://api.gobelieve.io";
var api = {
token:"",
uploadAudio:function(filePath) {
var url = API_URL + "/v2/audios";
var formData = new FormData();
console.log("uri:", filePath);
var s = filePath.split("/");
if (s.length == 0) {
return;
}
var fileName = s[s.length-1];
formData.append('file', {uri: "file://" + filePath, name:fileName, type:"audio/amr-nb"});
let options = {};
options.body = formData;
options.method = 'post';
options.headers = {
"Authorization":"Bearer " + this.token,
'Content-Type': 'multipart/form-data',
};
return fetch(url, options)
.then((response) => {
return Promise.all([response.status, response.json()]);
})
.then((values)=>{
var status = values[0];
var respJson = values[1];
if (status != 200) {
console.log("upload image fail:", respJson);
return Promise.reject(respJson);
}
console.log("upload image success:", respJson);
return respJson.src_url;
});
},
uploadImage: function(uri, fileName) {
var url = API_URL + "/v2/images";
var formData = new FormData();
formData.append('file', {uri: uri, name:fileName, type:"image/jpeg"});
let options = {};
options.body = formData;
options.method = 'POST';
options.headers = {
'Content-Type': 'multipart/form-data; boundary=6ff46e0b6b5148d984f148b6542e5a5d',
"Authorization":"Bearer " + this.token,
};
return fetch(url, options)
.then((response) => {
return Promise.all([response.status, response.json()]);
})
.then((values)=>{
var status = values[0];
var respJson = values[1];
if (status != 200) {
console.log("upload image fail:", respJson);
Promise.reject(respJson);
return;
}
console.log("upload image success:", respJson);
return respJson.src_url;
});
}
};
export default api;