mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-04 19:20:05 +00:00
fix: created sasjsJobExecutor class and overrided parseResponse for sasjsRequestClient
This commit is contained in:
10
src/SASjs.ts
10
src/SASjs.ts
@@ -24,6 +24,7 @@ import { SasjsRequestClient } from './request/SasjsRequestClient'
|
||||
import {
|
||||
JobExecutor,
|
||||
WebJobExecutor,
|
||||
SasjsJobExecutor,
|
||||
ComputeJobExecutor,
|
||||
JesJobExecutor,
|
||||
Sas9JobExecutor,
|
||||
@@ -59,6 +60,7 @@ export default class SASjs {
|
||||
private authManager: AuthManager | null = null
|
||||
private requestClient: RequestClient | null = null
|
||||
private webJobExecutor: JobExecutor | null = null
|
||||
private sasjsJobExecutor: JobExecutor | null = null
|
||||
private computeJobExecutor: JobExecutor | null = null
|
||||
private jesJobExecutor: JobExecutor | null = null
|
||||
private sas9JobExecutor: JobExecutor | null = null
|
||||
@@ -695,7 +697,7 @@ export default class SASjs {
|
||||
// status is true if the data passes validation checks above
|
||||
if (validationResult.status) {
|
||||
if (config.serverType === ServerType.Sasjs) {
|
||||
return await this.webJobExecutor!.execute(
|
||||
return await this.sasjsJobExecutor!.execute(
|
||||
sasJob,
|
||||
data,
|
||||
config,
|
||||
@@ -1053,6 +1055,12 @@ export default class SASjs {
|
||||
this.sasViyaApiClient!
|
||||
)
|
||||
|
||||
this.sasjsJobExecutor = new SasjsJobExecutor(
|
||||
this.sasjsConfig.serverUrl,
|
||||
this.jobsPath,
|
||||
this.requestClient
|
||||
)
|
||||
|
||||
this.sas9JobExecutor = new Sas9JobExecutor(
|
||||
this.sasjsConfig.serverUrl,
|
||||
this.sasjsConfig.serverType!,
|
||||
|
||||
@@ -81,29 +81,9 @@ export class FileUploader extends BaseJobExecutor {
|
||||
this.requestClient
|
||||
.post(uploadUrl, formData, undefined, 'application/json', headers)
|
||||
.then(async (res: any) => {
|
||||
const parsedSasjsLog =
|
||||
this.serverType === ServerType.Sasjs
|
||||
? res.log?.split(SASJS_LOGS_SEPARATOR)[1]
|
||||
: res.result.log
|
||||
this.requestClient.appendRequest(res, sasJob, config.debug)
|
||||
|
||||
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
|
||||
let jsonResponse = res.result
|
||||
|
||||
if (config.debug) {
|
||||
switch (this.serverType) {
|
||||
@@ -121,6 +101,11 @@ export class FileUploader extends BaseJobExecutor {
|
||||
: res.result
|
||||
break
|
||||
}
|
||||
} else if (this.serverType !== ServerType.Sasjs) {
|
||||
jsonResponse =
|
||||
typeof res.result === 'string'
|
||||
? getValidJson(res.result)
|
||||
: res.result
|
||||
}
|
||||
|
||||
resolve(jsonResponse)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { AuthConfig, ServerType } from '@sasjs/utils/types'
|
||||
import { ExtraResponseAttributes } from '@sasjs/utils/types'
|
||||
import { asyncForEach } from '../utils'
|
||||
import { asyncForEach, isRelativePath } from '../utils'
|
||||
|
||||
export type ExecuteFunction = () => Promise<any>
|
||||
|
||||
@@ -45,4 +45,17 @@ export abstract class BaseJobExecutor implements JobExecutor {
|
||||
protected appendWaitingRequest(request: ExecuteFunction) {
|
||||
this.waitingRequests.push(request)
|
||||
}
|
||||
|
||||
protected getRequestParams(config: any): any {
|
||||
const requestParams: any = {}
|
||||
|
||||
if (config.debug) {
|
||||
requestParams['_omittextlog'] = 'false'
|
||||
requestParams['_omitsessionresults'] = 'false'
|
||||
|
||||
requestParams['_debug'] = 131
|
||||
}
|
||||
|
||||
return requestParams
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ export class Sas9JobExecutor extends BaseJobExecutor {
|
||||
return requestPromise
|
||||
}
|
||||
|
||||
private getRequestParams(config: any): any {
|
||||
protected getRequestParams(config: any): any {
|
||||
const requestParams: any = {}
|
||||
|
||||
if (config.debug) {
|
||||
|
||||
141
src/job-execution/SasjsJobExecutor.ts
Normal file
141
src/job-execution/SasjsJobExecutor.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import * as NodeFormData from 'form-data'
|
||||
import {
|
||||
AuthConfig,
|
||||
ExtraResponseAttributes,
|
||||
ServerType
|
||||
} from '@sasjs/utils/types'
|
||||
import {
|
||||
ErrorResponse,
|
||||
JobExecutionError,
|
||||
LoginRequiredError
|
||||
} from '../types/errors'
|
||||
import { generateFileUploadForm } from '../file/generateFileUploadForm'
|
||||
|
||||
import { RequestClient } from '../request/RequestClient'
|
||||
|
||||
import { isRelativePath, appendExtraResponseAttributes } from '../utils'
|
||||
import { BaseJobExecutor } from './JobExecutor'
|
||||
|
||||
export class SasjsJobExecutor extends BaseJobExecutor {
|
||||
constructor(
|
||||
serverUrl: string,
|
||||
private jobsPath: string,
|
||||
private requestClient: RequestClient
|
||||
) {
|
||||
super(serverUrl, ServerType.Sasjs)
|
||||
}
|
||||
|
||||
async execute(
|
||||
sasJob: string,
|
||||
data: any,
|
||||
config: any,
|
||||
loginRequiredCallback?: any,
|
||||
authConfig?: AuthConfig,
|
||||
extraResponseAttributes: ExtraResponseAttributes[] = []
|
||||
) {
|
||||
const loginCallback = loginRequiredCallback
|
||||
const program =
|
||||
isRelativePath(sasJob) && config.appLoc
|
||||
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
|
||||
: sasJob
|
||||
|
||||
let apiUrl = `${config.serverUrl}${this.jobsPath}/?${'_program=' + program}`
|
||||
|
||||
let requestParams = {
|
||||
...this.getRequestParams(config)
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the available form data object (FormData in Browser, NodeFormData in
|
||||
* Node)
|
||||
*/
|
||||
let formData =
|
||||
typeof FormData === 'undefined' ? new NodeFormData() : new FormData()
|
||||
|
||||
if (data) {
|
||||
// file upload approach
|
||||
try {
|
||||
formData = generateFileUploadForm(formData, data)
|
||||
} catch (e: any) {
|
||||
return Promise.reject(new ErrorResponse(e?.message, e))
|
||||
}
|
||||
}
|
||||
|
||||
for (const key in requestParams) {
|
||||
if (requestParams.hasOwnProperty(key)) {
|
||||
formData.append(key, requestParams[key])
|
||||
}
|
||||
}
|
||||
|
||||
/* The NodeFormData object does not set the request header - so, set it */
|
||||
const contentType =
|
||||
formData instanceof NodeFormData && typeof FormData === 'undefined'
|
||||
? `multipart/form-data; boundary=${formData.getBoundary()}`
|
||||
: undefined
|
||||
|
||||
const requestPromise = new Promise((resolve, reject) => {
|
||||
this.requestClient!.post(
|
||||
apiUrl,
|
||||
formData,
|
||||
authConfig?.access_token,
|
||||
contentType
|
||||
)
|
||||
.then(async (res: any) => {
|
||||
if (Object.entries(res.result).length < 1) {
|
||||
throw new JobExecutionError(
|
||||
0,
|
||||
`No webout was returned by job ${program}. Please check the SAS log for more info.`,
|
||||
res.log
|
||||
)
|
||||
}
|
||||
|
||||
this.requestClient!.appendRequest(res, sasJob, config.debug)
|
||||
|
||||
const responseObject = appendExtraResponseAttributes(
|
||||
res,
|
||||
extraResponseAttributes
|
||||
)
|
||||
resolve(responseObject)
|
||||
})
|
||||
.catch(async (e: Error) => {
|
||||
if (e instanceof JobExecutionError) {
|
||||
this.requestClient!.appendRequest(e, sasJob, config.debug)
|
||||
reject(new ErrorResponse(e?.message, e))
|
||||
}
|
||||
|
||||
if (e instanceof LoginRequiredError) {
|
||||
if (!loginRequiredCallback) {
|
||||
reject(
|
||||
new ErrorResponse(
|
||||
'Request is not authenticated. Make sure .env file exists with valid credentials.',
|
||||
e
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
this.appendWaitingRequest(() => {
|
||||
return this.execute(
|
||||
sasJob,
|
||||
data,
|
||||
config,
|
||||
loginRequiredCallback,
|
||||
authConfig,
|
||||
extraResponseAttributes
|
||||
).then(
|
||||
(res: any) => {
|
||||
resolve(res)
|
||||
},
|
||||
(err: any) => {
|
||||
reject(err)
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
if (loginCallback) await loginCallback()
|
||||
} else reject(new ErrorResponse(e?.message, e))
|
||||
})
|
||||
})
|
||||
|
||||
return requestPromise
|
||||
}
|
||||
}
|
||||
@@ -16,13 +16,10 @@ import { SASViyaApiClient } from '../SASViyaApiClient'
|
||||
import {
|
||||
isRelativePath,
|
||||
parseSasViyaDebugResponse,
|
||||
appendExtraResponseAttributes,
|
||||
getValidJson,
|
||||
SASJS_LOGS_SEPARATOR
|
||||
appendExtraResponseAttributes
|
||||
} from '../utils'
|
||||
import { BaseJobExecutor } from './JobExecutor'
|
||||
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
||||
import { Server } from 'https'
|
||||
|
||||
export interface WaitingRequstPromise {
|
||||
promise: Promise<any> | null
|
||||
@@ -122,7 +119,6 @@ export class WebJobExecutor extends BaseJobExecutor {
|
||||
const stringifiedData = JSON.stringify(data)
|
||||
if (
|
||||
config.serverType === ServerType.Sas9 ||
|
||||
config.serverType === ServerType.Sasjs ||
|
||||
stringifiedData.length > 500000 ||
|
||||
stringifiedData.includes(';')
|
||||
) {
|
||||
@@ -165,37 +161,9 @@ export class WebJobExecutor extends BaseJobExecutor {
|
||||
contentType
|
||||
)
|
||||
.then(async (res: any) => {
|
||||
const parsedSasjsLog =
|
||||
this.serverType === ServerType.Sasjs
|
||||
? res.log?.split(SASJS_LOGS_SEPARATOR)[1]
|
||||
: res.result.log
|
||||
this.requestClient!.appendRequest(res, sasJob, config.debug)
|
||||
|
||||
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
|
||||
|
||||
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.`,
|
||||
parsedSasjsLog
|
||||
)
|
||||
}
|
||||
|
||||
this.requestClient!.appendRequest(resObj, sasJob, config.debug)
|
||||
|
||||
let jsonResponse = resObj.result
|
||||
let jsonResponse = res.result
|
||||
|
||||
if (config.debug) {
|
||||
switch (this.serverType) {
|
||||
@@ -216,7 +184,7 @@ export class WebJobExecutor extends BaseJobExecutor {
|
||||
}
|
||||
|
||||
const responseObject = appendExtraResponseAttributes(
|
||||
{ result: jsonResponse, log: parsedSasjsLog },
|
||||
{ result: jsonResponse, log: res.log },
|
||||
extraResponseAttributes
|
||||
)
|
||||
resolve(responseObject)
|
||||
@@ -294,39 +262,4 @@ export class WebJobExecutor extends BaseJobExecutor {
|
||||
}
|
||||
return uri
|
||||
}
|
||||
|
||||
private getRequestParams(config: any): any {
|
||||
const requestParams: any = {}
|
||||
|
||||
if (config.debug) {
|
||||
requestParams['_omittextlog'] = 'false'
|
||||
requestParams['_omitsessionresults'] = 'false'
|
||||
|
||||
requestParams['_debug'] = 131
|
||||
}
|
||||
|
||||
return requestParams
|
||||
}
|
||||
|
||||
private parseSAS9ErrorResponse(response: string) {
|
||||
const logLines = response.split('\n')
|
||||
const parsedLines: string[] = []
|
||||
let firstErrorLineIndex: number = -1
|
||||
|
||||
logLines.map((line: string, index: number) => {
|
||||
if (
|
||||
line.toLowerCase().includes('error') &&
|
||||
!line.toLowerCase().includes('this request completed with errors.') &&
|
||||
firstErrorLineIndex === -1
|
||||
) {
|
||||
firstErrorLineIndex = index
|
||||
}
|
||||
})
|
||||
|
||||
for (let i = firstErrorLineIndex - 10; i <= firstErrorLineIndex + 10; i++) {
|
||||
parsedLines.push(logLines[i])
|
||||
}
|
||||
|
||||
return parsedLines.join(', ')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,3 +4,4 @@ export * from './JesJobExecutor'
|
||||
export * from './JobExecutor'
|
||||
export * from './Sas9JobExecutor'
|
||||
export * from './WebJobExecutor'
|
||||
export * from './SasjsJobExecutor'
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { RequestClient } from './RequestClient'
|
||||
import { AxiosResponse } from 'axios'
|
||||
import { SASJS_LOGS_SEPARATOR, getValidJson } from '../utils'
|
||||
|
||||
/**
|
||||
* Specific request client for SASJS.
|
||||
* Append tokens in headers.
|
||||
*/
|
||||
|
||||
export class SasjsRequestClient extends RequestClient {
|
||||
getHeaders = (accessToken: string | undefined, contentType: string) => {
|
||||
const headers: any = {}
|
||||
@@ -20,4 +23,32 @@ export class SasjsRequestClient extends RequestClient {
|
||||
|
||||
return headers
|
||||
}
|
||||
|
||||
protected parseResponse<T>(response: AxiosResponse<any>) {
|
||||
const etag = response?.headers ? response.headers['etag'] : ''
|
||||
let parsedResponse
|
||||
let log
|
||||
|
||||
try {
|
||||
if (typeof response.data === 'string') {
|
||||
parsedResponse = JSON.parse(response.data)
|
||||
} else {
|
||||
parsedResponse = response.data
|
||||
}
|
||||
} catch {
|
||||
if (response.data.includes(SASJS_LOGS_SEPARATOR)) {
|
||||
parsedResponse = getValidJson(
|
||||
response.data.split(SASJS_LOGS_SEPARATOR)[0]
|
||||
)
|
||||
log = response.data.split(SASJS_LOGS_SEPARATOR)[1]
|
||||
} else parsedResponse = response.data
|
||||
}
|
||||
|
||||
return {
|
||||
result: parsedResponse as T,
|
||||
log,
|
||||
etag,
|
||||
status: response.status
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user