File tree 6 files changed +372
-220
lines changed 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 1
1
from __future__ import annotations
2
2
3
+ import logging
3
4
import os
4
5
from functools import cache
5
6
from pathlib import Path
6
7
from typing import NamedTuple
7
8
8
9
from dotenv import load_dotenv
9
10
11
+ logger = logging .getLogger (__name__ )
12
+
13
+
10
14
__all__ = [
11
15
'Config' ,
12
16
'config' ,
@@ -24,8 +28,12 @@ class Config(NamedTuple):
24
28
@cache
25
29
def get_config () -> Config :
26
30
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 )
29
37
return Config (notion_key = notion_key , root_id = root_id )
30
38
31
39
Original file line number Diff line number Diff line change 1
1
from __future__ import annotations
2
2
3
3
import argparse
4
+ import logging
4
5
from typing import cast
5
6
6
7
from notion_graph .parser import parser_main
7
8
from notion_graph .server import server_main
8
9
9
10
10
11
def main () -> int :
12
+
13
+ logging .basicConfig (level = logging .INFO , format = '%(message)s' )
14
+
11
15
parser = argparse .ArgumentParser ()
12
16
subparsers = parser .add_subparsers (required = True )
13
17
You can’t perform that action at this time.
0 commit comments