@isaac-platform/isaac-integration-sdk
Version:
A Typescript SDK for integrating with ISAAC
68 lines (67 loc) • 2.98 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import axios from "axios";
export class IsaacConnection {
constructor(newConnection) {
this.connectionDetails = null;
this.setConnectionDetails = (newConnection) => {
this.connectionDetails = newConnection;
};
this.postRequest = (endpoint, payload, params) => {
if (!this.connectionDetails) {
throw new Error('ISAAC SDK: No connection details provided.');
}
return axios.post(`http://${this.connectionDetails.ip}/api/v1/${endpoint}`, payload, {
params: params
}).then(response => response.data)
.catch(error => {
console.error('ISAAC SDK: POST ERROR', error);
throw error;
});
};
this.putRequest = (endpoint, payload) => __awaiter(this, void 0, void 0, function* () {
if (!this.connectionDetails) {
throw new Error('ISAAC SDK: No connection details provided.');
}
return (yield axios.put(`http://${this.connectionDetails.ip}/api/v1/${endpoint}`, payload)).data;
});
if (newConnection)
this.setConnectionDetails(newConnection);
}
get subsystemID() {
var _a;
return ((_a = this.connectionDetails) === null || _a === void 0 ? void 0 : _a.subsystemID) || '';
}
get isReady() {
return this.connectionDetails != null;
}
// TODO: Proper async handling
getRequest(endpoint, config) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.connectionDetails) {
throw new Error('ISAAC SDK: No connection details provided.');
}
try {
const response = yield axios.get(`http://${this.connectionDetails.ip}/api/v1/${endpoint}`, config);
return response.data;
}
catch (err) {
// @ts-ignore
if (err.response && err.response.status && err.response.status === 304) {
console.warn("ISAAC 304: No change.");
return;
}
console.error(err);
}
});
}
}
const isaacConnection = new IsaacConnection();
export default isaacConnection;