Fix notion indexing with manually set token

This commit is contained in:
sabaimran 2024-10-05 09:13:16 -07:00
parent 29a422b6bc
commit d4ffeca90a
2 changed files with 16 additions and 1 deletions

View file

@ -52,7 +52,8 @@ class NotionToEntries(TextToEntries):
token=config.token,
)
self.session = requests.Session()
self.session.headers.update({"Authorization": f"Bearer {config.token}", "Notion-Version": "2022-02-22"})
if config.token:
self.session.headers.update({"Authorization": f"Bearer {config.token}", "Notion-Version": "2022-02-22"})
self.unsupported_block_types = [
NotionBlockType.BOOKMARK.value,
NotionBlockType.DIVIDER.value,

View file

@ -2,11 +2,13 @@ import asyncio
import json
import logging
import math
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, List, Optional, Union
from asgiref.sync import sync_to_async
from fastapi import (
APIRouter,
BackgroundTasks,
Depends,
Header,
HTTPException,
@ -58,6 +60,8 @@ logger = logging.getLogger(__name__)
api_content = APIRouter()
executor = ThreadPoolExecutor()
class File(BaseModel):
path: str
@ -77,6 +81,11 @@ class IndexerInput(BaseModel):
docx: Optional[dict[str, bytes]] = None
async def run_in_executor(func, *args):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(executor, func, *args)
@api_content.put("")
@requires(["authenticated"])
async def put_content(
@ -209,6 +218,7 @@ async def set_content_github(
@requires(["authenticated"])
async def set_content_notion(
request: Request,
background_tasks: BackgroundTasks,
updated_config: Union[NotionContentConfig, None],
client: Optional[str] = None,
):
@ -225,6 +235,10 @@ async def set_content_notion(
logger.error(e, exc_info=True)
raise HTTPException(status_code=500, detail="Failed to set Notion config")
if updated_config.token:
# Trigger an async job to configure_content. Let it run without blocking the response.
background_tasks.add_task(run_in_executor, configure_content, {}, False, SearchType.Notion, user)
update_telemetry_state(
request=request,
telemetry_type="api",