Skip to content

feat: add pagination support for Notion search #11194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
63 changes: 45 additions & 18 deletions api/libs/oauth_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,29 @@ def get_authorized_pages(self, access_token: str):
return pages

def notion_page_search(self, access_token: str):
data = {"filter": {"value": "page", "property": "object"}}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}",
"Notion-Version": "2022-06-28",
}
response = requests.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers)
response_json = response.json()
results = response_json.get("results", [])
results = []
next_cursor = None
has_more = True

while has_more:
data = {
"filter": {"value": "page", "property": "object"},
**({"start_cursor": next_cursor} if next_cursor else {}),
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}",
"Notion-Version": "2022-06-28",
}

response = requests.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers)
response_json = response.json()

results.extend(response_json.get("results", []))

has_more = response_json.get("has_more", False)
next_cursor = response_json.get("next_cursor", None)

return results

def notion_block_parent_page_id(self, access_token: str, block_id: str):
Expand Down Expand Up @@ -260,13 +274,26 @@ def notion_workspace_name(self, access_token: str):
return "workspace"

def notion_database_search(self, access_token: str):
data = {"filter": {"value": "database", "property": "object"}}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}",
"Notion-Version": "2022-06-28",
}
response = requests.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers)
response_json = response.json()
results = response_json.get("results", [])
results = []
next_cursor = None
has_more = True

while has_more:
data = {
"filter": {"value": "database", "property": "object"},
**({"start_cursor": next_cursor} if next_cursor else {}),
}
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}",
"Notion-Version": "2022-06-28",
}
response = requests.post(url=self._NOTION_PAGE_SEARCH, json=data, headers=headers)
response_json = response.json()

results.extend(response_json.get("results", []))

has_more = response_json.get("has_more", False)
next_cursor = response_json.get("next_cursor", None)

return results