-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotion_service.py
More file actions
80 lines (70 loc) · 2.16 KB
/
Copy pathnotion_service.py
File metadata and controls
80 lines (70 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
from notion_client import Client
NOTION_TOKEN = os.environ.get("NOTION_TOKEN")
def get_notion_client():
if not NOTION_TOKEN:
return None
return Client(auth=NOTION_TOKEN)
def create_page(parent_page_id, title, content_blocks=None):
"""
Creates a new page in Notion.
"""
client = get_notion_client()
if not client:
return {"error": "Notion token not configured"}
try:
new_page = client.pages.create(
parent={"page_id": parent_page_id},
properties={
"title": [
{
"text": {
"content": title
}
}
]
},
children=content_blocks or []
)
return {"status": "success", "page": new_page}
except Exception as e:
return {"error": str(e)}
def add_to_database(database_id, properties):
"""
Adds a new entry to a Notion database.
"""
client = get_notion_client()
if not client:
return {"error": "Notion token not configured"}
try:
new_page = client.pages.create(
parent={"database_id": database_id},
properties=properties
)
return {"status": "success", "page": new_page}
except Exception as e:
return {"error": str(e)}
def get_database(database_id):
"""
Retrieves a Notion database.
"""
client = get_notion_client()
if not client:
return {"error": "Notion token not configured"}
try:
database = client.databases.retrieve(database_id=database_id)
return {"status": "success", "database": database}
except Exception as e:
return {"error": str(e)}
def query_database(database_id, filter_obj=None):
"""
Queries a Notion database.
"""
client = get_notion_client()
if not client:
return {"error": "Notion token not configured"}
try:
results = client.databases.query(database_id=database_id, filter=filter_obj)
return {"status": "success", "results": results.get("results", [])}
except Exception as e:
return {"error": str(e)}