Skip to content

Commit

Permalink
Removed dependency on Axios
Browse files Browse the repository at this point in the history
And some cleanup
  • Loading branch information
rajch committed Jul 18, 2024
1 parent 435f7c6 commit 6197f9a
Show file tree
Hide file tree
Showing 5 changed files with 3,270 additions and 1,885 deletions.
63 changes: 46 additions & 17 deletions client/apiclient.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,61 @@
const axios = require('axios')
"use strict"

function apiclient (baseurl) {
baseurl = baseurl || ''

function get (url, opts, successHandler, errorHandler, progressHandler) {
opts = opts || {}
function request (url, method, opts, successHandler, errorHandler) {
let requesturl = baseurl + url

const requestopts = {
method
}

const config = {}
config.params = opts
if (typeof progressHandler === 'function') {
config.onDownloadProgress = progressHandler
if (opts) {
if (method === 'GET') {
// GET calls should pass options in query string parameters
requesturl = requesturl + '?' + new URLSearchParams(opts).toString()
} else {
// All other verbs carry a JSON payload in the request body
requestopts.headers = { 'Content-type': 'application/json' }
requestopts.body = JSON.stringify(opts)
}
}

axios.get(baseurl + url, config).then(function (response) { successHandler.call(axios, response) }).catch(
function (error) { errorHandler.call(axios, error) })
fetch(requesturl, requestopts)
.then(function (response) {
// Replicate axios behaviour of a non-ok response being an error
if (!response.ok) {
const errorvalue = new Error('Error status returned:' + response.status)
errorvalue.response = response
throw errorvalue
} else {
// Only .text() is okay with an empty response. Later, we will parse it
// into valid json.
// This is a promise, so gets taken care of in the next .then
return response.text()
}
})
.then(function (responseData) {
// Replicate axios behaviour of response body being sent back in
// data property
const responseBody = responseData ? { data: JSON.parse(responseData) } : {}
successHandler(responseBody)
})
.catch(function (error) {
errorHandler(error)
})
}

function post (url, opts, successHandler, errorHandler) {
opts = opts || {}
function get (url, opts, successHandler, errorHandler, progressHandler) {
request(url, 'GET', opts, successHandler, errorHandler)
}

axios.post(baseurl + url, opts).then(function (response) { successHandler.call(axios, response) }).catch(
function (error) { errorHandler.call(axios, error) })
function post (url, opts, successHandler, errorHandler) {
request(url, 'POST', opts, successHandler, errorHandler)
}

function deleteverb (url, opts, successHandler, errorHandler) {
opts = opts || {}

axios.delete(baseurl + url, opts).then(function (response) { successHandler.call(axios, response) }).catch(
function (error) { errorHandler.call(axios, error) })
request(url, 'DELETE', opts, successHandler, errorHandler)
}

this.listcontainers = function (opts, successHandler, errorHandler) {
Expand Down Expand Up @@ -87,4 +115,5 @@ function apiclient (baseurl) {
get('/images/json', opts, successHandler, errorHandler)
}
}

module.exports = apiclient
3 changes: 3 additions & 0 deletions client/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ const world = function (opts) {
function listContainers (successHandler, errorHandler) {
apiclient.listcontainers({},
function (success) {
if (!success.data) {
return
}
const returneddata = success.data.reverse()
for (let i = 0; i < returneddata.length; i++) {
cc.add(returneddata[i].Names[0].substring(1), returneddata[i])
Expand Down
Loading

0 comments on commit 6197f9a

Please sign in to comment.