mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-08 21:10:05 +00:00
Merge branches 'issue-258' and 'master' of github.com:sasjs/adapter
This commit is contained in:
@@ -21,6 +21,7 @@ import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
|
|||||||
import { RequestClient } from './request/RequestClient'
|
import { RequestClient } from './request/RequestClient'
|
||||||
import { NotFoundError } from './types/NotFoundError'
|
import { NotFoundError } from './types/NotFoundError'
|
||||||
import { SasAuthResponse } from '@sasjs/utils/types'
|
import { SasAuthResponse } from '@sasjs/utils/types'
|
||||||
|
import { prefixMessage } from '@sasjs/utils/error'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A client for interfacing with the SAS Viya REST API.
|
* A client for interfacing with the SAS Viya REST API.
|
||||||
@@ -280,25 +281,24 @@ export class SASViyaApiClient {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
|
|
||||||
if (accessToken) {
|
if (accessToken) headers.Authorization = `Bearer ${accessToken}`
|
||||||
headers.Authorization = `Bearer ${accessToken}`
|
|
||||||
}
|
|
||||||
|
|
||||||
let executionSessionId: string
|
let executionSessionId: string
|
||||||
|
|
||||||
const session = await this.sessionManager
|
const session = await this.sessionManager
|
||||||
.getSession(accessToken)
|
.getSession(accessToken)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while getting session. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
executionSessionId = session!.id
|
executionSessionId = session!.id
|
||||||
|
|
||||||
if (printPid) {
|
if (printPid) {
|
||||||
const { result: jobIdVariable } = await this.sessionManager.getVariable(
|
const { result: jobIdVariable } = await this.sessionManager
|
||||||
executionSessionId,
|
.getVariable(executionSessionId, 'SYSJOBID', accessToken)
|
||||||
'SYSJOBID',
|
.catch((err) => {
|
||||||
accessToken
|
throw prefixMessage(err, 'Error while getting session variable. ')
|
||||||
)
|
})
|
||||||
|
|
||||||
if (jobIdVariable && jobIdVariable.value) {
|
if (jobIdVariable && jobIdVariable.value) {
|
||||||
const relativeJobPath = this.rootFolderName
|
const relativeJobPath = this.rootFolderName
|
||||||
@@ -331,6 +331,7 @@ export class SASViyaApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let fileName
|
let fileName
|
||||||
|
|
||||||
if (isRelativePath(jobPath)) {
|
if (isRelativePath(jobPath)) {
|
||||||
fileName = `exec-${
|
fileName = `exec-${
|
||||||
jobPath.includes('/') ? jobPath.split('/')[1] : jobPath
|
jobPath.includes('/') ? jobPath.split('/')[1] : jobPath
|
||||||
@@ -352,7 +353,7 @@ export class SASViyaApiClient {
|
|||||||
if (data) {
|
if (data) {
|
||||||
if (JSON.stringify(data).includes(';')) {
|
if (JSON.stringify(data).includes(';')) {
|
||||||
files = await this.uploadTables(data, accessToken).catch((err) => {
|
files = await this.uploadTables(data, accessToken).catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while uploading tables. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
jobVariables['_webin_file_count'] = files.length
|
jobVariables['_webin_file_count'] = files.length
|
||||||
@@ -376,19 +377,18 @@ export class SASViyaApiClient {
|
|||||||
variables: jobVariables,
|
variables: jobVariables,
|
||||||
arguments: jobArguments
|
arguments: jobArguments
|
||||||
}
|
}
|
||||||
|
|
||||||
const { result: postedJob, etag } = await this.requestClient
|
const { result: postedJob, etag } = await this.requestClient
|
||||||
.post<Job>(
|
.post<Job>(
|
||||||
`/compute/sessions/${executionSessionId}/jobs`,
|
`/compute/sessions/${executionSessionId}/jobs`,
|
||||||
jobRequestBody,
|
jobRequestBody,
|
||||||
accessToken
|
accessToken
|
||||||
)
|
)
|
||||||
.catch((err: any) => {
|
.catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while posting job. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!waitForResult) {
|
if (!waitForResult) return session
|
||||||
return session
|
|
||||||
}
|
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
console.log(`Job has been submitted for '${fileName}'.`)
|
console.log(`Job has been submitted for '${fileName}'.`)
|
||||||
@@ -404,7 +404,9 @@ export class SASViyaApiClient {
|
|||||||
etag,
|
etag,
|
||||||
accessToken,
|
accessToken,
|
||||||
pollOptions
|
pollOptions
|
||||||
)
|
).catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while polling job status. ')
|
||||||
|
})
|
||||||
|
|
||||||
const { result: currentJob } = await this.requestClient
|
const { result: currentJob } = await this.requestClient
|
||||||
.get<Job>(
|
.get<Job>(
|
||||||
@@ -412,7 +414,7 @@ export class SASViyaApiClient {
|
|||||||
accessToken
|
accessToken
|
||||||
)
|
)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while getting job. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
let jobResult
|
let jobResult
|
||||||
@@ -427,7 +429,7 @@ export class SASViyaApiClient {
|
|||||||
res.result.items.map((i: any) => i.line).join('\n')
|
res.result.items.map((i: any) => i.line).join('\n')
|
||||||
)
|
)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while getting log. ')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -455,7 +457,7 @@ export class SASViyaApiClient {
|
|||||||
res.result.items.map((i: any) => i.line).join('\n')
|
res.result.items.map((i: any) => i.line).join('\n')
|
||||||
)
|
)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while getting log. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
return Promise.reject({
|
return Promise.reject({
|
||||||
@@ -464,6 +466,7 @@ export class SASViyaApiClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
result: JSON.stringify(e)
|
result: JSON.stringify(e)
|
||||||
}
|
}
|
||||||
@@ -473,7 +476,7 @@ export class SASViyaApiClient {
|
|||||||
await this.sessionManager
|
await this.sessionManager
|
||||||
.clearSession(executionSessionId, accessToken)
|
.clearSession(executionSessionId, accessToken)
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while clearing session. ')
|
||||||
})
|
})
|
||||||
|
|
||||||
return { result: jobResult?.result, log }
|
return { result: jobResult?.result, log }
|
||||||
@@ -490,7 +493,7 @@ export class SASViyaApiClient {
|
|||||||
true
|
true
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
throw e
|
throw prefixMessage(e, 'Error while executing script. ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -814,9 +817,12 @@ export class SASViyaApiClient {
|
|||||||
? `${this.rootFolderName}/${folderPath}`
|
? `${this.rootFolderName}/${folderPath}`
|
||||||
: folderPath
|
: folderPath
|
||||||
|
|
||||||
await this.populateFolderMap(fullFolderPath, accessToken)
|
await this.populateFolderMap(fullFolderPath, accessToken).catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while populating folder map. ')
|
||||||
|
})
|
||||||
|
|
||||||
const jobFolder = this.folderMap.get(fullFolderPath)
|
const jobFolder = this.folderMap.get(fullFolderPath)
|
||||||
|
|
||||||
if (!jobFolder) {
|
if (!jobFolder) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`The folder '${fullFolderPath}' was not found on '${this.serverUrl}'`
|
`The folder '${fullFolderPath}' was not found on '${this.serverUrl}'`
|
||||||
@@ -824,6 +830,7 @@ export class SASViyaApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const headers: any = { 'Content-Type': 'application/json' }
|
const headers: any = { 'Content-Type': 'application/json' }
|
||||||
|
|
||||||
if (!!accessToken) {
|
if (!!accessToken) {
|
||||||
headers.Authorization = `Bearer ${accessToken}`
|
headers.Authorization = `Bearer ${accessToken}`
|
||||||
}
|
}
|
||||||
@@ -847,10 +854,14 @@ export class SASViyaApiClient {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
result: jobDefinition
|
result: jobDefinition
|
||||||
} = await this.requestClient.get<JobDefinition>(
|
} = await this.requestClient
|
||||||
`${this.serverUrl}${jobDefinitionLink.href}`,
|
.get<JobDefinition>(
|
||||||
accessToken
|
`${this.serverUrl}${jobDefinitionLink.href}`,
|
||||||
)
|
accessToken
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while getting job definition. ')
|
||||||
|
})
|
||||||
|
|
||||||
code = jobDefinition.code
|
code = jobDefinition.code
|
||||||
|
|
||||||
@@ -861,6 +872,7 @@ export class SASViyaApiClient {
|
|||||||
if (!code) code = ''
|
if (!code) code = ''
|
||||||
|
|
||||||
const linesToExecute = code.replace(/\r\n/g, '\n').split('\n')
|
const linesToExecute = code.replace(/\r\n/g, '\n').split('\n')
|
||||||
|
|
||||||
return await this.executeScript(
|
return await this.executeScript(
|
||||||
sasJob,
|
sasJob,
|
||||||
linesToExecute,
|
linesToExecute,
|
||||||
@@ -872,7 +884,9 @@ export class SASViyaApiClient {
|
|||||||
waitForResult,
|
waitForResult,
|
||||||
pollOptions,
|
pollOptions,
|
||||||
printPid
|
printPid
|
||||||
)
|
).catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while executing script. ')
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1007,19 +1021,27 @@ export class SASViyaApiClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const url = '/folders/folders/@item?path=' + path
|
const url = '/folders/folders/@item?path=' + path
|
||||||
const { result: folder } = await this.requestClient.get<Folder>(
|
const { result: folder } = await this.requestClient
|
||||||
`${url}`,
|
.get<Folder>(`${url}`, accessToken)
|
||||||
accessToken
|
.catch((err) => {
|
||||||
)
|
throw prefixMessage(err, 'Error while getting folder. ')
|
||||||
|
})
|
||||||
|
|
||||||
if (!folder) {
|
if (!folder) {
|
||||||
throw new Error(`The path ${path} does not exist on ${this.serverUrl}`)
|
throw new Error(`The path ${path} does not exist on ${this.serverUrl}`)
|
||||||
}
|
}
|
||||||
const { result: members } = await this.requestClient.get<{ items: any[] }>(
|
|
||||||
`/folders/folders/${folder.id}/members?limit=${folder.memberCount}`,
|
const { result: members } = await this.requestClient
|
||||||
accessToken
|
.get<{ items: any[] }>(
|
||||||
)
|
`/folders/folders/${folder.id}/members?limit=${folder.memberCount}`,
|
||||||
|
accessToken
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while getting members. ')
|
||||||
|
})
|
||||||
|
|
||||||
const itemsAtRoot = members.items
|
const itemsAtRoot = members.items
|
||||||
|
|
||||||
this.folderMap.set(path, itemsAtRoot)
|
this.folderMap.set(path, itemsAtRoot)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1052,11 +1074,15 @@ export class SASViyaApiClient {
|
|||||||
Promise.reject(`Job state link was not found.`)
|
Promise.reject(`Job state link was not found.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const { result: state } = await this.requestClient.get<string>(
|
const { result: state } = await this.requestClient
|
||||||
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
|
.get<string>(
|
||||||
accessToken,
|
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
|
||||||
'text/plain'
|
accessToken,
|
||||||
)
|
'text/plain'
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while getting job state. ')
|
||||||
|
})
|
||||||
|
|
||||||
const currentState = state.trim()
|
const currentState = state.trim()
|
||||||
if (currentState === 'completed') {
|
if (currentState === 'completed') {
|
||||||
@@ -1073,11 +1099,15 @@ export class SASViyaApiClient {
|
|||||||
postedJobState === 'pending'
|
postedJobState === 'pending'
|
||||||
) {
|
) {
|
||||||
if (stateLink) {
|
if (stateLink) {
|
||||||
const { result: jobState } = await this.requestClient.get<string>(
|
const { result: jobState } = await this.requestClient
|
||||||
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
|
.get<string>(
|
||||||
accessToken,
|
`${this.serverUrl}${stateLink.href}?_action=wait&wait=30`,
|
||||||
'text/plain'
|
accessToken,
|
||||||
)
|
'text/plain'
|
||||||
|
)
|
||||||
|
.catch((err) => {
|
||||||
|
throw prefixMessage(err, 'Error while getting job state. ')
|
||||||
|
})
|
||||||
|
|
||||||
postedJobState = jobState.trim()
|
postedJobState = jobState.trim()
|
||||||
|
|
||||||
@@ -1119,11 +1149,11 @@ export class SASViyaApiClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const uploadResponse = await this.requestClient.uploadFile(
|
const uploadResponse = await this.requestClient
|
||||||
`${this.serverUrl}/files/files#rawUpload`,
|
.uploadFile(`${this.serverUrl}/files/files#rawUpload`, csv, accessToken)
|
||||||
csv,
|
.catch((err) => {
|
||||||
accessToken
|
throw prefixMessage(err, 'Error while uploading file. ')
|
||||||
)
|
})
|
||||||
|
|
||||||
uploadedFiles.push({ tableName, file: uploadResponse.result })
|
uploadedFiles.push({ tableName, file: uploadResponse.result })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ export class SessionManager {
|
|||||||
this.sessions = this.sessions.filter((s) => s.id !== id)
|
this.sessions = this.sessions.filter((s) => s.id !== id)
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
throw err
|
throw prefixMessage(err, 'Error while deleting session. ')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user