import axios = require('axios') import { Environment } from '../core/environment' import { WebApiClient } from '../web-api/web-api-client' var uuid = require('uuid'); /** * @export * @class JudgeSession */ export class JudgeSession { public sessionId: string = null; private baseUrl: string; /** * Creates an instance of JudgeSession. * @param {string} sessionId * @param {string} [baseUrl] * * @memberOf JudgeSession */ constructor(sessionId: string, baseUrl?: string) { this.sessionId = sessionId; if (baseUrl) { this.baseUrl = baseUrl; } else { this.baseUrl = Judge.DefaultBaseURL; } } /** * @param {string} nextCase * @returns {Promise} * * @memberOf JudgeSession */ public setNextCase(nextCase: string): Promise { var nextCaseEndpointUrl = this.baseUrl + "/judge/nextCase" return axios.post(nextCaseEndpointUrl, null, { headers: { "x-judge-case": nextCase, "x-judge-session": this.sessionId } }); } } /** * @export * @class Judge */ export class Judge { public static JudgeSessionHeaderName: string = "x-judge-session"; public static JudgeCaseHeaderName: string = "x-judge-case"; public static DefaultBaseURL: string = "http://csas-judge.herokuapp.com"; public static DefaultTestEnvironment: Environment = new Environment(Judge.DefaultBaseURL + "/webapi", "") private baseUrl: string; /** * Creates an instance of Judge. * @param {string=} baseUrl * * @memberOf Judge */ constructor(baseUrl?: string) { if (baseUrl) { this.baseUrl = baseUrl; } else { this.baseUrl = Judge.DefaultBaseURL; } } /** * @readonly * @returns {Environment} * * @memberOf Judge */ get testEnvironment(): Environment { return new Environment(this.baseUrl + "/webapi", ""); } /** * @private * @static * @type {string} * @memberOf Judge */ private static CurrentSessionId: string = null; /** * @returns {JudgeSession} * * @memberOf Judge */ public startNewSession(): JudgeSession { Judge.CurrentSessionId = uuid.v4(); WebApiClient.GlobalRequestHeaders[Judge.JudgeSessionHeaderName] = Judge.CurrentSessionId; return new JudgeSession(Judge.CurrentSessionId, this.baseUrl); } }