2021-02-22 11:37:54 +01:00
|
|
|
const fetch = require('node-fetch')
|
|
|
|
|
2021-02-27 07:29:15 +01:00
|
|
|
function url_join(base, endpoint){
|
|
|
|
return base + endpoint
|
|
|
|
//return new URL(endpoint, base).href;
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:37:54 +01:00
|
|
|
class APIWrapper {
|
|
|
|
#base;
|
|
|
|
constructor(base_url){
|
|
|
|
this.#base = base_url.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
async query(endpoint, options={}){
|
2021-02-27 07:29:15 +01:00
|
|
|
const url = url_join(this.#base, endpoint);
|
2021-02-22 11:37:54 +01:00
|
|
|
return await fetch(url, options)
|
|
|
|
.then(res=>res.json())
|
|
|
|
.then(res=>{if(res.data)return res.data; throw new Error(res.message)});
|
|
|
|
}
|
|
|
|
|
|
|
|
get(endpoint){
|
|
|
|
return this.query(endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
post(endpoint, body={}){
|
|
|
|
return this.query(endpoint, {
|
|
|
|
method: 'post',
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: JSON.stringify(body)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(endpoint, body={}){
|
|
|
|
return this.query(endpoint, {
|
|
|
|
method: 'delete',
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: JSON.stringify(body)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
get url_base(){
|
|
|
|
return this.#base
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class PistonEngine extends APIWrapper {
|
|
|
|
constructor(base_url = 'http://127.0.0.1:6969'){
|
|
|
|
super(base_url);
|
|
|
|
}
|
|
|
|
|
2021-03-06 07:26:51 +01:00
|
|
|
run_job({language, version, files, main, args, stdin, compile_timeout, run_timeout}){
|
|
|
|
return this.post(`/jobs`, {language, version, files, main, args, stdin, compile_timeout, run_timeout})
|
2021-02-22 11:37:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 07:26:51 +01:00
|
|
|
list_packages(){
|
|
|
|
return this.get('/packages').then(x=>x.packages)
|
2021-02-22 11:37:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 07:26:51 +01:00
|
|
|
install_package({language, version}){
|
|
|
|
return this.post(`/packages/${language}/${version}`);
|
2021-02-22 11:37:54 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 07:26:51 +01:00
|
|
|
uninstall_package({language, version}){
|
|
|
|
return this.post(`/packages/${language}/${version}`);
|
2021-02-22 11:37:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {PistonEngine}
|