File tree Expand file tree Collapse file tree 6 files changed +372
-220
lines changed Expand file tree Collapse file tree 6 files changed +372
-220
lines changed Original file line number Diff line number Diff line change 1+ from __future__ import annotations
2+
3+ from contextlib import asynccontextmanager
4+ from typing import Any
5+ from typing import cast
6+
7+ from aiohttp import ClientSession
8+
9+ from notion_graph .config import config
10+
11+
12+ @asynccontextmanager
13+ async def HeaderSession (auth : str | None = None ):
14+ auth = auth or config .notion_key
15+ headers = {
16+ 'Authorization' : f'Bearer { auth } ' ,
17+ 'Notion-Version' : '2022-02-22' ,
18+ }
19+ async with ClientSession (headers = headers ) as session :
20+ yield session
21+
22+
23+ async def _api_get (endpoint : str ) -> dict [str , Any ]:
24+ async with HeaderSession () as session :
25+ async with session .get (endpoint ) as resp :
26+ return cast (dict [str , Any ], await resp .json ())
27+
28+
29+ async def get_page (page_id : str ) -> dict [str , Any ]:
30+ return await _api_get (f'https://api.notion.com/v1/pages/{ page_id } ' )
31+
32+
33+ async def get_children (block_id : str ) -> dict [str , Any ]:
34+ return await _api_get (f'https://api.notion.com/v1/blocks/{ block_id } /children' )
35+
36+
37+ async def get_block (block_id : str ) -> dict [str , Any ]:
38+ return await _api_get (f'https://api.notion.com/v1/blocks/{ block_id } ' )
Original file line number Diff line number Diff line change 11from __future__ import annotations
22
3+ import logging
34import os
45from functools import cache
56from pathlib import Path
67from typing import NamedTuple
78
89from dotenv import load_dotenv
910
11+ logger = logging .getLogger (__name__ )
12+
13+
1014__all__ = [
1115 'Config' ,
1216 'config' ,
@@ -24,8 +28,12 @@ class Config(NamedTuple):
2428@cache
2529def get_config () -> Config :
2630 load_dotenv ()
27- notion_key = os .environ ['NOTION_KEY' ]
28- root_id = os .environ ['ROOT_ID' ]
31+ try :
32+ notion_key = os .environ ['NOTION_KEY' ]
33+ root_id = os .environ ['ROOT_ID' ]
34+ except KeyError :
35+ logger .warning ('Please set NOTION_KEY and ROOT_ID in .env' )
36+ raise SystemExit (1 )
2937 return Config (notion_key = notion_key , root_id = root_id )
3038
3139
Original file line number Diff line number Diff line change 11from __future__ import annotations
22
33import argparse
4+ import logging
45from typing import cast
56
67from notion_graph .parser import parser_main
78from notion_graph .server import server_main
89
910
1011def main () -> int :
12+
13+ logging .basicConfig (level = logging .INFO , format = '%(message)s' )
14+
1115 parser = argparse .ArgumentParser ()
1216 subparsers = parser .add_subparsers (required = True )
1317
You can’t perform that action at this time.
0 commit comments