| 
									
										
										
										
											2024-10-10 17:21:28 +08:00
										 |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  | import urllib.parse | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import requests | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from flask_login import current_user | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  | from models.source import DataSourceOauthBinding | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OAuthDataSource: | 
					
						
							|  |  |  |     def __init__(self, client_id: str, client_secret: str, redirect_uri: str): | 
					
						
							|  |  |  |         self.client_id = client_id | 
					
						
							|  |  |  |         self.client_secret = client_secret | 
					
						
							|  |  |  |         self.redirect_uri = redirect_uri | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_authorization_url(self): | 
					
						
							|  |  |  |         raise NotImplementedError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_access_token(self, code: str): | 
					
						
							|  |  |  |         raise NotImplementedError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class NotionOAuth(OAuthDataSource): | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |     _AUTH_URL = "https://api.notion.com/v1/oauth/authorize" | 
					
						
							|  |  |  |     _TOKEN_URL = "https://api.notion.com/v1/oauth/token" | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |     _NOTION_PAGE_SEARCH = "https://api.notion.com/v1/search" | 
					
						
							|  |  |  |     _NOTION_BLOCK_SEARCH = "https://api.notion.com/v1/blocks" | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |     _NOTION_BOT_USER = "https://api.notion.com/v1/users/me" | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_authorization_url(self): | 
					
						
							|  |  |  |         params = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             "client_id": self.client_id, | 
					
						
							|  |  |  |             "response_type": "code", | 
					
						
							|  |  |  |             "redirect_uri": self.redirect_uri, | 
					
						
							|  |  |  |             "owner": "user", | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         return f"{self._AUTH_URL}?{urllib.parse.urlencode(params)}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_access_token(self, code: str): | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         data = {"code": code, "grant_type": "authorization_code", "redirect_uri": self.redirect_uri} | 
					
						
							|  |  |  |         headers = {"Accept": "application/json"} | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         auth = (self.client_id, self.client_secret) | 
					
						
							|  |  |  |         response = requests.post(self._TOKEN_URL, data=data, auth=auth, headers=headers) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         response_json = response.json() | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         access_token = response_json.get("access_token") | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         if not access_token: | 
					
						
							|  |  |  |             raise ValueError(f"Error in Notion OAuth: {response_json}") | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         workspace_name = response_json.get("workspace_name") | 
					
						
							|  |  |  |         workspace_icon = response_json.get("workspace_icon") | 
					
						
							|  |  |  |         workspace_id = response_json.get("workspace_id") | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         # get all authorized pages | 
					
						
							|  |  |  |         pages = self.get_authorized_pages(access_token) | 
					
						
							|  |  |  |         source_info = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             "workspace_name": workspace_name, | 
					
						
							|  |  |  |             "workspace_icon": workspace_icon, | 
					
						
							|  |  |  |             "workspace_id": workspace_id, | 
					
						
							|  |  |  |             "pages": pages, | 
					
						
							|  |  |  |             "total": len(pages), | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         # save data source binding | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |         data_source_binding = DataSourceOauthBinding.query.filter( | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             db.and_( | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |                 DataSourceOauthBinding.tenant_id == current_user.current_tenant_id, | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 DataSourceOauthBinding.provider == "notion", | 
					
						
							|  |  |  |                 DataSourceOauthBinding.access_token == access_token, | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ).first() | 
					
						
							|  |  |  |         if data_source_binding: | 
					
						
							|  |  |  |             data_source_binding.source_info = source_info | 
					
						
							|  |  |  |             data_source_binding.disabled = False | 
					
						
							| 
									
										
										
										
											2024-10-10 17:21:28 +08:00
										 |  |  |             data_source_binding.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             db.session.commit() | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |             new_data_source_binding = DataSourceOauthBinding( | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |                 tenant_id=current_user.current_tenant_id, | 
					
						
							|  |  |  |                 access_token=access_token, | 
					
						
							|  |  |  |                 source_info=source_info, | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 provider="notion", | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |             db.session.add(new_data_source_binding) | 
					
						
							|  |  |  |             db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |     def save_internal_access_token(self, access_token: str): | 
					
						
							|  |  |  |         workspace_name = self.notion_workspace_name(access_token) | 
					
						
							|  |  |  |         workspace_icon = None | 
					
						
							|  |  |  |         workspace_id = current_user.current_tenant_id | 
					
						
							|  |  |  |         # get all authorized pages | 
					
						
							|  |  |  |         pages = self.get_authorized_pages(access_token) | 
					
						
							|  |  |  |         source_info = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             "workspace_name": workspace_name, | 
					
						
							|  |  |  |             "workspace_icon": workspace_icon, | 
					
						
							|  |  |  |             "workspace_id": workspace_id, | 
					
						
							|  |  |  |             "pages": pages, | 
					
						
							|  |  |  |             "total": len(pages), | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         # save data source binding | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |         data_source_binding = DataSourceOauthBinding.query.filter( | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |             db.and_( | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |                 DataSourceOauthBinding.tenant_id == current_user.current_tenant_id, | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 DataSourceOauthBinding.provider == "notion", | 
					
						
							|  |  |  |                 DataSourceOauthBinding.access_token == access_token, | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ).first() | 
					
						
							|  |  |  |         if data_source_binding: | 
					
						
							|  |  |  |             data_source_binding.source_info = source_info | 
					
						
							|  |  |  |             data_source_binding.disabled = False | 
					
						
							| 
									
										
										
										
											2024-10-10 17:21:28 +08:00
										 |  |  |             data_source_binding.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |             db.session.commit() | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |             new_data_source_binding = DataSourceOauthBinding( | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |                 tenant_id=current_user.current_tenant_id, | 
					
						
							|  |  |  |                 access_token=access_token, | 
					
						
							|  |  |  |                 source_info=source_info, | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 provider="notion", | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |             db.session.add(new_data_source_binding) | 
					
						
							|  |  |  |             db.session.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |     def sync_data_source(self, binding_id: str): | 
					
						
							|  |  |  |         # save data source binding | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |         data_source_binding = DataSourceOauthBinding.query.filter( | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             db.and_( | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |                 DataSourceOauthBinding.tenant_id == current_user.current_tenant_id, | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 DataSourceOauthBinding.provider == "notion", | 
					
						
							| 
									
										
										
										
											2024-06-15 02:46:02 +08:00
										 |  |  |                 DataSourceOauthBinding.id == binding_id, | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 DataSourceOauthBinding.disabled == False, | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ).first() | 
					
						
							|  |  |  |         if data_source_binding: | 
					
						
							|  |  |  |             # get all authorized pages | 
					
						
							|  |  |  |             pages = self.get_authorized_pages(data_source_binding.access_token) | 
					
						
							|  |  |  |             source_info = data_source_binding.source_info | 
					
						
							|  |  |  |             new_source_info = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 "workspace_name": source_info["workspace_name"], | 
					
						
							|  |  |  |                 "workspace_icon": source_info["workspace_icon"], | 
					
						
							|  |  |  |                 "workspace_id": source_info["workspace_id"], | 
					
						
							|  |  |  |                 "pages": pages, | 
					
						
							|  |  |  |                 "total": len(pages), | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             data_source_binding.source_info = new_source_info | 
					
						
							|  |  |  |             data_source_binding.disabled = False | 
					
						
							| 
									
										
										
										
											2024-10-10 17:21:28 +08:00
										 |  |  |             data_source_binding.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             db.session.commit() | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             raise ValueError("Data source binding not found") | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_authorized_pages(self, access_token: str): | 
					
						
							|  |  |  |         pages = [] | 
					
						
							|  |  |  |         page_results = self.notion_page_search(access_token) | 
					
						
							|  |  |  |         database_results = self.notion_database_search(access_token) | 
					
						
							|  |  |  |         # get page detail | 
					
						
							|  |  |  |         for page_result in page_results: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             page_id = page_result["id"] | 
					
						
							|  |  |  |             page_name = "Untitled" | 
					
						
							|  |  |  |             for key in page_result["properties"]: | 
					
						
							|  |  |  |                 if "title" in page_result["properties"][key] and page_result["properties"][key]["title"]: | 
					
						
							|  |  |  |                     title_list = page_result["properties"][key]["title"] | 
					
						
							|  |  |  |                     if len(title_list) > 0 and "plain_text" in title_list[0]: | 
					
						
							|  |  |  |                         page_name = title_list[0]["plain_text"] | 
					
						
							|  |  |  |             page_icon = page_result["icon"] | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             if page_icon: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 icon_type = page_icon["type"] | 
					
						
							| 
									
										
										
										
											2024-09-13 22:42:08 +08:00
										 |  |  |                 if icon_type in {"external", "file"}: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                     url = page_icon[icon_type]["url"] | 
					
						
							|  |  |  |                     icon = {"type": "url", "url": url if url.startswith("http") else f"https://www.notion.so{url}"} | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                     icon = {"type": "emoji", "emoji": page_icon[icon_type]} | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 icon = None | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             parent = page_result["parent"] | 
					
						
							|  |  |  |             parent_type = parent["type"] | 
					
						
							|  |  |  |             if parent_type == "block_id": | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |                 parent_id = self.notion_block_parent_page_id(access_token, parent[parent_type]) | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             elif parent_type == "workspace": | 
					
						
							|  |  |  |                 parent_id = "root" | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 parent_id = parent[parent_type] | 
					
						
							|  |  |  |             page = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 "page_id": page_id, | 
					
						
							|  |  |  |                 "page_name": page_name, | 
					
						
							|  |  |  |                 "page_icon": icon, | 
					
						
							|  |  |  |                 "parent_id": parent_id, | 
					
						
							|  |  |  |                 "type": "page", | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             pages.append(page) | 
					
						
							|  |  |  |             # get database detail | 
					
						
							|  |  |  |         for database_result in database_results: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             page_id = database_result["id"] | 
					
						
							|  |  |  |             if len(database_result["title"]) > 0: | 
					
						
							|  |  |  |                 page_name = database_result["title"][0]["plain_text"] | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 page_name = "Untitled" | 
					
						
							|  |  |  |             page_icon = database_result["icon"] | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             if page_icon: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 icon_type = page_icon["type"] | 
					
						
							| 
									
										
										
										
											2024-09-13 22:42:08 +08:00
										 |  |  |                 if icon_type in {"external", "file"}: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                     url = page_icon[icon_type]["url"] | 
					
						
							|  |  |  |                     icon = {"type": "url", "url": url if url.startswith("http") else f"https://www.notion.so{url}"} | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                     icon = {"type": icon_type, icon_type: page_icon[icon_type]} | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 icon = None | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             parent = database_result["parent"] | 
					
						
							|  |  |  |             parent_type = parent["type"] | 
					
						
							|  |  |  |             if parent_type == "block_id": | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |                 parent_id = self.notion_block_parent_page_id(access_token, parent[parent_type]) | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             elif parent_type == "workspace": | 
					
						
							|  |  |  |                 parent_id = "root" | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 parent_id = parent[parent_type] | 
					
						
							|  |  |  |             page = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |                 "page_id": page_id, | 
					
						
							|  |  |  |                 "page_name": page_name, | 
					
						
							|  |  |  |                 "page_icon": icon, | 
					
						
							|  |  |  |                 "parent_id": parent_id, | 
					
						
							|  |  |  |                 "type": "database", | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             pages.append(page) | 
					
						
							|  |  |  |         return pages | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def notion_page_search(self, access_token: str): | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         data = {"filter": {"value": "page", "property": "object"}} | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         headers = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             "Content-Type": "application/json", | 
					
						
							|  |  |  |             "Authorization": f"Bearer {access_token}", | 
					
						
							|  |  |  |             "Notion-Version": "2022-06-28", | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         response = requests.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers) | 
					
						
							|  |  |  |         response_json = response.json() | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         results = response_json.get("results", []) | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         return results | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def notion_block_parent_page_id(self, access_token: str, block_id: str): | 
					
						
							|  |  |  |         headers = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             "Authorization": f"Bearer {access_token}", | 
					
						
							|  |  |  |             "Notion-Version": "2022-06-28", | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         response = requests.get(url=f"{self._NOTION_BLOCK_SEARCH}/{block_id}", headers=headers) | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         response_json = response.json() | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         parent = response_json["parent"] | 
					
						
							|  |  |  |         parent_type = parent["type"] | 
					
						
							|  |  |  |         if parent_type == "block_id": | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |             return self.notion_block_parent_page_id(access_token, parent[parent_type]) | 
					
						
							|  |  |  |         return parent[parent_type] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |     def notion_workspace_name(self, access_token: str): | 
					
						
							|  |  |  |         headers = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             "Authorization": f"Bearer {access_token}", | 
					
						
							|  |  |  |             "Notion-Version": "2022-06-28", | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         response = requests.get(url=self._NOTION_BOT_USER, headers=headers) | 
					
						
							|  |  |  |         response_json = response.json() | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         if "object" in response_json and response_json["object"] == "user": | 
					
						
							|  |  |  |             user_type = response_json["type"] | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  |             user_info = response_json[user_type] | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             if "workspace_name" in user_info: | 
					
						
							|  |  |  |                 return user_info["workspace_name"] | 
					
						
							|  |  |  |         return "workspace" | 
					
						
							| 
									
										
										
										
											2023-06-17 19:50:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |     def notion_database_search(self, access_token: str): | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         data = {"filter": {"value": "database", "property": "object"}} | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         headers = { | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |             "Content-Type": "application/json", | 
					
						
							|  |  |  |             "Authorization": f"Bearer {access_token}", | 
					
						
							|  |  |  |             "Notion-Version": "2022-06-28", | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         response = requests.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers) | 
					
						
							|  |  |  |         response_json = response.json() | 
					
						
							| 
									
										
										
										
											2024-08-15 17:53:12 +08:00
										 |  |  |         results = response_json.get("results", []) | 
					
						
							| 
									
										
										
										
											2023-06-16 21:47:51 +08:00
										 |  |  |         return results |