|
15 | 15 | "metadata": {}, |
16 | 16 | "outputs": [], |
17 | 17 | "source": [ |
18 | | - "#| default_exp backends.factory" |
| 18 | + "# | default_exp backends.factory" |
19 | 19 | ] |
20 | 20 | }, |
21 | 21 | { |
|
24 | 24 | "metadata": {}, |
25 | 25 | "outputs": [], |
26 | 26 | "source": [ |
27 | | - "#| export\n", |
| 27 | + "# | export\n", |
28 | 28 | "import typing as t\n", |
29 | 29 | "import os\n", |
30 | 30 | "\n", |
|
39 | 39 | "metadata": {}, |
40 | 40 | "outputs": [], |
41 | 41 | "source": [ |
42 | | - "#| export\n", |
| 42 | + "# | export\n", |
43 | 43 | "class NotionClientFactory:\n", |
44 | 44 | " \"\"\"Factory for creating Notion client instances.\"\"\"\n", |
45 | | - " \n", |
| 45 | + "\n", |
46 | 46 | " @staticmethod\n", |
47 | 47 | " def create(\n", |
48 | 48 | " use_mock: bool = False,\n", |
49 | 49 | " api_key: t.Optional[str] = None,\n", |
50 | 50 | " initialize_project: bool = False,\n", |
51 | | - " root_page_id: t.Optional[str] = None\n", |
| 51 | + " root_page_id: t.Optional[str] = None,\n", |
52 | 52 | " ) -> t.Union[NotionClient, MockNotionClient]:\n", |
53 | 53 | " \"\"\"Create a Notion client.\n", |
54 | | - " \n", |
| 54 | + "\n", |
55 | 55 | " Args:\n", |
56 | 56 | " use_mock: If True, create a mock client\n", |
57 | 57 | " api_key: Notion API key (only used for real client)\n", |
58 | 58 | " initialize_project: If True and using mock, initialize project structure\n", |
59 | 59 | " root_page_id: Required if initialize_project is True\n", |
60 | | - " \n", |
| 60 | + "\n", |
61 | 61 | " Returns:\n", |
62 | 62 | " Union[NotionClient, MockNotionClient]: A real or mock client\n", |
63 | 63 | " \"\"\"\n", |
64 | 64 | " if use_mock:\n", |
65 | 65 | " client = MockNotionClient()\n", |
66 | | - " \n", |
| 66 | + "\n", |
67 | 67 | " # Optionally initialize project structure\n", |
68 | 68 | " if initialize_project and root_page_id:\n", |
69 | 69 | " # Create root page if it doesn't exist in the mock client\n", |
|
77 | 77 | " \"archived\": False,\n", |
78 | 78 | " \"properties\": {\n", |
79 | 79 | " \"title\": {\n", |
80 | | - " \"type\": \"title\", \n", |
81 | | - " \"title\": [{\"plain_text\": \"Root Page\", \"type\": \"text\", \"text\": {\"content\": \"Root Page\"}}]\n", |
| 80 | + " \"type\": \"title\",\n", |
| 81 | + " \"title\": [\n", |
| 82 | + " {\n", |
| 83 | + " \"plain_text\": \"Root Page\",\n", |
| 84 | + " \"type\": \"text\",\n", |
| 85 | + " \"text\": {\"content\": \"Root Page\"},\n", |
| 86 | + " }\n", |
| 87 | + " ],\n", |
82 | 88 | " }\n", |
83 | | - " }\n", |
| 89 | + " },\n", |
84 | 90 | " }\n", |
85 | 91 | " client.add_page(root_page)\n", |
86 | | - " \n", |
| 92 | + "\n", |
87 | 93 | " # Create required sub-pages\n", |
88 | 94 | " for page_name in [\"Datasets\", \"Experiments\", \"Comparisons\"]:\n", |
89 | 95 | " # Create page ID\n", |
90 | 96 | " page_id = client._create_id()\n", |
91 | | - " \n", |
| 97 | + "\n", |
92 | 98 | " # Create page\n", |
93 | 99 | " page = {\n", |
94 | 100 | " \"id\": page_id,\n", |
|
98 | 104 | " \"archived\": False,\n", |
99 | 105 | " \"properties\": {\n", |
100 | 106 | " \"title\": {\n", |
101 | | - " \"type\": \"title\", \n", |
102 | | - " \"title\": [{\"plain_text\": page_name, \"type\": \"text\", \"text\": {\"content\": page_name}}]\n", |
| 107 | + " \"type\": \"title\",\n", |
| 108 | + " \"title\": [\n", |
| 109 | + " {\n", |
| 110 | + " \"plain_text\": page_name,\n", |
| 111 | + " \"type\": \"text\",\n", |
| 112 | + " \"text\": {\"content\": page_name},\n", |
| 113 | + " }\n", |
| 114 | + " ],\n", |
103 | 115 | " }\n", |
104 | 116 | " },\n", |
105 | | - " \"parent\": {\"type\": \"page_id\", \"page_id\": root_page_id}\n", |
| 117 | + " \"parent\": {\"type\": \"page_id\", \"page_id\": root_page_id},\n", |
106 | 118 | " }\n", |
107 | 119 | " client.add_page(page)\n", |
108 | | - " \n", |
| 120 | + "\n", |
109 | 121 | " # Add child block to root\n", |
110 | 122 | " child_block = {\n", |
111 | 123 | " \"id\": client._create_id(),\n", |
112 | 124 | " \"object\": \"block\",\n", |
113 | 125 | " \"type\": \"child_page\",\n", |
114 | 126 | " \"created_time\": client._get_timestamp(),\n", |
115 | 127 | " \"last_edited_time\": client._get_timestamp(),\n", |
116 | | - " \"child_page\": {\n", |
117 | | - " \"title\": page_name\n", |
118 | | - " }\n", |
| 128 | + " \"child_page\": {\"title\": page_name},\n", |
119 | 129 | " }\n", |
120 | | - " \n", |
| 130 | + "\n", |
121 | 131 | " client.add_children(root_page_id, [child_block])\n", |
122 | | - " \n", |
| 132 | + "\n", |
123 | 133 | " return client\n", |
124 | 134 | " else:\n", |
125 | 135 | " # For real client, use provided API key or environment variable\n", |
126 | 136 | " if api_key is None:\n", |
127 | 137 | " api_key = os.getenv(\"NOTION_API_KEY\")\n", |
128 | | - " \n", |
| 138 | + "\n", |
129 | 139 | " if api_key is None:\n", |
130 | | - " raise ValueError(\"api_key must be provided or set as NOTION_API_KEY environment variable\")\n", |
131 | | - " \n", |
| 140 | + " raise ValueError(\n", |
| 141 | + " \"api_key must be provided or set as NOTION_API_KEY environment variable\"\n", |
| 142 | + " )\n", |
| 143 | + "\n", |
132 | 144 | " return NotionClient(auth=api_key)" |
133 | 145 | ] |
134 | 146 | }, |
|
179 | 191 | ], |
180 | 192 | "source": [ |
181 | 193 | "mock_notion_client = NotionClientFactory.create(\n", |
182 | | - " use_mock=True, \n", |
183 | | - " initialize_project=True, \n", |
184 | | - " root_page_id=\"your_root_page_id\"\n", |
| 194 | + " use_mock=True, initialize_project=True, root_page_id=\"your_root_page_id\"\n", |
185 | 195 | ")\n", |
186 | 196 | "mock_notion_client" |
187 | 197 | ] |
|
192 | 202 | "metadata": {}, |
193 | 203 | "outputs": [], |
194 | 204 | "source": [ |
195 | | - "#| export\n", |
| 205 | + "# | export\n", |
196 | 206 | "class NotionBackendFactory:\n", |
197 | 207 | " \"\"\"Factory for creating NotionBackend instances.\"\"\"\n", |
198 | | - " \n", |
| 208 | + "\n", |
199 | 209 | " @staticmethod\n", |
200 | 210 | " def create(\n", |
201 | 211 | " root_page_id: str,\n", |
202 | 212 | " use_mock: bool = False,\n", |
203 | 213 | " api_key: t.Optional[str] = None,\n", |
204 | 214 | " initialize_project: bool = False,\n", |
205 | | - " notion_client: t.Optional[t.Union[NotionClient, MockNotionClient]] = None\n", |
| 215 | + " notion_client: t.Optional[t.Union[NotionClient, MockNotionClient]] = None,\n", |
206 | 216 | " ) -> NotionBackend:\n", |
207 | 217 | " \"\"\"Create a NotionBackend instance.\n", |
208 | | - " \n", |
| 218 | + "\n", |
209 | 219 | " Args:\n", |
210 | 220 | " root_page_id: The ID of the root page\n", |
211 | 221 | " use_mock: If True, create a backend with a mock client\n", |
212 | 222 | " api_key: Notion API key (only used for real client)\n", |
213 | 223 | " initialize_project: If True and using mock, initialize project structure\n", |
214 | 224 | " notion_client: Optional pre-configured Notion client\n", |
215 | | - " \n", |
| 225 | + "\n", |
216 | 226 | " Returns:\n", |
217 | 227 | " NotionBackend: A backend instance with either real or mock client\n", |
218 | 228 | " \"\"\"\n", |
|
222 | 232 | " use_mock=use_mock,\n", |
223 | 233 | " api_key=api_key,\n", |
224 | 234 | " initialize_project=initialize_project,\n", |
225 | | - " root_page_id=root_page_id\n", |
| 235 | + " root_page_id=root_page_id,\n", |
226 | 236 | " )\n", |
227 | | - " \n", |
| 237 | + "\n", |
228 | 238 | " # Create and return the backend\n", |
229 | | - " return NotionBackend(\n", |
230 | | - " root_page_id=root_page_id,\n", |
231 | | - " notion_client=notion_client\n", |
232 | | - " )" |
| 239 | + " return NotionBackend(root_page_id=root_page_id, notion_client=notion_client)" |
233 | 240 | ] |
234 | 241 | } |
235 | 242 | ], |
|
0 commit comments