bit4sat/web/src/api.js

136 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-03-23 17:01:49 +00:00
import 'babel-polyfill';
2019-03-23 17:01:49 +00:00
const endPoints = {
2019-04-02 13:53:00 +00:00
upload: '/a/u',
session: '/a/session',
pollstatus: '/a/u/poll',
checkstatus: '/a/u/check'
2019-04-02 13:53:00 +00:00
}
export async function lastSession(){
let req = new Request(endPoints.session, {
method: 'GET',
credentials: 'same-origin'
})
let res = await fetch(req)
.catch((e) => {console.error(e)})
2019-03-23 17:01:49 +00:00
2019-04-02 13:53:00 +00:00
return res.json()
2019-03-23 17:01:49 +00:00
}
export async function checkUploadStatus(uploadId){
let req = new Request(endPoints.checkstatus + '/' + uploadId,{
method: 'GET',
credentials: 'same-origin'
})
let res = await fetch(req)
if (!res.ok && res.status != 402){
let json = await res.json()
.catch((e)=>{
console.error(res.text())
})
console.error(`${res.status}: ` + json.error)
}
2019-04-05 22:02:04 +00:00
return res.json().catch((e)=>{console.error(e)})
}
2019-04-02 17:40:25 +00:00
export async function pollUploadStatus(uploadId){
2019-04-02 13:53:00 +00:00
//console.log('polling upload status')
2019-04-02 13:53:00 +00:00
let req = new Request(endPoints.pollstatus + '/' + uploadId,{
2019-04-02 13:53:00 +00:00
method: 'GET',
credentials: 'same-origin'
2019-04-02 13:53:00 +00:00
})
let res = await fetch(req)
.catch((e) => {console.error(e)})
2019-04-05 22:02:04 +00:00
return res.json().catch((e)=>{console.error(e)})
2019-04-02 13:53:00 +00:00
}
2019-03-23 17:01:49 +00:00
class Upload {
2019-04-05 16:18:44 +00:00
constructor(filesMetadata, fileObjects, options) {
2019-03-23 17:01:49 +00:00
this.filesMetadata = Array.from(filesMetadata)
this.fileObjects = fileObjects
this.timestamp = new Date()
2019-04-05 16:18:44 +00:00
this.options = options
2019-03-23 17:01:49 +00:00
}
get uploadMetadata() {
return {
files: this.filesMetadata,
timestamp: this.timestamp,
2019-04-05 16:18:44 +00:00
...this.options
2019-03-23 17:01:49 +00:00
}
}
async create() {
let req = new Request(endPoints.upload , {
method: 'POST',
2019-03-31 20:53:53 +00:00
credentials: 'same-origin',
2019-03-23 17:01:49 +00:00
body: JSON.stringify(this.uploadMetadata)
})
//let result = await fetch(req);
let res = await fetch(req)
.catch((e) => { console.error(e) })
return res.json().then(data => {
2019-04-02 13:53:00 +00:00
let { upload_id: id } = data
2019-03-23 17:01:49 +00:00
this.uploadId = id
return data
})
}
2019-04-02 13:53:00 +00:00
async checkstatus(){
2019-04-02 17:40:25 +00:00
return pollUploadStatus(this.uploadId)
2019-04-02 13:53:00 +00:00
}
2019-03-23 17:01:49 +00:00
async send(){
console.log(`Sending with id ${this.uploadId}`)
let formData = new FormData();
for (let file of this.fileObjects) {
formData.append('upload[]', file)
}
let req = new Request(endPoints.upload + '/' + this.uploadId, {
2019-03-23 17:01:49 +00:00
method: 'PUT',
2019-03-31 20:53:53 +00:00
credentials: 'same-origin',
2019-03-23 17:01:49 +00:00
body: formData
})
let res = await fetch(req)
.catch((e) => {console.error(e)})
2019-03-27 19:13:07 +00:00
return res.json()
2019-03-23 17:01:49 +00:00
}
}
export default {
endPoints: endPoints,
Upload: Upload,
2019-04-02 17:40:25 +00:00
pollUploadStatus: pollUploadStatus,
checkUploadStatus: checkUploadStatus
2019-03-23 17:01:49 +00:00
}