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

View file

@ -2,11 +2,13 @@ import asyncio
import json import json
import logging import logging
import math import math
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, List, Optional, Union from typing import Dict, List, Optional, Union
from asgiref.sync import sync_to_async from asgiref.sync import sync_to_async
from fastapi import ( from fastapi import (
APIRouter, APIRouter,
BackgroundTasks,
Depends, Depends,
Header, Header,
HTTPException, HTTPException,
@ -58,6 +60,8 @@ logger = logging.getLogger(__name__)
api_content = APIRouter() api_content = APIRouter()
executor = ThreadPoolExecutor()
class File(BaseModel): class File(BaseModel):
path: str path: str
@ -77,6 +81,11 @@ class IndexerInput(BaseModel):
docx: Optional[dict[str, bytes]] = None 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("") @api_content.put("")
@requires(["authenticated"]) @requires(["authenticated"])
async def put_content( async def put_content(
@ -209,6 +218,7 @@ async def set_content_github(
@requires(["authenticated"]) @requires(["authenticated"])
async def set_content_notion( async def set_content_notion(
request: Request, request: Request,
background_tasks: BackgroundTasks,
updated_config: Union[NotionContentConfig, None], updated_config: Union[NotionContentConfig, None],
client: Optional[str] = None, client: Optional[str] = None,
): ):
@ -225,6 +235,10 @@ async def set_content_notion(
logger.error(e, exc_info=True) logger.error(e, exc_info=True)
raise HTTPException(status_code=500, detail="Failed to set Notion config") 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( update_telemetry_state(
request=request, request=request,
telemetry_type="api", telemetry_type="api",