diff --git a/notion_client/_async_api_endpoints.py b/notion_client/_async_api_endpoints.py new file mode 100644 index 00000000..d1c937f1 --- /dev/null +++ b/notion_client/_async_api_endpoints.py @@ -0,0 +1,308 @@ +# mypy: disable-error-code="no-any-return" + +from __future__ import annotations + +from typing import TYPE_CHECKING, cast + +from notion_client.helpers import pick +from notion_client.types import BotUserObjectResponse, UserObjectResponse + +if TYPE_CHECKING: + from typing import Any + + from notion_client.client import AsyncClient + + +class AsyncEndpoint: + def __init__(self, parent: AsyncClient) -> None: + self.parent = parent + + +class BlocksChildrenEndpoint(AsyncEndpoint): + async def append(self, block_id: str, **kwargs: Any) -> Any: + """Create and append new children blocks to the block using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/patch-block-children)* + """ # noqa: E501 + return await self.parent.request( + path=f"blocks/{block_id}/children", + method="PATCH", + body=pick(kwargs, "children"), + auth=kwargs.get("auth"), + ) + + async def list(self, block_id: str, **kwargs: Any) -> Any: + """Return a paginated array of child [block objects](https://developers.notion.com/reference/block) contained in the block. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-block-children)* + """ # noqa: E501 + return await self.parent.request( + path=f"blocks/{block_id}/children", + method="GET", + query=pick(kwargs, "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + +class BlocksEndpoint(AsyncEndpoint): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.children = BlocksChildrenEndpoint(*args, **kwargs) + + async def retrieve(self, block_id: str, **kwargs: Any) -> Any: + """Retrieve a [Block object](https://developers.notion.com/reference/block) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-block)* + """ # noqa: E501 + return await self.parent.request( + path=f"blocks/{block_id}", method="GET", auth=kwargs.get("auth") + ) + + async def update(self, block_id: str, **kwargs: Any) -> Any: + """Update the content for the specified `block_id` based on the block type. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/update-a-block)* + """ # noqa: E501 + return await self.parent.request( + path=f"blocks/{block_id}", + method="PATCH", + body=pick( + kwargs, + "embed", + "type", + "archived", + "bookmark", + "image", + "video", + "pdf", + "file", + "audio", + "code", + "equation", + "divider", + "breadcrumb", + "table_of_contents", + "link_to_page", + "table_row", + "heading_1", + "heading_2", + "heading_3", + "paragraph", + "bulleted_list_item", + "numbered_list_item", + "quote", + "to_do", + "toggle", + "template", + "callout", + "synced_block", + "table", + ), + auth=kwargs.get("auth"), + ) + + def delete(self, block_id: str, **kwargs: Any) -> Any: + """Set a [Block object](https://developers.notion.com/reference/block), including page blocks, to `archived: true`. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/delete-a-block)* + """ # noqa: E501 + return self.parent.request( + path=f"blocks/{block_id}", + method="DELETE", + auth=kwargs.get("auth"), + ) + + +class DatabasesEndpoint(AsyncEndpoint): + async def list(self, **kwargs: Any) -> Any: # pragma: no cover + """List all [Databases](https://developers.notion.com/reference/database) shared with the authenticated integration. + + > ⚠️ **Deprecated endpoint** + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-databases)* + """ # noqa: E501 + return await self.parent.request( + path="databases", + method="GET", + query=pick(kwargs, "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + async def query(self, database_id: str, **kwargs: Any) -> Any: + """Get a list of [Pages](https://developers.notion.com/reference/page) contained in the database. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-database-query)* + """ # noqa: E501 + return await self.parent.request( + path=f"databases/{database_id}/query", + method="POST", + query=pick(kwargs, "filter_properties"), + body=pick(kwargs, "filter", "sorts", "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + async def retrieve(self, database_id: str, **kwargs: Any) -> Any: + """Retrieve a [Database object](https://developers.notion.com/reference/database) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-database)* + """ # noqa: E501 + return await self.parent.request( + path=f"databases/{database_id}", method="GET", auth=kwargs.get("auth") + ) + + async def create(self, **kwargs: Any) -> Any: + """Create a database as a subpage in the specified parent page. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/create-a-database)* + """ # noqa: E501 + return await self.parent.request( + path="databases", + method="POST", + body=pick( + kwargs, "parent", "title", "properties", "icon", "cover", "is_inline" + ), + auth=kwargs.get("auth"), + ) + + async def update(self, database_id: str, **kwargs: Any) -> Any: + """Update an existing database as specified by the parameters. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/update-a-database)* + """ # noqa: E501 + return await self.parent.request( + path=f"databases/{database_id}", + method="PATCH", + body=pick(kwargs, "properties", "title", "icon", "cover", "is_inline"), + auth=kwargs.get("auth"), + ) + + +class PagesPropertiesEndpoint(AsyncEndpoint): + async def retrieve(self, page_id: str, property_id: str, **kwargs: Any) -> Any: + """Retrieve a `property_item` object for a given `page_id` and `property_id`. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-page-property)* + """ # noqa: E501 + return await self.parent.request( + path=f"pages/{page_id}/properties/{property_id}", + method="GET", + auth=kwargs.get("auth"), + query=pick(kwargs, "start_cursor", "page_size"), + ) + + +class PagesEndpoint(AsyncEndpoint): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.properties = PagesPropertiesEndpoint(*args, **kwargs) + + async def create(self, **kwargs: Any) -> Any: + """Create a new page in the specified database or as a child of an existing page. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-page)* + """ # noqa: E501 + return await self.parent.request( + path="pages", + method="POST", + body=pick(kwargs, "parent", "properties", "children", "icon", "cover"), + auth=kwargs.get("auth"), + ) + + async def retrieve(self, page_id: str, **kwargs: Any) -> Any: + """Retrieve a [Page object](https://developers.notion.com/reference/page) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-page)* + """ # noqa: E501 + return await self.parent.request( + path=f"pages/{page_id}", method="GET", auth=kwargs.get("auth") + ) + + async def update(self, page_id: str, **kwargs: Any) -> Any: + """Update [page property values](https://developers.notion.com/reference/page#property-value-object) for the specified page. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/patch-page)* + """ # noqa: E501 + return await self.parent.request( + path=f"pages/{page_id}", + method="PATCH", + body=pick(kwargs, "archived", "properties", "icon", "cover"), + auth=kwargs.get("auth"), + ) + + +class SearchEndpoint(AsyncEndpoint): + async def __call__(self, **kwargs: Any) -> Any: + """Search all pages and child pages that are shared with the integration. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-search)* + """ # noqa: E501 + return await self.parent.request( + path="search", + method="POST", + body=pick(kwargs, "query", "sort", "filter", "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + +class CommentsEndpoint(AsyncEndpoint): + async def create(self, **kwargs: Any) -> Any: + """Create a new comment in the specified page or existing discussion thread. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/create-a-comment)* + """ # noqa: E501 + return self.parent.request( + path="comments", + method="POST", + body=pick(kwargs, "parent", "discussion_id", "rich_text"), + auth=kwargs.get("auth"), + ) + + async def list(self, **kwargs: Any) -> Any: + """Retrieve a list of un-resolved [Comment objects](https://developers.notion.com/reference/comment-object) from the specified block. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-comment)* + """ # noqa: E501 + return await self.parent.request( + path="comments", + method="GET", + query=pick(kwargs, "block_id", "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + +class UsersEndpoint(AsyncEndpoint): + async def list(self, **kwargs: Any) -> list[UserObjectResponse]: + """Return a paginated list of [Users](https://developers.notion.com/reference/user) for the workspace. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-users)* + """ # noqa: E501 + users = await self.parent.request( + path="users", + method="GET", + query=pick(kwargs, "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + return cast(list[UserObjectResponse], users) + + async def retrieve(self, user_id: str, **kwargs: Any) -> UserObjectResponse: + """Retrieve a [User](https://developers.notion.com/reference/user) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-user)* + """ # noqa: E501 + user = await self.parent.request( + path=f"users/{user_id}", method="GET", auth=kwargs.get("auth") + ) + + return cast(UserObjectResponse, user) + + async def me(self, **kwargs: Any) -> BotUserObjectResponse: + """Retrieve the bot [User](https://developers.notion.com/reference/user) associated with the API token. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-self)* + """ # noqa: E501 + bot = await self.parent.request( + path="users/me", method="GET", auth=kwargs.get("auth") + ) + + return cast(BotUserObjectResponse, bot) diff --git a/notion_client/_sync_api_endpoints.py b/notion_client/_sync_api_endpoints.py new file mode 100644 index 00000000..ae6639c3 --- /dev/null +++ b/notion_client/_sync_api_endpoints.py @@ -0,0 +1,308 @@ +# mypy: disable-error-code="no-any-return" + +from __future__ import annotations + +from typing import TYPE_CHECKING, cast + +from notion_client.helpers import pick +from notion_client.types import BotUserObjectResponse, UserObjectResponse + +if TYPE_CHECKING: + from typing import Any + + from notion_client.client import Client + + +class Endpoint: + def __init__(self, parent: Client) -> None: + self.parent = parent + + +class BlocksChildrenEndpoint(Endpoint): + def append(self, block_id: str, **kwargs: Any) -> Any: + """Create and append new children blocks to the block using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/patch-block-children)* + """ # noqa: E501 + return self.parent.request( + path=f"blocks/{block_id}/children", + method="PATCH", + body=pick(kwargs, "children"), + auth=kwargs.get("auth"), + ) + + def list(self, block_id: str, **kwargs: Any) -> Any: + """Return a paginated array of child [block objects](https://developers.notion.com/reference/block) contained in the block. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-block-children)* + """ # noqa: E501 + return self.parent.request( + path=f"blocks/{block_id}/children", + method="GET", + query=pick(kwargs, "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + +class BlocksEndpoint(Endpoint): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.children = BlocksChildrenEndpoint(*args, **kwargs) + + def retrieve(self, block_id: str, **kwargs: Any) -> Any: + """Retrieve a [Block object](https://developers.notion.com/reference/block) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-block)* + """ # noqa: E501 + return self.parent.request( + path=f"blocks/{block_id}", method="GET", auth=kwargs.get("auth") + ) + + def update(self, block_id: str, **kwargs: Any) -> Any: + """Update the content for the specified `block_id` based on the block type. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/update-a-block)* + """ # noqa: E501 + return self.parent.request( + path=f"blocks/{block_id}", + method="PATCH", + body=pick( + kwargs, + "embed", + "type", + "archived", + "bookmark", + "image", + "video", + "pdf", + "file", + "audio", + "code", + "equation", + "divider", + "breadcrumb", + "table_of_contents", + "link_to_page", + "table_row", + "heading_1", + "heading_2", + "heading_3", + "paragraph", + "bulleted_list_item", + "numbered_list_item", + "quote", + "to_do", + "toggle", + "template", + "callout", + "synced_block", + "table", + ), + auth=kwargs.get("auth"), + ) + + def delete(self, block_id: str, **kwargs: Any) -> Any: + """Set a [Block object](https://developers.notion.com/reference/block), including page blocks, to `archived: true`. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/delete-a-block)* + """ # noqa: E501 + return self.parent.request( + path=f"blocks/{block_id}", + method="DELETE", + auth=kwargs.get("auth"), + ) + + +class DatabasesEndpoint(Endpoint): + def list(self, **kwargs: Any) -> Any: # pragma: no cover + """List all [Databases](https://developers.notion.com/reference/database) shared with the authenticated integration. + + > ⚠️ **Deprecated endpoint** + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-databases)* + """ # noqa: E501 + return self.parent.request( + path="databases", + method="GET", + query=pick(kwargs, "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + def query(self, database_id: str, **kwargs: Any) -> Any: + """Get a list of [Pages](https://developers.notion.com/reference/page) contained in the database. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-database-query)* + """ # noqa: E501 + return self.parent.request( + path=f"databases/{database_id}/query", + method="POST", + query=pick(kwargs, "filter_properties"), + body=pick(kwargs, "filter", "sorts", "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + def retrieve(self, database_id: str, **kwargs: Any) -> Any: + """Retrieve a [Database object](https://developers.notion.com/reference/database) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-database)* + """ # noqa: E501 + return self.parent.request( + path=f"databases/{database_id}", method="GET", auth=kwargs.get("auth") + ) + + def create(self, **kwargs: Any) -> Any: + """Create a database as a subpage in the specified parent page. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/create-a-database)* + """ # noqa: E501 + return self.parent.request( + path="databases", + method="POST", + body=pick( + kwargs, "parent", "title", "properties", "icon", "cover", "is_inline" + ), + auth=kwargs.get("auth"), + ) + + def update(self, database_id: str, **kwargs: Any) -> Any: + """Update an existing database as specified by the parameters. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/update-a-database)* + """ # noqa: E501 + return self.parent.request( + path=f"databases/{database_id}", + method="PATCH", + body=pick(kwargs, "properties", "title", "icon", "cover", "is_inline"), + auth=kwargs.get("auth"), + ) + + +class PagesPropertiesEndpoint(Endpoint): + def retrieve(self, page_id: str, property_id: str, **kwargs: Any) -> Any: + """Retrieve a `property_item` object for a given `page_id` and `property_id`. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-page-property)* + """ # noqa: E501 + return self.parent.request( + path=f"pages/{page_id}/properties/{property_id}", + method="GET", + auth=kwargs.get("auth"), + query=pick(kwargs, "start_cursor", "page_size"), + ) + + +class PagesEndpoint(Endpoint): + def __init__(self, *args: Any, **kwargs: Any) -> None: + super().__init__(*args, **kwargs) + self.properties = PagesPropertiesEndpoint(*args, **kwargs) + + def create(self, **kwargs: Any) -> Any: + """Create a new page in the specified database or as a child of an existing page. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-page)* + """ # noqa: E501 + return self.parent.request( + path="pages", + method="POST", + body=pick(kwargs, "parent", "properties", "children", "icon", "cover"), + auth=kwargs.get("auth"), + ) + + def retrieve(self, page_id: str, **kwargs: Any) -> Any: + """Retrieve a [Page object](https://developers.notion.com/reference/page) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-page)* + """ # noqa: E501 + return self.parent.request( + path=f"pages/{page_id}", method="GET", auth=kwargs.get("auth") + ) + + def update(self, page_id: str, **kwargs: Any) -> Any: + """Update [page property values](https://developers.notion.com/reference/page#property-value-object) for the specified page. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/patch-page)* + """ # noqa: E501 + return self.parent.request( + path=f"pages/{page_id}", + method="PATCH", + body=pick(kwargs, "archived", "properties", "icon", "cover"), + auth=kwargs.get("auth"), + ) + + +class SearchEndpoint(Endpoint): + def __call__(self, **kwargs: Any) -> Any: + """Search all pages and child pages that are shared with the integration. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-search)* + """ # noqa: E501 + return self.parent.request( + path="search", + method="POST", + body=pick(kwargs, "query", "sort", "filter", "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + +class CommentsEndpoint(Endpoint): + def create(self, **kwargs: Any) -> Any: + """Create a new comment in the specified page or existing discussion thread. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/create-a-comment)* + """ # noqa: E501 + return self.parent.request( + path="comments", + method="POST", + body=pick(kwargs, "parent", "discussion_id", "rich_text"), + auth=kwargs.get("auth"), + ) + + def list(self, **kwargs: Any) -> Any: + """Retrieve a list of un-resolved [Comment objects](https://developers.notion.com/reference/comment-object) from the specified block. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-comment)* + """ # noqa: E501 + return self.parent.request( + path="comments", + method="GET", + query=pick(kwargs, "block_id", "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + +class UsersEndpoint(Endpoint): + def list(self, **kwargs: Any) -> list[UserObjectResponse]: + """Return a paginated list of [Users](https://developers.notion.com/reference/user) for the workspace. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-users)* + """ # noqa: E501 + users = self.parent.request( + path="users", + method="GET", + query=pick(kwargs, "start_cursor", "page_size"), + auth=kwargs.get("auth"), + ) + + return cast(list[UserObjectResponse], users) + + def retrieve(self, user_id: str, **kwargs: Any) -> UserObjectResponse: + """Retrieve a [User](https://developers.notion.com/reference/user) using the ID specified. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-user)* + """ # noqa: E501 + user = self.parent.request( + path=f"users/{user_id}", method="GET", auth=kwargs.get("auth") + ) + + return cast(UserObjectResponse, user) + + def me(self, **kwargs: Any) -> BotUserObjectResponse: + """Retrieve the bot [User](https://developers.notion.com/reference/user) associated with the API token. + + *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-self)* + """ # noqa: E501 + bot = self.parent.request( + path="users/me", method="GET", auth=kwargs.get("auth") + ) + + return cast(BotUserObjectResponse, bot) diff --git a/notion_client/api_endpoints.py b/notion_client/api_endpoints.py index 8660dacc..4c4980b4 100644 --- a/notion_client/api_endpoints.py +++ b/notion_client/api_endpoints.py @@ -1,298 +1,46 @@ """Notion API endpoints.""" # noqa: E501 -from typing import TYPE_CHECKING, Any - -from notion_client.helpers import pick -from notion_client.typing import SyncAsync - -if TYPE_CHECKING: # pragma: no cover - from notion_client.client import BaseClient - - -class Endpoint: - def __init__(self, parent: "BaseClient") -> None: - self.parent = parent - - -class BlocksChildrenEndpoint(Endpoint): - def append(self, block_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Create and append new children blocks to the block using the ID specified. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/patch-block-children)* - """ # noqa: E501 - return self.parent.request( - path=f"blocks/{block_id}/children", - method="PATCH", - body=pick(kwargs, "children"), - auth=kwargs.get("auth"), - ) - - def list(self, block_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Return a paginated array of child [block objects](https://developers.notion.com/reference/block) contained in the block. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-block-children)* - """ # noqa: E501 - return self.parent.request( - path=f"blocks/{block_id}/children", - method="GET", - query=pick(kwargs, "start_cursor", "page_size"), - auth=kwargs.get("auth"), - ) - - -class BlocksEndpoint(Endpoint): - def __init__(self, *args: Any, **kwargs: Any) -> None: - super().__init__(*args, **kwargs) - self.children = BlocksChildrenEndpoint(*args, **kwargs) - - def retrieve(self, block_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Retrieve a [Block object](https://developers.notion.com/reference/block) using the ID specified. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-block)* - """ # noqa: E501 - return self.parent.request( - path=f"blocks/{block_id}", method="GET", auth=kwargs.get("auth") - ) - - def update(self, block_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Update the content for the specified `block_id` based on the block type. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/update-a-block)* - """ # noqa: E501 - return self.parent.request( - path=f"blocks/{block_id}", - method="PATCH", - body=pick( - kwargs, - "embed", - "type", - "archived", - "bookmark", - "image", - "video", - "pdf", - "file", - "audio", - "code", - "equation", - "divider", - "breadcrumb", - "table_of_contents", - "link_to_page", - "table_row", - "heading_1", - "heading_2", - "heading_3", - "paragraph", - "bulleted_list_item", - "numbered_list_item", - "quote", - "to_do", - "toggle", - "template", - "callout", - "synced_block", - "table", - ), - auth=kwargs.get("auth"), - ) - - def delete(self, block_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Set a [Block object](https://developers.notion.com/reference/block), including page blocks, to `archived: true`. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/delete-a-block)* - """ # noqa: E501 - return self.parent.request( - path=f"blocks/{block_id}", - method="DELETE", - auth=kwargs.get("auth"), - ) - - -class DatabasesEndpoint(Endpoint): - def list(self, **kwargs: Any) -> SyncAsync[Any]: # pragma: no cover - """List all [Databases](https://developers.notion.com/reference/database) shared with the authenticated integration. - - > ⚠️ **Deprecated endpoint** - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-databases)* - """ # noqa: E501 - return self.parent.request( - path="databases", - method="GET", - query=pick(kwargs, "start_cursor", "page_size"), - auth=kwargs.get("auth"), - ) - - def query(self, database_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Get a list of [Pages](https://developers.notion.com/reference/page) contained in the database. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-database-query)* - """ # noqa: E501 - return self.parent.request( - path=f"databases/{database_id}/query", - method="POST", - query=pick(kwargs, "filter_properties"), - body=pick(kwargs, "filter", "sorts", "start_cursor", "page_size"), - auth=kwargs.get("auth"), - ) - - def retrieve(self, database_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Retrieve a [Database object](https://developers.notion.com/reference/database) using the ID specified. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-database)* - """ # noqa: E501 - return self.parent.request( - path=f"databases/{database_id}", method="GET", auth=kwargs.get("auth") - ) - - def create(self, **kwargs: Any) -> SyncAsync[Any]: - """Create a database as a subpage in the specified parent page. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/create-a-database)* - """ # noqa: E501 - return self.parent.request( - path="databases", - method="POST", - body=pick( - kwargs, "parent", "title", "properties", "icon", "cover", "is_inline" - ), - auth=kwargs.get("auth"), - ) - - def update(self, database_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Update an existing database as specified by the parameters. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/update-a-database)* - """ # noqa: E501 - return self.parent.request( - path=f"databases/{database_id}", - method="PATCH", - body=pick(kwargs, "properties", "title", "icon", "cover", "is_inline"), - auth=kwargs.get("auth"), - ) - - -class PagesPropertiesEndpoint(Endpoint): - def retrieve(self, page_id: str, property_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Retrieve a `property_item` object for a given `page_id` and `property_id`. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-page-property)* - """ # noqa: E501 - return self.parent.request( - path=f"pages/{page_id}/properties/{property_id}", - method="GET", - auth=kwargs.get("auth"), - query=pick(kwargs, "start_cursor", "page_size"), - ) - - -class PagesEndpoint(Endpoint): - def __init__(self, *args: Any, **kwargs: Any) -> None: - super().__init__(*args, **kwargs) - self.properties = PagesPropertiesEndpoint(*args, **kwargs) - - def create(self, **kwargs: Any) -> SyncAsync[Any]: - """Create a new page in the specified database or as a child of an existing page. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-page)* - """ # noqa: E501 - return self.parent.request( - path="pages", - method="POST", - body=pick(kwargs, "parent", "properties", "children", "icon", "cover"), - auth=kwargs.get("auth"), - ) - - def retrieve(self, page_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Retrieve a [Page object](https://developers.notion.com/reference/page) using the ID specified. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-page)* - """ # noqa: E501 - return self.parent.request( - path=f"pages/{page_id}", method="GET", auth=kwargs.get("auth") - ) - - def update(self, page_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Update [page property values](https://developers.notion.com/reference/page#property-value-object) for the specified page. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/patch-page)* - """ # noqa: E501 - return self.parent.request( - path=f"pages/{page_id}", - method="PATCH", - body=pick(kwargs, "archived", "properties", "icon", "cover"), - auth=kwargs.get("auth"), - ) - - -class UsersEndpoint(Endpoint): - def list(self, **kwargs: Any) -> SyncAsync[Any]: - """Return a paginated list of [Users](https://developers.notion.com/reference/user) for the workspace. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-users)* - """ # noqa: E501 - return self.parent.request( - path="users", - method="GET", - query=pick(kwargs, "start_cursor", "page_size"), - auth=kwargs.get("auth"), - ) - - def retrieve(self, user_id: str, **kwargs: Any) -> SyncAsync[Any]: - """Retrieve a [User](https://developers.notion.com/reference/user) using the ID specified. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-user)* - """ # noqa: E501 - return self.parent.request( - path=f"users/{user_id}", method="GET", auth=kwargs.get("auth") - ) - - def me(self, **kwargs: Any) -> SyncAsync[Any]: - """Retrieve the bot [User](https://developers.notion.com/reference/user) associated with the API token. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/get-self)* - """ # noqa: E501 - return self.parent.request( - path="users/me", method="GET", auth=kwargs.get("auth") - ) - - -class SearchEndpoint(Endpoint): - def __call__(self, **kwargs: Any) -> SyncAsync[Any]: - """Search all pages and child pages that are shared with the integration. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/post-search)* - """ # noqa: E501 - return self.parent.request( - path="search", - method="POST", - body=pick(kwargs, "query", "sort", "filter", "start_cursor", "page_size"), - auth=kwargs.get("auth"), - ) - - -class CommentsEndpoint(Endpoint): - def create(self, **kwargs: Any) -> SyncAsync[Any]: - """Create a new comment in the specified page or existing discussion thread. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/create-a-comment)* - """ # noqa: E501 - return self.parent.request( - path="comments", - method="POST", - body=pick(kwargs, "parent", "discussion_id", "rich_text"), - auth=kwargs.get("auth"), - ) - - def list(self, **kwargs: Any) -> SyncAsync[Any]: - """Retrieve a list of un-resolved [Comment objects](https://developers.notion.com/reference/comment-object) from the specified block. - - *[🔗 Endpoint documentation](https://developers.notion.com/reference/retrieve-a-comment)* - """ # noqa: E501 - return self.parent.request( - path="comments", - method="GET", - query=pick(kwargs, "block_id", "start_cursor", "page_size"), - auth=kwargs.get("auth"), - ) +from notion_client._async_api_endpoints import ( + BlocksChildrenEndpoint as AsyncBlocksChildrenEndpoint, +) +from notion_client._async_api_endpoints import BlocksEndpoint as AsyncBlocksEndpoint +from notion_client._async_api_endpoints import CommentsEndpoint as AsyncCommentsEndpoint +from notion_client._async_api_endpoints import ( + DatabasesEndpoint as AsyncDatabasesEndpoint, +) +from notion_client._async_api_endpoints import PagesEndpoint as AsyncPagesEndpoint +from notion_client._async_api_endpoints import ( + PagesPropertiesEndpoint as AsyncPagePropertiesEndpoint, +) +from notion_client._async_api_endpoints import SearchEndpoint as AsyncSearchEndpoint +from notion_client._async_api_endpoints import UsersEndpoint as AsyncUsersEndpoint +from notion_client._sync_api_endpoints import ( + BlocksChildrenEndpoint, + BlocksEndpoint, + CommentsEndpoint, + DatabasesEndpoint, + PagesEndpoint, + PagesPropertiesEndpoint, + SearchEndpoint, + UsersEndpoint, +) + +__all__ = [ + "AsyncBlocksChildrenEndpoint", + "AsyncBlocksEndpoint", + "AsyncCommentsEndpoint", + "AsyncDatabasesEndpoint", + "AsyncPagePropertiesEndpoint", + "AsyncPagesEndpoint", + "AsyncPagePropertiesEndpoint", + "AsyncSearchEndpoint", + "AsyncUsersEndpoint", + "BlocksChildrenEndpoint", + "BlocksEndpoint", + "CommentsEndpoint", + "DatabasesEndpoint", + "PagesEndpoint", + "PagesPropertiesEndpoint", + "SearchEndpoint", + "UsersEndpoint", +] diff --git a/notion_client/client.py b/notion_client/client.py index 23fda579..01462ca9 100644 --- a/notion_client/client.py +++ b/notion_client/client.py @@ -10,6 +10,12 @@ from httpx import Request, Response from notion_client.api_endpoints import ( + AsyncBlocksEndpoint, + AsyncCommentsEndpoint, + AsyncDatabasesEndpoint, + AsyncPagesEndpoint, + AsyncSearchEndpoint, + AsyncUsersEndpoint, BlocksEndpoint, CommentsEndpoint, DatabasesEndpoint, @@ -24,7 +30,6 @@ is_api_error_code, ) from notion_client.logging import make_console_logger -from notion_client.typing import SyncAsync @dataclass @@ -71,13 +76,6 @@ def __init__( self._clients: List[Union[httpx.Client, httpx.AsyncClient]] = [] self.client = client - self.blocks = BlocksEndpoint(self) - self.databases = DatabasesEndpoint(self) - self.users = UsersEndpoint(self) - self.pages = PagesEndpoint(self) - self.search = SearchEndpoint(self) - self.comments = CommentsEndpoint(self) - @property def client(self) -> Union[httpx.Client, httpx.AsyncClient]: return self._clients[-1] @@ -139,7 +137,7 @@ def request( query: Optional[Dict[Any, Any]] = None, body: Optional[Dict[Any, Any]] = None, auth: Optional[str] = None, - ) -> SyncAsync[Any]: + ) -> Any: # noqa pass @@ -159,6 +157,13 @@ def __init__( client = httpx.Client() super().__init__(client, options, **kwargs) + self.blocks = BlocksEndpoint(self) + self.databases = DatabasesEndpoint(self) + self.users = UsersEndpoint(self) + self.pages = PagesEndpoint(self) + self.search = SearchEndpoint(self) + self.comments = CommentsEndpoint(self) + def __enter__(self) -> "Client": self.client = httpx.Client() self.client.__enter__() @@ -209,6 +214,13 @@ def __init__( client = httpx.AsyncClient() super().__init__(client, options, **kwargs) + self.blocks = AsyncBlocksEndpoint(self) + self.databases = AsyncDatabasesEndpoint(self) + self.users = AsyncUsersEndpoint(self) + self.pages = AsyncPagesEndpoint(self) + self.search = AsyncSearchEndpoint(self) + self.comments = AsyncCommentsEndpoint(self) + async def __aenter__(self) -> "AsyncClient": self.client = httpx.AsyncClient() await self.client.__aenter__() diff --git a/notion_client/types.py b/notion_client/types.py new file mode 100644 index 00000000..616ecb58 --- /dev/null +++ b/notion_client/types.py @@ -0,0 +1,45 @@ +"""The types for the various responses.""" + +from typing import Literal, TypedDict, Union + + +class EmptyObject(TypedDict): + ... + + +# ---------- User ---------- + + +class PersonDetails(TypedDict): + email: str | None + + +class PersonUserObjectResponse(TypedDict): + type: Literal["person"] + person: PersonDetails + name: str | None + avatar_url: str | None + id: str + object: Literal["user"] + + +class WorkspaceOwner(TypedDict): + type: Literal["workspace"] + workspace: Literal[True] + + +class BotDetails(TypedDict): + owner: PersonUserObjectResponse | WorkspaceOwner + workspace_name: str | None + + +class BotUserObjectResponse(TypedDict): + type: Literal["bot"] + bot: EmptyObject | BotDetails + name: str | None + avatar_url: str | None + id: str + object: Literal["user"] + + +UserObjectResponse = Union[PersonUserObjectResponse, BotUserObjectResponse] diff --git a/notion_client/typing.py b/notion_client/typing.py deleted file mode 100644 index 97ebc80c..00000000 --- a/notion_client/typing.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Custom type definitions for notion-sdk-py.""" -from typing import Awaitable, TypeVar, Union - -T = TypeVar("T") -SyncAsync = Union[T, Awaitable[T]]