From ebc547a2d7b9c1a0a78d2e814501d80894b95a18 Mon Sep 17 00:00:00 2001 From: Ronaldo Macapobre Date: Fri, 3 Jan 2025 00:55:49 +0000 Subject: [PATCH] Added lookupService and catsAPI service to further wrap bc gov apis --- aws/lambdas/locations/get-locations/index.ts | 11 +++--- aws/lambdas/locations/get-rooms/index.ts | 11 +++--- aws/services/lookupService.ts | 35 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 aws/services/lookupService.ts diff --git a/aws/lambdas/locations/get-locations/index.ts b/aws/lambdas/locations/get-locations/index.ts index 602ed46f..81217850 100644 --- a/aws/lambdas/locations/get-locations/index.ts +++ b/aws/lambdas/locations/get-locations/index.ts @@ -1,5 +1,6 @@ import { APIGatewayEvent, APIGatewayProxyResult, Context } from "aws-lambda"; import { HttpService } from "../../../services/httpService"; +import { LookupService } from "../../../services/lookupService"; import SecretsManagerService from "../../../services/secretsManagerService"; export const handler = async ( @@ -10,16 +11,14 @@ export const handler = async ( try { const smService = new SecretsManagerService(); - const secretStringJson = await smService.getSecret( - process.env.FILE_SERVICES_CLIENT_SECRET_NAME! - ); - const { baseUrl, username, password } = JSON.parse(secretStringJson); const httpService = new HttpService(); + const lookupService = new LookupService(httpService, smService); + + lookupService.init(); - await httpService.init(baseUrl, username, password); const queryParams = event.queryStringParameters || {}; - const data = await httpService.get(event.path, queryParams); + const data = await lookupService.get(event.path, queryParams); return { statusCode: 200, diff --git a/aws/lambdas/locations/get-rooms/index.ts b/aws/lambdas/locations/get-rooms/index.ts index 602ed46f..81217850 100644 --- a/aws/lambdas/locations/get-rooms/index.ts +++ b/aws/lambdas/locations/get-rooms/index.ts @@ -1,5 +1,6 @@ import { APIGatewayEvent, APIGatewayProxyResult, Context } from "aws-lambda"; import { HttpService } from "../../../services/httpService"; +import { LookupService } from "../../../services/lookupService"; import SecretsManagerService from "../../../services/secretsManagerService"; export const handler = async ( @@ -10,16 +11,14 @@ export const handler = async ( try { const smService = new SecretsManagerService(); - const secretStringJson = await smService.getSecret( - process.env.FILE_SERVICES_CLIENT_SECRET_NAME! - ); - const { baseUrl, username, password } = JSON.parse(secretStringJson); const httpService = new HttpService(); + const lookupService = new LookupService(httpService, smService); + + lookupService.init(); - await httpService.init(baseUrl, username, password); const queryParams = event.queryStringParameters || {}; - const data = await httpService.get(event.path, queryParams); + const data = await lookupService.get(event.path, queryParams); return { statusCode: 200, diff --git a/aws/services/lookupService.ts b/aws/services/lookupService.ts new file mode 100644 index 00000000..371e42c1 --- /dev/null +++ b/aws/services/lookupService.ts @@ -0,0 +1,35 @@ +import { IHttpService } from "./httpService"; +import SecretsManagerService from "./secretsManagerService"; + +export class LookupService { + constructor( + private httpService: IHttpService, + private smService: SecretsManagerService + ) {} + + async init() { + console.log( + `Retrieving credentials for ${process.env.FILE_SERVICES_CLIENT_SECRET_NAME}.` + ); + + const secretStringJson = await this.smService.getSecret( + process.env.FILE_SERVICES_CLIENT_SECRET_NAME! + ); + + console.log(`Credentials retrieved: ${secretStringJson}`); + + console.log(`Initializing httpService`); + + const { baseUrl, username, password } = JSON.parse(secretStringJson); + + await this.httpService.init(baseUrl, username, password); + + console.log("httpService initialized."); + } + + async get(url: string, queryParams?: Record) { + console.log(`Sending GET request for ${url}...`); + + return await this.httpService.get(url, queryParams); + } +}