Skip to content

Commit

Permalink
feat: optimise call to parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Mar 2, 2024
1 parent 1940ebb commit 6ac76a5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
})) ?? ``
})

const googleApiKey = await getParameterValue({
Name: `GOOGLE_API_KEY`,
WithDecryption: true
})

if (!OFFLINE) {
const files = await fs.promises.readdir(TMP_DIR)

Expand Down Expand Up @@ -80,7 +85,8 @@ const lambda: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (
userNameHex,
credentials,
book,
link
link,
googleApiKey
})
} catch (e) {
await atomicUpdate(db, "book", book._id, (old) => ({
Expand Down
23 changes: 16 additions & 7 deletions packages/api/src/libs/books/retrieveMetadataAndSaveCover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ import { downloadToTmpFolder } from "@libs/download/downloadToTmpFolder"

const logger = Logger.namespace("retrieveMetadataAndSaveCover")

export type Context = {
export type RetrieveMetadataAndSaveCoverContext = {
userName: string
userNameHex: string
credentials?: any
book: BookDocType
link: LinkDocType
}

export const retrieveMetadataAndSaveCover = async (ctx: Context) => {
export const retrieveMetadataAndSaveCover = async (
ctx: RetrieveMetadataAndSaveCoverContext & {
googleApiKey?: string
}
) => {
console.log(
`syncMetadata run for user ${ctx.userName} with book ${ctx.book._id}`
)
Expand All @@ -46,11 +50,16 @@ export const retrieveMetadataAndSaveCover = async (ctx: Context) => {

let contentType = linkMetadata.contentType

const sourcesMetadata = await getBookSourcesMetadata({
...linkMetadata,
// some plugins returns filename and not title
title: path.parse(linkMetadata.title ?? "").name
})
const sourcesMetadata = await getBookSourcesMetadata(
{
...linkMetadata,
// some plugins returns filename and not title
title: path.parse(linkMetadata.title ?? "").name
},
{
googleApiKey: ctx.googleApiKey
}
)

const metadataList = [linkMetadata, ...sourcesMetadata]

Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/libs/download/downloadToTmpFolder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from "@libs/books/retrieveMetadataAndSaveCover"
import { RetrieveMetadataAndSaveCoverContext } from "@libs/books/retrieveMetadataAndSaveCover"
import { dataSourceFacade } from "@libs/plugins"
import { PromiseReturnType } from "@libs/types"
import { BookDocType, LinkDocType } from "@oboku/shared"
Expand All @@ -7,7 +7,7 @@ import fs from "fs"
import { TMP_DIR } from "src/constants"

export const downloadToTmpFolder = (
ctx: Context,
ctx: RetrieveMetadataAndSaveCoverContext,
book: BookDocType,
link: LinkDocType
) =>
Expand Down
13 changes: 2 additions & 11 deletions packages/api/src/libs/google/googleBooksApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ export type GoogleBooksApiResult = {
/**
* Supports formats like: [9782413023470, 978-1-947804-36-4]
*/
export const findByISBN = async (isbn: string) => {
const apiKey = await getParameterValue({
Name: `GOOGLE_API_KEY`,
WithDecryption: true
})
export const findByISBN = async (isbn: string, apiKey: string) => {
const response = await performWithBackoff({
asyncFunction: () =>
axios.get<GoogleBooksApiResult>(
Expand All @@ -38,12 +34,7 @@ export const findByISBN = async (isbn: string) => {
throw new Error(`An error occurred during findByISBN`)
}

export const findByTitle = async (name: string) => {
const apiKey = await getParameterValue({
Name: `GOOGLE_API_KEY`,
WithDecryption: true
})

export const findByTitle = async (name: string, apiKey: string) => {
const response = await performWithBackoff({
asyncFunction: () =>
axios.get<GoogleBooksApiResult>(
Expand Down
18 changes: 13 additions & 5 deletions packages/api/src/libs/metadata/getBookSourcesMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { Metadata } from "./types"
import { getGoogleMetadata } from "./google/getGoogleMetadata"
import { Logger } from "@libs/logger"
import { isAxiosError } from "axios"

const swallowError = async <T>(promise: Promise<T>) => {
const swallowGoogleError = async <T>(promise: Promise<T>) => {
try {
return await promise
} catch (e) {
Logger.error(e)
} catch (error) {
if (isAxiosError(error) && error.response?.status === 429) {
Logger.error("Google API too many request error")
} else {
Logger.error(error)
}
}
}

export const getBookSourcesMetadata = async (
metadata: Metadata
metadata: Metadata,
{ googleApiKey }: { googleApiKey?: string }
): Promise<Metadata[]> => {
const list = []

const google = await swallowError(getGoogleMetadata(metadata))
const google = await swallowGoogleError(
getGoogleMetadata(metadata, googleApiKey ?? "")
)

if (google) {
list.push(google)
Expand Down
15 changes: 7 additions & 8 deletions packages/api/src/libs/metadata/google/getGoogleMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { Logger } from "@libs/logger"
import { refineTitle } from "../refineTitle"

export const getGoogleMetadata = async (
metadata: Metadata
metadata: Metadata,
apiKey: string
): Promise<Metadata> => {
let response = metadata.isbn
? await findByISBN(metadata.isbn)
: await findByTitle(metadata.title ?? "")
? await findByISBN(metadata.isbn, apiKey)
: await findByTitle(metadata.title ?? "", apiKey)

if (!response.items?.length) {
let titleRefined = refineTitle(metadata.title ?? "", 1)
Expand All @@ -18,7 +19,7 @@ export const getGoogleMetadata = async (
`getGoogleMetadata was unable to find result for isbn:${metadata.isbn} or title:${metadata.title}. Trying to refine title with 1 deepness ${titleRefined}`
)

response = await findByTitle(titleRefined)
response = await findByTitle(titleRefined, apiKey)

if (!response.items?.length) {
titleRefined = refineTitle(metadata.title ?? "", 2)
Expand All @@ -27,7 +28,7 @@ export const getGoogleMetadata = async (
`getGoogleMetadata was unable to find result for ${titleRefined}. Trying to refine title with 2 deepness ${titleRefined}`
)

response = await findByTitle(titleRefined)
response = await findByTitle(titleRefined, apiKey)
}

if (!response.items?.length) {
Expand All @@ -37,12 +38,10 @@ export const getGoogleMetadata = async (
`getGoogleMetadata was unable to find result for ${titleRefined}. Trying to refine title with 2 deepness ${titleRefined}`
)

response = await findByTitle(titleRefined)
response = await findByTitle(titleRefined, apiKey)
}
}

console.log((response.items ?? [])[0])

return {
...parseGoogleMetadata(response),
type: "googleBookApi"
Expand Down

0 comments on commit 6ac76a5

Please sign in to comment.