bit4sat/web/src/api.js

161 lines
3.6 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: '/api/v1/u',
session: '/api/v1/session',
2019-04-07 20:31:35 +00:00
pollupstatus: '/api/v1/u/poll',
checkstatus: '/api/v1/u/check',
2019-04-09 07:20:46 +00:00
download: '/api/v1/d/q', // query download
getFiles: '/api/v1/d/g', // get file
pollinvoice: '/api/v1/pollinvoice',
adminToken: '/api/v1/a/info'
2019-04-02 13:53:00 +00:00
}
2019-04-09 07:20:46 +00:00
export function adminInfo(adminToken){
let req = new Request(endPoints.adminToken + '/' + adminToken,{
method: 'GET',
credentials: 'same-origin'
})
console.log(req)
return fetch(req).catch((e)=>{console.error(e)})
}
2019-04-02 13:53:00 +00:00
export async function download(dlId){
let req = new Request(endPoints.download + '/' + dlId,{
method: 'GET',
credentials: 'same-origin'
})
return fetch(req).catch((e)=>{console.error(e)})
}
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'
})
return fetch(req).catch((e)=>{console.error(e)})
}
2019-04-07 20:31:35 +00:00
export function pollInvoice(invoiceId){
let req = new Request(endPoints.pollinvoice + '/' + invoiceId,{
method: 'GET',
credentials: 'same-origin'
})
return fetch(req).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
2019-04-07 20:31:35 +00:00
let req = new Request(endPoints.pollupstatus + '/' + uploadId,{
2019-04-02 13:53:00 +00:00
method: 'GET',
credentials: 'same-origin'
2019-04-02 13:53:00 +00:00
})
2019-04-07 11:54:41 +00:00
return fetch(req).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,
download: download,
2019-04-07 20:31:35 +00:00
pollInvoice: pollInvoice,
2019-04-09 07:20:46 +00:00
adminInfo: adminInfo
2019-03-23 17:01:49 +00:00
}