-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconnect.js
104 lines (94 loc) · 3.14 KB
/
connect.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
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
console.log('connect.js loaded')
connect=function(){
// ini
console.log('ini at '+Date())
let div = document.body.querySelector('#connectDiv')
if(div){connect.UI(div)}
}
connect.api= location.origin.match('localhost') ? "http://localhost:3000" : "https://nih-nci-dceg-episphere-dev.appspot.com"
connect.UI=function(div){
// show/hide POST options
sendPost.onclick=function(){
ifPost.hidden=false
}
sendGet.onclick=function(){
ifPost.hidden=true
}
// check API status
fetch(connect.api).then(resp=>{
resp.text().then(y=>{
apistatus.textContent=resp.statusText
if(resp.ok){apistatus.style.color="green"}
})
})
// sendPost.onchange=evt=>{sendGet.checked=!evt.target.checked}
// sendGet.onchange=evt=>{sendPost.checked=!evt.target.checked}
loadFile.onchange=function(evt){
var files=evt.target.files
if(files.length>0){
let file=files[0]
let fileType = file.name.slice(file.name.lastIndexOf('.')+1, file.name.length);
filename.value=file.name // copy name to filename input
document.getElementById('filename').value = filename.value;
document.getElementById('fileType').value = fileType;
sendPost.click() // presume file loaded is to be posted (i.e. it is not a list of commands)
var reader = new FileReader()
reader.onload = function() {
sendContent.value=reader.result
//debugger
}
reader.readAsText(file)
//debugger
}
}
doSend.onclick=evt=>{
// method
responded.value = ""
let method = "GET"
if (sendPost.checked) {
method="POST"
}
let txt = sendContent.value !== "" ? sendContent.value : 'status';
var opts = {
method:method,
headers:{
"Authorization": `Bearer ${callKey.value}`,
"Content-Type": "application/json"
}
}
if(method=="POST"){
const fileType = document.getElementById('fileType').value;
if(fileType !== 'csv' && fileType !== 'tsv' && fileType !== 'json') {
handleError('File type not supported!');
return;
}
if(filename.value === "" || fileType.value === ""){
handleError('Please upload a file!');
return;
}
if(txt === ""){
handleError('File is empty!');
return;
}
opts.body=JSON.stringify({
data: txt,
filename: filename.value,
type: fileType
})
txt="submit"
}
handleError('');
fetch(connect.api+"/"+txt,opts).then(resp=>{
resp.json().then(y=>{
responded.value=JSON.stringify(y,null,3)
})
})
//debugger
}
}
handleError = (err) => {
let errorMessage = document.getElementById('error');
errorMessage.innerHTML = err;
}
// ini
window.onload=connect