Fix:Add timeout to provider matching default to 30s #3000

This commit is contained in:
advplyr 2024-05-25 16:32:02 -05:00
parent 30d3e41542
commit 6fa49e0aab
9 changed files with 633 additions and 444 deletions

View file

@ -2,12 +2,14 @@ const axios = require('axios')
const Logger = require('../Logger')
class GoogleBooks {
constructor() { }
#responseTimeout = 30000
constructor() {}
extractIsbn(industryIdentifiers) {
if (!industryIdentifiers || !industryIdentifiers.length) return null
var isbnObj = industryIdentifiers.find(i => i.type === 'ISBN_13') || industryIdentifiers.find(i => i.type === 'ISBN_10')
var isbnObj = industryIdentifiers.find((i) => i.type === 'ISBN_13') || industryIdentifiers.find((i) => i.type === 'ISBN_10')
if (isbnObj && isbnObj.identifier) return isbnObj.identifier
return null
}
@ -38,24 +40,38 @@ class GoogleBooks {
}
}
async search(title, author) {
/**
* Search for a book by title and author
* @param {string} title
* @param {string} author
* @param {number} [timeout] response timeout in ms
* @returns {Promise<Object[]>}
**/
async search(title, author, timeout = this.#responseTimeout) {
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout
title = encodeURIComponent(title)
var queryString = `q=intitle:${title}`
let queryString = `q=intitle:${title}`
if (author) {
author = encodeURIComponent(author)
queryString += `+inauthor:${author}`
}
var url = `https://www.googleapis.com/books/v1/volumes?${queryString}`
const url = `https://www.googleapis.com/books/v1/volumes?${queryString}`
Logger.debug(`[GoogleBooks] Search url: ${url}`)
var items = await axios.get(url).then((res) => {
if (!res || !res.data || !res.data.items) return []
return res.data.items
}).catch(error => {
Logger.error('[GoogleBooks] Volume search error', error)
return []
})
return items.map(item => this.cleanResult(item))
const items = await axios
.get(url, {
timeout
})
.then((res) => {
if (!res || !res.data || !res.data.items) return []
return res.data.items
})
.catch((error) => {
Logger.error('[GoogleBooks] Volume search error', error)
return []
})
return items.map((item) => this.cleanResult(item))
}
}
module.exports = GoogleBooks
module.exports = GoogleBooks