From 4a319f1aef00fcb713f4b6afc98aaaf089fde25d Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Fri, 19 Aug 2022 16:10:05 +0500 Subject: [PATCH] fix: handled updated sasjs response --- src/SASjs.ts | 8 +++-- src/SASjsApiClient.ts | 17 ++++++----- src/job-execution/FileUploader.ts | 45 +++++++++++++++-------------- src/job-execution/WebJobExecutor.ts | 43 ++++++++++++--------------- src/request/RequestClient.ts | 20 ------------- src/utils/constants.ts | 2 ++ src/utils/index.ts | 1 + 7 files changed, 61 insertions(+), 75 deletions(-) create mode 100644 src/utils/constants.ts diff --git a/src/SASjs.ts b/src/SASjs.ts index 8fd5cde..ee3cbc0 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -102,10 +102,14 @@ export default class SASjs { * @param code - a string of code from the file to run. * @param authConfig - (optional) a valid client, secret, refresh and access tokens that are authorised to execute scripts. */ - public async executeScriptSASjs(code: string, authConfig?: AuthConfig) { + public async executeScriptSASjs( + code: string, + runTime?: string, + authConfig?: AuthConfig + ) { this.isMethodSupported('executeScriptSASJS', [ServerType.Sasjs]) - return await this.sasJSApiClient?.executeScript(code, authConfig) + return await this.sasJSApiClient?.executeScript(code, runTime, authConfig) } /** diff --git a/src/SASjsApiClient.ts b/src/SASjsApiClient.ts index e0d8955..8928a59 100644 --- a/src/SASjsApiClient.ts +++ b/src/SASjsApiClient.ts @@ -3,7 +3,7 @@ import { ExecutionQuery } from './types' import { RequestClient } from './request/RequestClient' import { getAccessTokenForSasjs } from './auth/getAccessTokenForSasjs' import { refreshTokensForSasjs } from './auth/refreshTokensForSasjs' -import { parseWeboutResponse } from './utils' +import { parseWeboutResponse, SASJS_LOGS_SEPARATOR } from './utils' import { getTokens } from './auth/getTokens' export class SASjsApiClient { @@ -64,9 +64,14 @@ export class SASjsApiClient { /** * Executes code on a SASJS server. * @param code - a string of code to execute. + * @param runTime - a string to representing runTime for code execution * @param authConfig - an object for authentication. */ - public async executeScript(code: string, authConfig?: AuthConfig) { + public async executeScript( + code: string, + runTime: string = 'sas', + authConfig?: AuthConfig + ) { let access_token = (authConfig || {}).access_token if (authConfig) { ;({ access_token } = await getTokens( @@ -79,12 +84,10 @@ export class SASjsApiClient { let parsedSasjsServerLog = '' await this.requestClient - .post('SASjsApi/code/execute', { code }, access_token) + .post('SASjsApi/code/execute', { code, runTime }, access_token) .then((res: any) => { - if (res.result?.log) { - parsedSasjsServerLog = res.result.log - .map((logLine: any) => logLine.line) - .join('\n') + if (res.log) { + parsedSasjsServerLog = res.log.split(SASJS_LOGS_SEPARATOR)[1] } }) .catch((err) => { diff --git a/src/job-execution/FileUploader.ts b/src/job-execution/FileUploader.ts index 13aa107..2072e22 100644 --- a/src/job-execution/FileUploader.ts +++ b/src/job-execution/FileUploader.ts @@ -1,7 +1,8 @@ import { getValidJson, parseSasViyaDebugResponse, - parseWeboutResponse + parseWeboutResponse, + SASJS_LOGS_SEPARATOR } from '../utils' import { UploadFile } from '../types/UploadFile' import { @@ -80,9 +81,29 @@ export class FileUploader extends BaseJobExecutor { this.requestClient .post(uploadUrl, formData, undefined, 'application/json', headers) .then(async (res: any) => { - this.requestClient.appendRequest(res, sasJob, config.debug) + const parsedSasjsLog = + this.serverType === ServerType.Sasjs + ? res.log?.split(SASJS_LOGS_SEPARATOR)[1] + : res.result.log - let jsonResponse = res.result + const parsedSasjsServerWebout = + this.serverType === ServerType.Sasjs + ? typeof res.result === 'string' + ? getValidJson(res.log?.split(SASJS_LOGS_SEPARATOR)[0]) + : res.result + : undefined + + const resObj = + this.serverType === ServerType.Sasjs + ? { + result: parsedSasjsServerWebout, + log: parsedSasjsLog + } + : res + + this.requestClient.appendRequest(resObj, sasJob, config.debug) + + let jsonResponse = resObj.result if (config.debug) { switch (this.serverType) { @@ -99,25 +120,7 @@ export class FileUploader extends BaseJobExecutor { ? parseWeboutResponse(res.result, uploadUrl) : res.result break - case ServerType.Sasjs: - if (typeof res.result._webout === 'object') { - jsonResponse = res.result._webout - } else { - const webout = parseWeboutResponse( - res.result._webout, - uploadUrl - ) - jsonResponse = getValidJson(webout) - } - break } - } else if (this.serverType === ServerType.Sasjs) { - jsonResponse = getValidJson(res.result._webout) - } else { - jsonResponse = - typeof res.result === 'string' - ? getValidJson(res.result) - : res.result } resolve(jsonResponse) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index a813794..8e425cd 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -17,7 +17,8 @@ import { isRelativePath, parseSasViyaDebugResponse, appendExtraResponseAttributes, - getValidJson + getValidJson, + SASJS_LOGS_SEPARATOR } from '../utils' import { BaseJobExecutor } from './JobExecutor' import { parseWeboutResponse } from '../utils/parseWeboutResponse' @@ -164,33 +165,37 @@ export class WebJobExecutor extends BaseJobExecutor { contentType ) .then(async (res: any) => { - const parsedSasjsServerLog = + const parsedSasjsLog = this.serverType === ServerType.Sasjs - ? res.result.log.map((logLine: any) => logLine.line).join('\n') + ? res.log?.split(SASJS_LOGS_SEPARATOR)[1] : res.result.log + const parsedSasjsServerWebout = + this.serverType === ServerType.Sasjs + ? typeof res.result === 'string' + ? getValidJson(res.log?.split(SASJS_LOGS_SEPARATOR)[0]) + : res.result + : undefined + const resObj = this.serverType === ServerType.Sasjs ? { - result: res.result._webout, - log: parsedSasjsServerLog + result: parsedSasjsServerWebout, + log: parsedSasjsLog } : res - if ( - this.serverType === ServerType.Sasjs && - res.result._webout.length < 1 - ) { + if (this.serverType === ServerType.Sasjs && res.result.length < 1) { throw new JobExecutionError( 0, `No webout was returned by job ${program}. Server type is SASJS and the calling function is WebJobExecutor. Please check the SAS log for more info.`, - parsedSasjsServerLog + parsedSasjsLog ) } this.requestClient!.appendRequest(resObj, sasJob, config.debug) - let jsonResponse = res.result + let jsonResponse = resObj.result if (config.debug) { switch (this.serverType) { @@ -207,21 +212,11 @@ export class WebJobExecutor extends BaseJobExecutor { ? parseWeboutResponse(res.result, apiUrl) : res.result break - case ServerType.Sasjs: - if (typeof res.result._webout === 'object') { - jsonResponse = res.result._webout - } else { - const webout = parseWeboutResponse(res.result._webout, apiUrl) - jsonResponse = getValidJson(webout) - } - break } - } else if (this.serverType === ServerType.Sasjs) { - jsonResponse = getValidJson(res.result._webout) } const responseObject = appendExtraResponseAttributes( - { result: jsonResponse, log: parsedSasjsServerLog }, + { result: jsonResponse, log: parsedSasjsLog }, extraResponseAttributes ) resolve(responseObject) @@ -261,9 +256,7 @@ export class WebJobExecutor extends BaseJobExecutor { }) if (loginCallback) await loginCallback() - } else { - reject(new ErrorResponse(e?.message, e)) - } + } else reject(new ErrorResponse(e?.message, e)) }) }) diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index 3ab2b83..ea56eb9 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -133,26 +133,6 @@ export class RequestClient implements HttpClient { } else { sasWork = response.log } - } else if (response?.result?.log) { - //In this scenario we know we got the response from SASJS server - //Log is array of `{ line: '' }` so we need to convert it back to text - //To be able to parse it with current functions. - let log: string = '' - - if (typeof log !== 'string') { - log = response.result.log - .map((logLine: any) => logLine.line) - .join('\n') - } - - sourceCode = parseSourceCode(log) - generatedCode = parseGeneratedCode(log) - - if (response?.result?._webout) { - sasWork = response.result._webout.WORK - } else { - sasWork = log - } } else if (response?.result) { sourceCode = parseSourceCode(response.result) generatedCode = parseGeneratedCode(response.result) diff --git a/src/utils/constants.ts b/src/utils/constants.ts new file mode 100644 index 0000000..943e05a --- /dev/null +++ b/src/utils/constants.ts @@ -0,0 +1,2 @@ +export const SASJS_LOGS_SEPARATOR = + 'SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784' diff --git a/src/utils/index.ts b/src/utils/index.ts index 62ceb97..4f02fc8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,6 +2,7 @@ export * from './appendExtraResponseAttributes' export * from './asyncForEach' export * from './compareTimestamps' export * from './convertToCsv' +export * from './constants' export * from './createAxiosInstance' export * from './delay' export * from './fetchLogByChunks'