Skip to content

Commit c2bed3d

Browse files
Merge pull request #1 from DominiqueGarmier/feature/new-parser
new parser
2 parents 53ff338 + fbbafd5 commit c2bed3d

File tree

6 files changed

+372
-220
lines changed

6 files changed

+372
-220
lines changed

notion_graph/client.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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}')

notion_graph/config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from __future__ import annotations
22

3+
import logging
34
import os
45
from functools import cache
56
from pathlib import Path
67
from typing import NamedTuple
78

89
from 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
2529
def 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

notion_graph/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
from __future__ import annotations
22

33
import argparse
4+
import logging
45
from typing import cast
56

67
from notion_graph.parser import parser_main
78
from notion_graph.server import server_main
89

910

1011
def 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

0 commit comments

Comments
 (0)