Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added nbs/backends/.notest
Empty file.
85 changes: 46 additions & 39 deletions nbs/backends/factory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| default_exp backends.factory"
"# | default_exp backends.factory"
]
},
{
Expand All @@ -24,7 +24,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"# | export\n",
"import typing as t\n",
"import os\n",
"\n",
Expand All @@ -39,31 +39,31 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"# | export\n",
"class NotionClientFactory:\n",
" \"\"\"Factory for creating Notion client instances.\"\"\"\n",
" \n",
"\n",
" @staticmethod\n",
" def create(\n",
" use_mock: bool = False,\n",
" api_key: t.Optional[str] = None,\n",
" initialize_project: bool = False,\n",
" root_page_id: t.Optional[str] = None\n",
" root_page_id: t.Optional[str] = None,\n",
" ) -> t.Union[NotionClient, MockNotionClient]:\n",
" \"\"\"Create a Notion client.\n",
" \n",
"\n",
" Args:\n",
" use_mock: If True, create a mock client\n",
" api_key: Notion API key (only used for real client)\n",
" initialize_project: If True and using mock, initialize project structure\n",
" root_page_id: Required if initialize_project is True\n",
" \n",
"\n",
" Returns:\n",
" Union[NotionClient, MockNotionClient]: A real or mock client\n",
" \"\"\"\n",
" if use_mock:\n",
" client = MockNotionClient()\n",
" \n",
"\n",
" # Optionally initialize project structure\n",
" if initialize_project and root_page_id:\n",
" # Create root page if it doesn't exist in the mock client\n",
Expand All @@ -77,18 +77,24 @@
" \"archived\": False,\n",
" \"properties\": {\n",
" \"title\": {\n",
" \"type\": \"title\", \n",
" \"title\": [{\"plain_text\": \"Root Page\", \"type\": \"text\", \"text\": {\"content\": \"Root Page\"}}]\n",
" \"type\": \"title\",\n",
" \"title\": [\n",
" {\n",
" \"plain_text\": \"Root Page\",\n",
" \"type\": \"text\",\n",
" \"text\": {\"content\": \"Root Page\"},\n",
" }\n",
" ],\n",
" }\n",
" }\n",
" },\n",
" }\n",
" client.add_page(root_page)\n",
" \n",
"\n",
" # Create required sub-pages\n",
" for page_name in [\"Datasets\", \"Experiments\", \"Comparisons\"]:\n",
" # Create page ID\n",
" page_id = client._create_id()\n",
" \n",
"\n",
" # Create page\n",
" page = {\n",
" \"id\": page_id,\n",
Expand All @@ -98,37 +104,43 @@
" \"archived\": False,\n",
" \"properties\": {\n",
" \"title\": {\n",
" \"type\": \"title\", \n",
" \"title\": [{\"plain_text\": page_name, \"type\": \"text\", \"text\": {\"content\": page_name}}]\n",
" \"type\": \"title\",\n",
" \"title\": [\n",
" {\n",
" \"plain_text\": page_name,\n",
" \"type\": \"text\",\n",
" \"text\": {\"content\": page_name},\n",
" }\n",
" ],\n",
" }\n",
" },\n",
" \"parent\": {\"type\": \"page_id\", \"page_id\": root_page_id}\n",
" \"parent\": {\"type\": \"page_id\", \"page_id\": root_page_id},\n",
" }\n",
" client.add_page(page)\n",
" \n",
"\n",
" # Add child block to root\n",
" child_block = {\n",
" \"id\": client._create_id(),\n",
" \"object\": \"block\",\n",
" \"type\": \"child_page\",\n",
" \"created_time\": client._get_timestamp(),\n",
" \"last_edited_time\": client._get_timestamp(),\n",
" \"child_page\": {\n",
" \"title\": page_name\n",
" }\n",
" \"child_page\": {\"title\": page_name},\n",
" }\n",
" \n",
"\n",
" client.add_children(root_page_id, [child_block])\n",
" \n",
"\n",
" return client\n",
" else:\n",
" # For real client, use provided API key or environment variable\n",
" if api_key is None:\n",
" api_key = os.getenv(\"NOTION_API_KEY\")\n",
" \n",
"\n",
" if api_key is None:\n",
" raise ValueError(\"api_key must be provided or set as NOTION_API_KEY environment variable\")\n",
" \n",
" raise ValueError(\n",
" \"api_key must be provided or set as NOTION_API_KEY environment variable\"\n",
" )\n",
"\n",
" return NotionClient(auth=api_key)"
]
},
Expand Down Expand Up @@ -179,9 +191,7 @@
],
"source": [
"mock_notion_client = NotionClientFactory.create(\n",
" use_mock=True, \n",
" initialize_project=True, \n",
" root_page_id=\"your_root_page_id\"\n",
" use_mock=True, initialize_project=True, root_page_id=\"your_root_page_id\"\n",
")\n",
"mock_notion_client"
]
Expand All @@ -192,27 +202,27 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"# | export\n",
"class NotionBackendFactory:\n",
" \"\"\"Factory for creating NotionBackend instances.\"\"\"\n",
" \n",
"\n",
" @staticmethod\n",
" def create(\n",
" root_page_id: str,\n",
" use_mock: bool = False,\n",
" api_key: t.Optional[str] = None,\n",
" initialize_project: bool = False,\n",
" notion_client: t.Optional[t.Union[NotionClient, MockNotionClient]] = None\n",
" notion_client: t.Optional[t.Union[NotionClient, MockNotionClient]] = None,\n",
" ) -> NotionBackend:\n",
" \"\"\"Create a NotionBackend instance.\n",
" \n",
"\n",
" Args:\n",
" root_page_id: The ID of the root page\n",
" use_mock: If True, create a backend with a mock client\n",
" api_key: Notion API key (only used for real client)\n",
" initialize_project: If True and using mock, initialize project structure\n",
" notion_client: Optional pre-configured Notion client\n",
" \n",
"\n",
" Returns:\n",
" NotionBackend: A backend instance with either real or mock client\n",
" \"\"\"\n",
Expand All @@ -222,14 +232,11 @@
" use_mock=use_mock,\n",
" api_key=api_key,\n",
" initialize_project=initialize_project,\n",
" root_page_id=root_page_id\n",
" root_page_id=root_page_id,\n",
" )\n",
" \n",
"\n",
" # Create and return the backend\n",
" return NotionBackend(\n",
" root_page_id=root_page_id,\n",
" notion_client=notion_client\n",
" )"
" return NotionBackend(root_page_id=root_page_id, notion_client=notion_client)"
]
}
],
Expand Down
Loading