Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #48 from alexa/csr-ddb-provider
Browse files Browse the repository at this point in the history
Added ddb provider for catalog explorer. Also added a new response fo…
  • Loading branch information
karanggg authored Mar 15, 2023
2 parents 8b2ff73 + 68d92ac commit ef2e68f
Show file tree
Hide file tree
Showing 16 changed files with 730 additions and 101 deletions.
6 changes: 3 additions & 3 deletions catalog-explorer/lambda/catalog-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// representation of a page token; simply a raw string as the real representation
// will differ with each catalog provider instance
export type PageToken = string;
export type PageToken = any;

// a single page of items, contains next/previous tokens to allow for easy navigation
export interface Page<Item>{
Expand Down Expand Up @@ -58,7 +58,7 @@ export interface CatalogProvider<SearchConditions, Item> {
performSearch(
searchConditions: SearchConditions,
pageSize: number
): RecommendationResult<SearchConditions, Item>;
): RecommendationResult<SearchConditions, Item> | Promise<RecommendationResult<SearchConditions, Item>>;

// get a page of items for the given search conditions, page token,
// page size and paging direction
Expand All @@ -67,7 +67,7 @@ export interface CatalogProvider<SearchConditions, Item> {
pageSize: number,
pageToken: PageToken | undefined,
pagingDirection: PagingDirection
): RecommendationResult<SearchConditions, Item>;
): RecommendationResult<SearchConditions, Item> | Promise<RecommendationResult<SearchConditions, Item>>;

// selects the item at the given index from the provided page
selectItem(
Expand Down
2 changes: 1 addition & 1 deletion catalog-explorer/lambda/handlers/base-api-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export abstract class BaseApiHandler implements RequestHandler {
return util.isApiRequest(handlerInput, this.apiName);
}

abstract handle(handlerInput : HandlerInput): Response;
abstract handle(handlerInput : HandlerInput): Response| Promise<Response>;

// returns activeCatalog from session, when session is used instead of focus or
// when there is no active catalog reference provided in the arguments
Expand Down
13 changes: 7 additions & 6 deletions catalog-explorer/lambda/handlers/get-page-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export class GetPageHandler extends BaseApiHandler{
super(apiName);
}

handle(handlerInput: HandlerInput): Response {
async handle(handlerInput: HandlerInput): Promise<Response> {
const args = util.getApiArguments(handlerInput) as Arguments;

let recommendationResult: RecommendationResult<any,any>;
let recommendationResult: RecommendationResult<any,any> | Promise< RecommendationResult<any,any>>;
const catalogRef = super.getActiveCatalog(handlerInput, args.catalogRef);

if (CatalogExplorer.useSessionArgs){
Expand All @@ -51,7 +51,9 @@ export class GetPageHandler extends BaseApiHandler{
// needs to be refactored for pagingDirection and current page size, once support for generics on actions is added
const pagingDirection = PagingDirection.NEXT;
recommendationResult = catalogProvider.getRecommendationsPage(args.searchConditions, catalogRef.pageSize, args.pageToken, pagingDirection);

}
recommendationResult = await Promise.resolve(recommendationResult)
//converting rescoped boolean to number, as ACDL does not support boolean datatype
const modifiedRecommendationResult = util.modifyRecommendationResultRescoped(recommendationResult);

Expand All @@ -61,12 +63,11 @@ export class GetPageHandler extends BaseApiHandler{
.getResponse();
}

getNewRecommendationResultFromSession(handlerInput: HandlerInput, args: Arguments, catalogRef: CatalogReference): RecommendationResult<any,any>{
async getNewRecommendationResultFromSession(handlerInput: HandlerInput, args: Arguments, catalogRef: CatalogReference): Promise<RecommendationResult<any,any>>{
const sessionState = CatalogExplorerSessionState.load(handlerInput);
const providerState = sessionState.providerState;

const catalogProvider: CatalogProvider<any, any> = CatalogExplorer.getProvider(catalogRef, providerState);

// get arguments from session state
const pageToken = sessionState.argsState?.upcomingPageToken;
const searchConditions = sessionState.argsState?.searchConditions;
Expand All @@ -76,8 +77,8 @@ export class GetPageHandler extends BaseApiHandler{
if (pagingDirection === undefined) {
throw new Error("Paging Direction not present in session")
}
const recommendationResult = catalogProvider.getRecommendationsPage(searchConditions,currPageSize!, pageToken, pagingDirection);

let recommendationResult = catalogProvider.getRecommendationsPage(searchConditions,currPageSize!, pageToken, pagingDirection);
recommendationResult = await Promise.resolve(recommendationResult)
// updating session state arguments
sessionState.providerState = catalogProvider.serialize();
sessionState.argsState!.currentPageTokens = {
Expand Down
12 changes: 6 additions & 6 deletions catalog-explorer/lambda/handlers/search-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export class SearchHandler extends BaseApiHandler {
return util.isApiRequestPrefix(handlerInput, this.apiName);
}

handle(handlerInput : HandlerInput): Response {
async handle(handlerInput : HandlerInput): Promise<Response >{
const args = util.getApiArguments(handlerInput) as any;
const searchType = this.getSearchTypeFromAPI(handlerInput);
let recommendationResult : RecommendationResult<any,any>;
let recommendationResult : RecommendationResult<any,any> | Promise<RecommendationResult<any,any>>;
if (searchType === "new" || searchType == "refine"){
recommendationResult = this.newSearchSession(handlerInput,searchType);
recommendationResult = await Promise.resolve(this.newSearchSession(handlerInput,searchType));
}
else {
throw new Error("Search Type fetched from API is incorrect");
Expand All @@ -45,9 +45,9 @@ export class SearchHandler extends BaseApiHandler {
.getResponse();
}

newSearchSession(handlerInput: HandlerInput, searchType : string): RecommendationResult<any,any>{
async newSearchSession(handlerInput: HandlerInput, searchType : string): Promise<RecommendationResult<any,any>>{
const sessionState = CatalogExplorerSessionState.load(handlerInput);

// get catalog reference out of session, instead of arguments
const catalogRef = sessionState.activeCatalog;
const providerState = sessionState.providerState;
Expand All @@ -64,7 +64,7 @@ export class SearchHandler extends BaseApiHandler {
const refinedSearchConditions = { ...searchConditionsFromSession, ...newSearchConditions };
newSearchConditions = refinedSearchConditions;
}
const recommendationResult = catalogProvider.performSearch(newSearchConditions, catalogRef.pageSize);
const recommendationResult = await Promise.resolve(catalogProvider.performSearch(newSearchConditions, catalogRef.pageSize));

// updating session state arguments
sessionState.providerState = catalogProvider.serialize();
Expand Down
58 changes: 0 additions & 58 deletions catalog-explorer/lambda/package.json

This file was deleted.

Loading

0 comments on commit ef2e68f

Please sign in to comment.