Skip to content

Instantly share code, notes, and snippets.

@markroxor
Created September 2, 2018 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markroxor/1866cdf49a2cf5e9c3fe5abd13364648 to your computer and use it in GitHub Desktop.
Save markroxor/1866cdf49a2cf5e9c3fe5abd13364648 to your computer and use it in GitHub Desktop.
"use strict";
var axios = require("axios");
var Client = (function () {
function Client(remote) {
this.remote = remote;
this.shouldLog = (process.env.SHOULD_LOG === "true");
if (this.shouldLog) {
console.log("Logging enabled");
}
else {
console.log("To enable logging, set SHOULD_LOG=true");
}
}
Client.prototype._parseServerErrors = function (promise) {
return promise.then(function (response) {
var status = response.status, message = response.data["message"];
if (status !== 200 && message !== undefined) {
throw new Error("ERROR: Got status code " + status + "\nMessage: " + message);
}
else {
return response.data;
}
});
};
Client.prototype._buildURL = function (route) {
return "" + this.remote + route;
};
Client.prototype._post = function (route, data) {
if (this.shouldLog) {
console.log("POST " + route + "\n" + JSON.stringify(data));
}
return this._parseServerErrors(axios.post(this._buildURL(route), data));
};
Client.prototype._get = function (route) {
if (this.shouldLog) {
console.log("GET " + route);
}
return this._parseServerErrors(axios.get(this._buildURL(route)));
};
Client.prototype.envCreate = function (envID) {
return this._post("/v1/envs/", { env_id: envID }).then(function (value) { return value; });
};
Client.prototype.envListAll = function () {
return this._get("/v1/envs/")
.then(function (value) { return value; });
};
Client.prototype.envReset = function (instanceID) {
var route = "/v1/envs/" + instanceID + "/reset/";
return this._post(route, null)
.then(function (value) { return value.observation; });
};
Client.prototype.envStep = function (instanceID, action, render) {
if (render === void 0) { render = false; }
var route = "/v1/envs/" + instanceID + "/step/";
var data = { action: action, render: render };
return this._post(route, data)
.then(function (value) { return value; });
};
Client.prototype.envActionSpaceInfo = function (instanceID) {
var route = "/v1/envs/" + instanceID + "/action_space/";
return this._get(route)
.then(function (reply) { return reply; });
};
Client.prototype.envObservationSpaceInfo = function (instanceID) {
var route = "/v1/envs/" + instanceID + "/observation_space/";
return this._get(route)
.then(function (reply) { return reply; });
};
Client.prototype.envMonitorStart = function (instanceID, directory, force, resume) {
if (force === void 0) { force = false; }
if (resume === void 0) { resume = false; }
var route = "/v1/envs/" + instanceID + "/monitor/start/";
return this._post(route, { directory: directory, force: force, resume: resume })
.then(function (reply) { return; });
};
Client.prototype.envMonitorClose = function (instanceID) {
var route = "/v1/envs/" + instanceID + "/monitor/close/";
return this._post(route, null)
.then(function (reply) { return; });
};
Client.prototype.envClose = function (instanceID) {
var route = "/v1/envs/" + instanceID + "/close/";
return this._post(route, null)
.then(function (reply) { return; });
};
Client.prototype.upload = function (trainingDir, algorithmID, apiKey) {
if (algorithmID === void 0) { algorithmID = undefined; }
if (apiKey === void 0) { apiKey = undefined; }
if (apiKey === undefined) {
apiKey = process.env["OPENAI_GYM_API_KEY"];
}
this._post("/v1/upload/", {
training_dir: trainingDir,
algorithm_id: algorithmID,
api_key: apiKey
});
};
Client.prototype.shutdownServer = function (self) {
this._post("/v1/shutdown/", null);
};
return Client;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Client;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment