From 4d14d028d0ea424eec842e0115bd093e03c6c09d Mon Sep 17 00:00:00 2001 From: Boris Bera Date: Tue, 20 Feb 2024 05:46:06 -0500 Subject: [PATCH] feat(pushapi): add method to create a file container (#797) * feat(pushapi): add method to create a file container * feat(pushapi): handle `useVirtualHostedStyleUrl` when creating a file container --- src/resources/PushApi/PushApi.ts | 18 +++++++---- src/resources/PushApi/PushApiInterfaces.ts | 15 +++++++++ src/resources/PushApi/tests/PushApi.spec.ts | 35 ++++++++++++++++++++- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/resources/PushApi/PushApi.ts b/src/resources/PushApi/PushApi.ts index 778750731..d63dee6ac 100644 --- a/src/resources/PushApi/PushApi.ts +++ b/src/resources/PushApi/PushApi.ts @@ -1,6 +1,8 @@ import API from '../../APICore.js'; import Resource from '../Resource.js'; import { + FileContainer, + FileContainerOptions, SecurityIdentityAliasModel, SecurityIdentityBatchConfig, SecurityIdentityDelete, @@ -10,7 +12,11 @@ import { } from './PushApiInterfaces.js'; export default class PushApi extends Resource { - static baseUrl = `/push/v1/organizations/${API.orgPlaceholder}/providers`; + static baseUrl = `/push/v1/organizations/${API.orgPlaceholder}`; + + createFileContainer(options?: FileContainerOptions) { + return this.serverlessApi.post(this.buildPath(`${PushApi.baseUrl}/files`, options)); + } createOrUpdateSecurityIdentityAlias( securityProviderId: string, @@ -18,7 +24,7 @@ export default class PushApi extends Resource { options?: SecurityIdentityOptions, ) { return this.serverlessApi.put( - this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/mappings`, options), + this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/mappings`, options), alias, ); } @@ -29,7 +35,7 @@ export default class PushApi extends Resource { options?: SecurityIdentityOptions, ) { return this.serverlessApi.delete( - this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions`, options), + this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions`, options), { body: JSON.stringify(securityIdentity), headers: {'Content-Type': 'application/json'}, @@ -43,20 +49,20 @@ export default class PushApi extends Resource { options?: SecurityIdentityOptions, ) { return this.serverlessApi.put( - this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions`, options), + this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions`, options), securityIdentity, ); } manageSecurityIdentities(securityProviderId: string, batchConfig: SecurityIdentityBatchConfig) { return this.serverlessApi.put( - this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions/batch`, batchConfig), + this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions/batch`, batchConfig), ); } deleteOldSecurityIdentities(securityProviderId: string, batchDelete: SecurityIdentityDeleteOptions) { return this.serverlessApi.delete( - this.buildPath(`${PushApi.baseUrl}/${securityProviderId}/permissions/olderthan`, batchDelete), + this.buildPath(`${PushApi.baseUrl}/providers/${securityProviderId}/permissions/olderthan`, batchDelete), ); } } diff --git a/src/resources/PushApi/PushApiInterfaces.ts b/src/resources/PushApi/PushApiInterfaces.ts index 973bae115..f315ba390 100644 --- a/src/resources/PushApi/PushApiInterfaces.ts +++ b/src/resources/PushApi/PushApiInterfaces.ts @@ -1,5 +1,20 @@ import {SinglePermissionResult, SinglePermissionIdentityType} from '../Enums.js'; +export interface FileContainer { + uploadUri: string; + fileId: string; + requiredHeaders: Record; +} + +export interface FileContainerOptions { + /** + * Whether to generate the presigned URL using the virtual hosted-style URL. + * + * Example of a virtual hosted-style url: `https://coveo-nprod-customer-data.s3.us-east-1.amazonaws.com/proda/blobstore/mycoveocloudv2organizationg8tp8wu3/b5e8767e-8f0d-4a89-9095-1127915c89c7[...]` + */ + useVirtualHostedStyleUrl?: boolean; +} + export interface SecurityIdentityAliasModel extends SecurityIdentityBase { mappings: AliasMappings; } diff --git a/src/resources/PushApi/tests/PushApi.spec.ts b/src/resources/PushApi/tests/PushApi.spec.ts index ec1530c2e..8a5965165 100644 --- a/src/resources/PushApi/tests/PushApi.spec.ts +++ b/src/resources/PushApi/tests/PushApi.spec.ts @@ -1,7 +1,7 @@ import API from '../../../APICore.js'; import {SinglePermissionIdentityType} from '../../Enums.js'; import PushApi from '../PushApi.js'; -import {SecurityIdentityAliasModel, SecurityIdentityOptions} from '../PushApiInterfaces.js'; +import {FileContainer, SecurityIdentityAliasModel, SecurityIdentityOptions} from '../PushApiInterfaces.js'; jest.mock('../../../APICore.js'); @@ -27,6 +27,39 @@ describe('PushAPI', () => { pushApi = new PushApi(api, serverlessApi); }); + describe('createFileContainer', () => { + test.each([ + ['/push/v1/organizations/{organizationName}/files', undefined], + ['/push/v1/organizations/{organizationName}/files', {}], + [ + '/push/v1/organizations/{organizationName}/files?useVirtualHostedStyleUrl=true', + {useVirtualHostedStyleUrl: true}, + ], + [ + '/push/v1/organizations/{organizationName}/files?useVirtualHostedStyleUrl=false', + {useVirtualHostedStyleUrl: false}, + ], + ['/push/v1/organizations/{organizationName}/files', {useVirtualHostedStyleUrl: undefined}], + ])('should make a POST call to %s when called with %o', async (url, options) => { + serverlessApi.post.mockImplementation( + async (): Promise => ({ + fileId: 'somefileid', + requiredHeaders: {a: 'b', c: 'd'}, + uploadUri: 'https://something', + }), + ); + + const fileContainer = await pushApi.createFileContainer(options); + expect(fileContainer).toEqual({ + fileId: 'somefileid', + requiredHeaders: {a: 'b', c: 'd'}, + uploadUri: 'https://something', + }); + expect(serverlessApi.post).toHaveBeenCalledTimes(1); + expect(serverlessApi.post).toHaveBeenCalledWith(url); + }); + }); + describe('Security Identity', () => { describe('createOrUpdateSecurityIdentityAlias', () => { test.each([