|
| 1 | +import axios, { AxiosResponse } from 'axios' |
| 2 | +import { |
| 3 | + DatasetExternalToolUrl, |
| 4 | + ExternalTool, |
| 5 | + FileExternalToolUrl, |
| 6 | + ToolScope, |
| 7 | + ToolType |
| 8 | +} from '../../../src' |
| 9 | +import { TestConstants } from '../TestConstants' |
| 10 | +import { ExternalToolPayload } from '../../../src/externalTools/infra/transformers/ExternalToolPayload' |
| 11 | + |
| 12 | +const DATAVERSE_API_REQUEST_HEADERS = { |
| 13 | + headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': process.env.TEST_API_KEY } |
| 14 | +} |
| 15 | + |
| 16 | +const CREATE_FILE_EXTERNAL_TOOL_PAYLOAD: ISetExternalToolViaApi = { |
| 17 | + id: 80, |
| 18 | + displayName: 'Text File Tool', |
| 19 | + toolName: 'textFileTool', |
| 20 | + description: 'Text File Tool', |
| 21 | + types: [ToolType.Preview], |
| 22 | + scope: ToolScope.File, |
| 23 | + toolUrl: 'http://example.org/text-tool', |
| 24 | + toolParameters: { |
| 25 | + queryParameters: [ |
| 26 | + { fileid: '{fileId}' }, |
| 27 | + { siteUrl: '{siteUrl}' }, |
| 28 | + { datasetid: '{datasetId}' }, |
| 29 | + { datasetversion: '{datasetVersion}' }, |
| 30 | + { locale: '{localeCode}' } |
| 31 | + ] |
| 32 | + }, |
| 33 | + contentType: 'text/plain', |
| 34 | + allowedApiCalls: [ |
| 35 | + { |
| 36 | + name: 'retrieveFileContents', |
| 37 | + httpMethod: 'GET', |
| 38 | + urlTemplate: '/api/v1/access/datafile/{fileId}', |
| 39 | + timeOut: 3600 |
| 40 | + } |
| 41 | + ] |
| 42 | +} |
| 43 | + |
| 44 | +export const createExternalToolsModel = (): ExternalTool[] => { |
| 45 | + return [ |
| 46 | + { |
| 47 | + id: 1, |
| 48 | + displayName: 'Test External Tool 1', |
| 49 | + description: 'Description for Test External Tool 1', |
| 50 | + scope: ToolScope.Dataset, |
| 51 | + types: [ToolType.Explore] |
| 52 | + }, |
| 53 | + { |
| 54 | + id: 2, |
| 55 | + displayName: 'Test External Tool 2', |
| 56 | + description: 'Description for Test External Tool 2', |
| 57 | + scope: ToolScope.File, |
| 58 | + types: [ToolType.Preview] |
| 59 | + } |
| 60 | + ] |
| 61 | +} |
| 62 | + |
| 63 | +export const createFileExternalToolUrlModel = (): FileExternalToolUrl => { |
| 64 | + return { |
| 65 | + toolUrlResolved: 'https://example.com/text-tool?fileId=123', |
| 66 | + displayName: 'Test File External Tool', |
| 67 | + fileId: 123, |
| 68 | + preview: true |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export const createDatasetExternalToolUrlModel = (): DatasetExternalToolUrl => { |
| 73 | + return { |
| 74 | + toolUrlResolved: 'https://example.com/dataset-tool?datasetId=456', |
| 75 | + displayName: 'Test Dataset External Tool', |
| 76 | + datasetId: 456, |
| 77 | + preview: false |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +interface ISetExternalToolViaApi { |
| 82 | + id: number |
| 83 | + displayName: string |
| 84 | + toolName: string |
| 85 | + description: string |
| 86 | + types: ToolType[] |
| 87 | + scope: ToolScope |
| 88 | + toolUrl: string |
| 89 | + toolParameters: { |
| 90 | + queryParameters: { [key: string]: string }[] |
| 91 | + } |
| 92 | + contentType: string |
| 93 | + allowedApiCalls: { |
| 94 | + name: string |
| 95 | + httpMethod: 'GET' | 'POST' | 'PUT' | 'DELETE' |
| 96 | + urlTemplate: string |
| 97 | + timeOut: number |
| 98 | + }[] |
| 99 | +} |
| 100 | + |
| 101 | +export async function setExternalToolViaApi( |
| 102 | + externalTool: ISetExternalToolViaApi = CREATE_FILE_EXTERNAL_TOOL_PAYLOAD |
| 103 | +): Promise<AxiosResponse<{ data: ExternalToolPayload }>> { |
| 104 | + try { |
| 105 | + return await axios.post( |
| 106 | + `${TestConstants.TEST_API_URL}/admin/externalTools`, |
| 107 | + externalTool, |
| 108 | + DATAVERSE_API_REQUEST_HEADERS |
| 109 | + ) |
| 110 | + } catch (error) { |
| 111 | + console.log(error) |
| 112 | + throw new Error('Error while setting external tool via API.') |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export async function deleteExternalToolViaApi(toolId: number): Promise<void> { |
| 117 | + try { |
| 118 | + await axios.delete( |
| 119 | + `${TestConstants.TEST_API_URL}/externalTools/${toolId}`, |
| 120 | + DATAVERSE_API_REQUEST_HEADERS |
| 121 | + ) |
| 122 | + } catch (error) { |
| 123 | + console.log(error) |
| 124 | + throw new Error('Error while deleting external tool via API.') |
| 125 | + } |
| 126 | +} |
0 commit comments