Skip to content

Commit

Permalink
Merge pull request #423 from uploadcare/feat/addons-api
Browse files Browse the repository at this point in the history
feat: addons api
  • Loading branch information
nd0ut authored Sep 13, 2022
2 parents 95ecfcc + 18a9c2b commit bc93268
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 17 deletions.
22 changes: 19 additions & 3 deletions packages/rest-client/src/api/addons/addonExecutionStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ import { addonExecutionStatus } from './addonExecutionStatus'

import { testSettings } from '../../../test/helpers'
import { AddonName } from '../../types/AddonName'
import { executeAddon } from './executeAddon'
import { ADDONS_UUID } from '../../../test/fixtures'
import { copyFileToLocalStorage } from '../file/copyFileToLocalStorage'

describe.skip('addonExecutionStatus', () => {
describe('addonExecutionStatus', () => {
it('should work', async () => {
const copy = await copyFileToLocalStorage(
{ source: ADDONS_UUID, store: false },
testSettings
)

const { requestId } = await executeAddon(
{
addonName: AddonName.AWS_REKOGNITION_DETECT_LABELS,
target: copy.result.uuid
},
testSettings
)

const response = await addonExecutionStatus(
{
addonName: AddonName.UC_CLAMAV_VIRUS_SCAN,
requestId: 'requestId'
addonName: AddonName.AWS_REKOGNITION_DETECT_LABELS,
requestId: requestId
},
testSettings
)
Expand Down
18 changes: 15 additions & 3 deletions packages/rest-client/src/api/addons/executeAddon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ import { executeAddon } from './executeAddon'

import { testSettings } from '../../../test/helpers'
import { AddonName } from '../../types/AddonName'
import { ADDONS_UUID } from '../../../test/fixtures'
import { copyFileToLocalStorage } from '../file/copyFileToLocalStorage'

describe.skip('executeAddon', () => {
describe('executeAddon', () => {
it('should work', async () => {
const copy = await copyFileToLocalStorage(
{ source: ADDONS_UUID, store: false },
testSettings
)

const response = await executeAddon(
{
addonName: AddonName.UC_CLAMAV_VIRUS_SCAN
addonName: AddonName.UC_CLAMAV_VIRUS_SCAN,
target: copy.result.uuid,
params: {
purge_infected: false
}
},
testSettings
)
Expand All @@ -19,7 +30,8 @@ describe.skip('executeAddon', () => {
await expect(
executeAddon(
{
addonName: 'invalid' as AddonName
addonName: 'invalid' as AddonName,
target: ADDONS_UUID
},
testSettings
)
Expand Down
17 changes: 12 additions & 5 deletions packages/rest-client/src/api/addons/executeAddon.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import { makeApiRequest, ApiRequestSettings } from '../../makeApiRequest'
import { AddonName } from '../../types/AddonName'
import { AddonParams } from '../../types/AddonParams'
import { handleApiRequest } from '../handleApiRequest'

export type ExecuteAddonOptions = {
addonName: AddonName
export type ExecuteAddonOptions<T extends AddonName> = {
addonName: T
target: string
params?: AddonParams[T]
}

export type ExecuteAddonResponse = {
requestId: string
}

export async function executeAddon(
options: ExecuteAddonOptions,
export async function executeAddon<T extends AddonName = AddonName>(
options: ExecuteAddonOptions<T>,
userSettings: ApiRequestSettings
): Promise<ExecuteAddonResponse> {
const apiRequest = await makeApiRequest(
{
method: 'POST',
path: `/addons/${options.addonName}/execute/`
path: `/addons/${options.addonName}/execute/`,
body: {
target: options.target,
params: options.params
}
},
userSettings
)
Expand Down
31 changes: 26 additions & 5 deletions packages/rest-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export { ApiRequestSettings } from './makeApiRequest'
export { UserSettings } from './settings'
export { AddonExecutionStatus } from './types/AddonExecutionStatus'
export { AddonName } from './types/AddonName'
export {
AddonParams,
AddonUcClamavVirusScanParams,
AddonAwsRekognitionDetectLabelsParams,
AddonRemoveBgParams
} from './types/AddonParams'
export {
AppData,
ClamavVirusScan,
Expand Down Expand Up @@ -34,7 +40,8 @@ export {
AudioInfo,
CustomUserAgent,
CustomUserAgentFn,
CustomUserAgentOptions
CustomUserAgentOptions,
GetUserAgentOptions
} from '@uploadcare/api-client-utils'

/**
Expand Down Expand Up @@ -178,19 +185,33 @@ export {
convertVideo,
ConvertVideoResponse,
ConvertVideoOptions
} from './api/addons/convertVideo'
} from './api/conversion/convertVideo'
export {
convertDocument,
ConvertDocumentResponse,
ConvertDocumentOptions
} from './api/addons/convertDocument'
} from './api/conversion/convertDocument'
export {
documentConversionJobStatus,
DocumentConversionJobStatusResponse,
DocumentConversionJobStatusOptions
} from './api/addons/documentConversionJobStatus'
} from './api/conversion/documentConversionJobStatus'
export {
videoConversionJobStatus,
VideoConversionJobStatusResponse,
VideoConversionJobStatusOptions
} from './api/addons/videoConversionJobStatus'
} from './api/conversion/videoConversionJobStatus'

/**
* Low-level Addons API
*/
export {
executeAddon,
ExecuteAddonResponse,
ExecuteAddonOptions
} from './api/addons/executeAddon'
export {
addonExecutionStatus,
AddonExecutionStatusResponse,
AddonExecutionStatusOptions
} from './api/addons/addonExecutionStatus'
3 changes: 2 additions & 1 deletion packages/rest-client/src/types/AddonName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum AddonName {
UC_CLAMAV_VIRUS_SCAN = 'uc_clamav_virus_scan',
AWS_REKOGNITION_DETECT_LABELS = 'aws_rekognition_detect_labels'
AWS_REKOGNITION_DETECT_LABELS = 'aws_rekognition_detect_labels',
REMOVE_BG = 'remove_bg'
}
26 changes: 26 additions & 0 deletions packages/rest-client/src/types/AddonParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { AddonName } from './AddonName'

export type AddonUcClamavVirusScanParams = {
purge_infected: boolean
}

export type AddonAwsRekognitionDetectLabelsParams = undefined

export type AddonRemoveBgParams = {
crop?: boolean
crop_margin?: string
scale?: string
add_shadow?: boolean
type_level?: 'none' | '1' | '2' | 'latest'
type?: 'auto' | 'person' | 'product' | 'car'
semitransparency?: boolean
channels?: 'rgba' | 'alpha'
roi?: string
position?: string
}

export type AddonParams = {
[AddonName.UC_CLAMAV_VIRUS_SCAN]: AddonUcClamavVirusScanParams
[AddonName.AWS_REKOGNITION_DETECT_LABELS]: AddonAwsRekognitionDetectLabelsParams
[AddonName.REMOVE_BG]: AddonRemoveBgParams
}
1 change: 1 addition & 0 deletions packages/rest-client/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const GROUP_FILE_UUID = '749aee13-a313-48bf-a252-a7eeaadc1b55'
export const INVALID_UUID = 'invalid-uuid-4f20-966f-caed5f978e15'
export const DOCUMENT_UUID = 'bf96bce4-0c49-491a-887d-9855b319c549'
export const VIDEO_UUID = '6992a455-5eb6-49a0-972a-36de53f51b5b'
export const ADDONS_UUID = 'd666789a-0067-4662-addf-c8452f6153bf'
1 change: 1 addition & 0 deletions packages/upload-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { default as uploadFileGroup } from './uploadFileGroup'
export { default as UploadClient } from './UploadClient'
export {
getUserAgent,
GetUserAgentOptions,
UploadcareNetworkError
} from '@uploadcare/api-client-utils'

Expand Down

0 comments on commit bc93268

Please sign in to comment.