mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-20 23:16:49 +00:00
优化重试请求逻辑
This commit is contained in:
@@ -1,28 +1,19 @@
|
||||
import BLOG from '@/blog.config'
|
||||
import { NotionAPI } from 'notion-client'
|
||||
import { getDataFromCache, setDataToCache } from '@/lib/cache/cache_manager'
|
||||
import { deepClone, delay } from '../utils'
|
||||
|
||||
export async function getPostBlocks(id, from, slice, retryCount = 3) {
|
||||
export async function getPostBlocks(id, from, slice) {
|
||||
const cacheKey = 'page_block_' + id
|
||||
let pageBlock = await getDataFromCache(cacheKey)
|
||||
if (pageBlock) {
|
||||
console.log('[请求缓存]:', `from:${from}`, `id:${id}`, cacheKey)
|
||||
console.log('[请求缓存]:', `from:${from}`, cacheKey)
|
||||
return filterPostBlocks(id, pageBlock, slice)
|
||||
}
|
||||
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
|
||||
const api = new NotionAPI({ authToken })
|
||||
try {
|
||||
console.warn('[请求API]:', `from:${from}`, `id:${id}`)
|
||||
pageBlock = await api.getPage(id)
|
||||
console.warn('[请求成功]', `from:${from}`, `id:${id}`)
|
||||
} catch (error) {
|
||||
console.error('[请求失败]', `from:${from}`, `id:${id}`, `error:${error}`)
|
||||
if (retryCount && retryCount > 0) { // 重试
|
||||
console.error('[重试请求]', `from:${from}`, `id:${id}`, `剩余次数:${retryCount}`)
|
||||
return getPostBlocks(id, from, slice, retryCount - 1)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
console.warn('[请求API]:', `from:${from}`, `id:${id}`)
|
||||
pageBlock = await getPageWithRetry(id, from)
|
||||
console.warn('[请求完成]: 结果', `${pageBlock ? '成功' : '失败'}`, `from:${from}`, `id:${id}`)
|
||||
|
||||
if (pageBlock) {
|
||||
await setDataToCache(cacheKey, pageBlock)
|
||||
@@ -31,6 +22,33 @@ export async function getPostBlocks(id, from, slice, retryCount = 3) {
|
||||
return pageBlock
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用接口,失败会重试
|
||||
* @param {*} id
|
||||
* @param {*} retryAttempts
|
||||
*/
|
||||
async function getPageWithRetry(id, from, retryAttempts = 3) {
|
||||
if (retryAttempts && retryAttempts > 0) {
|
||||
console.error('[重试请求]', `from:${from}`, `id:${id}`, `剩余次数:${retryAttempts}`)
|
||||
try {
|
||||
const authToken = BLOG.NOTION_ACCESS_TOKEN || null
|
||||
const api = new NotionAPI({ authToken })
|
||||
return await api.getPage(id)
|
||||
} catch (e) {
|
||||
await delay(1000)
|
||||
const cacheKey = 'page_block_' + id
|
||||
const pageBlock = await getDataFromCache(cacheKey)
|
||||
if (pageBlock) {
|
||||
console.error('[获取缓存]', `from:${from}`, `id:${id}`, `剩余次数:${retryAttempts}`)
|
||||
return pageBlock
|
||||
}
|
||||
return await getPageWithRetry(id, from, retryAttempts - 1)
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取到的blockMap删除不需要的字段
|
||||
* @param {*} id 页面ID
|
||||
@@ -74,15 +92,3 @@ function filterPostBlocks(id, pageBlock, slice) {
|
||||
}
|
||||
return clonePageBlock
|
||||
}
|
||||
|
||||
function deepClone(obj) {
|
||||
const newObj = Array.isArray(obj) ? [] : {}
|
||||
if (obj && typeof obj === 'object') {
|
||||
for (const key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
newObj[key] = (obj && typeof obj[key] === 'object') ? deepClone(obj[key]) : obj[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
return newObj
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user