From a1536ffe565e1641db8f50c269de96241a1e587d Mon Sep 17 00:00:00 2001 From: Thomas Hobson Date: Sat, 13 Mar 2021 16:08:35 +1300 Subject: [PATCH] cli: merge with api-client --- api-client/.gitignore | 1 - api-client/index.cjs | 70 -------------------------- api-client/package.json | 11 ---- cli/commands/execute.js | 17 +++++-- cli/commands/ppman_commands/install.js | 16 +++--- cli/commands/ppman_commands/list.js | 8 ++- cli/package.json | 2 +- cli/yarn.lock | 5 -- 8 files changed, 28 insertions(+), 102 deletions(-) delete mode 100644 api-client/.gitignore delete mode 100644 api-client/index.cjs delete mode 100644 api-client/package.json diff --git a/api-client/.gitignore b/api-client/.gitignore deleted file mode 100644 index b512c09..0000000 --- a/api-client/.gitignore +++ /dev/null @@ -1 +0,0 @@ -node_modules \ No newline at end of file diff --git a/api-client/index.cjs b/api-client/index.cjs deleted file mode 100644 index 14723dd..0000000 --- a/api-client/index.cjs +++ /dev/null @@ -1,70 +0,0 @@ -const fetch = require('node-fetch') - -function url_join(base, endpoint){ - return base + endpoint - //return new URL(endpoint, base).href; -} - -class APIWrapper { - #base; - constructor(base_url){ - this.#base = base_url.toString() - } - - async query(endpoint, options={}){ - const url = url_join(this.#base, endpoint); - 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); - } - - 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}) - } - - list_packages(){ - return this.get('/packages').then(x=>x.packages) - } - - install_package({language, version}){ - return this.post(`/packages/${language}/${version}`); - } - - uninstall_package({language, version}){ - return this.post(`/packages/${language}/${version}`); - } -} - - -module.exports = {PistonEngine} \ No newline at end of file diff --git a/api-client/package.json b/api-client/package.json deleted file mode 100644 index a0f144d..0000000 --- a/api-client/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "piston-api-client", - "version": "1.0.0", - "description": "Wraps API of Piston Engine API", - "main": "index.cjs", - "author": "Thomas Hobson ", - "license": "MIT", - "dependencies": { - "node-fetch": "^2.6.1" - } -} diff --git a/cli/commands/execute.js b/cli/commands/execute.js index 8d5ed69..f782207 100644 --- a/cli/commands/execute.js +++ b/cli/commands/execute.js @@ -1,4 +1,4 @@ -const {PistonEngine} = require('piston-api-client'); +const fetch = require('node-fetch'); const fs = require('fs'); const path = require('path'); const chalk = require('chalk'); @@ -34,8 +34,6 @@ exports.builder = { exports.handler = async function(argv){ - const api = new PistonEngine(argv['piston-url']); - const files = [...(argv.files || []),argv.file] .map(file_path => ({ name: path.basename(file_path), @@ -50,7 +48,7 @@ exports.handler = async function(argv){ })) || ""; - const response = await api.run_job({ + const request = { language: argv.language, version: argv['language-version'], files: files, @@ -59,7 +57,16 @@ exports.handler = async function(argv){ stdin, compile_timeout: argv.ct, run_timeout: argv.rt - }) + }; + + const response = await fetch(argv['piston-url'] + '/jobs', { + method: 'post', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify(request) + }) + .then(res=>res.json()) + .then(res=>{if(res.data)return res.data; throw new Error(res.message)}) + .catch(x=>x); function step(name, ctx){ console.log(chalk.bold(`== ${name} ==`)) diff --git a/cli/commands/ppman_commands/install.js b/cli/commands/ppman_commands/install.js index 39d0a28..792a9be 100644 --- a/cli/commands/ppman_commands/install.js +++ b/cli/commands/ppman_commands/install.js @@ -1,4 +1,4 @@ -const {PistonEngine} = require('piston-api-client'); +const fetch = require('node-fetch'); const chalk = require('chalk'); exports.command = ['install '] @@ -14,14 +14,14 @@ const msg_format = { } exports.handler = async function(argv){ - const api = new PistonEngine(argv['piston-url']); + const install = await fetch( + `${argv['piston-url']}/packages/${argv['language']}/${argv['language-version']}`, + {method: 'post'} + ) + .then(res=>res.json()) + .then(res=>{if(res.data)return res.data; throw new Error(res.message)}) + .catch(x=>x); - const opts = { - language: argv['language'], - version: argv['language-version'] - }; - - const install = await api.install_package(opts).catch(x=>x); console.log(msg_format.color(install)); } \ No newline at end of file diff --git a/cli/commands/ppman_commands/list.js b/cli/commands/ppman_commands/list.js index 4a44dcf..c3aa894 100644 --- a/cli/commands/ppman_commands/list.js +++ b/cli/commands/ppman_commands/list.js @@ -1,4 +1,4 @@ -const {PistonEngine} = require('piston-api-client'); +const fetch = require('node-fetch'); const chalk = require('chalk'); exports.command = ['list'] @@ -18,6 +18,12 @@ exports.handler = async function(argv){ const packages = await api.list_packages(); + const packages = await fetch(argv['piston-url'] + '/packages') + .then(res=>res.json()) + .then(res=>{if(res.data)return res.data; throw new Error(res.message)}); + .then(res=>res.packages) + .catch(x=>x) + const pkg_msg = packages .map(msg_format.color) diff --git a/cli/package.json b/cli/package.json index ca8f8f5..2075189 100644 --- a/cli/package.json +++ b/cli/package.json @@ -7,7 +7,7 @@ "license": "MIT", "dependencies": { "chalk": "^4.1.0", - "piston-api-client": "file:../api-client", + "node-fetch": "^2.6.1", "yargs": "^16.2.0" } } diff --git a/cli/yarn.lock b/cli/yarn.lock index d6958b1..c7d3bdb 100644 --- a/cli/yarn.lock +++ b/cli/yarn.lock @@ -73,11 +73,6 @@ node-fetch@^2.6.1: resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw== -"piston-api-client@file:../api-client": - version "1.0.0" - dependencies: - node-fetch "^2.6.1" - require-directory@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"