From fe8ac41e1c10a229c040bd7cadd9429a66fbcda3 Mon Sep 17 00:00:00 2001
From: c0j0s <c.junsheng@hotmail.com>
Date: Sat, 6 Mar 2021 00:52:26 +0800
Subject: [PATCH 1/6] Add optional limit argument to get_block

---
 notion/client.py     | 12 ++++++------
 notion/collection.py |  3 +++
 notion/store.py      | 11 ++++++-----
 3 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/notion/client.py b/notion/client.py
index 3d748934..b49cd779 100644
--- a/notion/client.py
+++ b/notion/client.py
@@ -158,15 +158,15 @@ def get_top_level_pages(self):
         records = self._update_user_info()
         return [self.get_block(bid) for bid in records["block"].keys()]
 
-    def get_record_data(self, table, id, force_refresh=False):
-        return self._store.get(table, id, force_refresh=force_refresh)
+    def get_record_data(self, table, id, force_refresh=False, limit=100):
+        return self._store.get(table, id, force_refresh=force_refresh, limit=limit)
 
-    def get_block(self, url_or_id, force_refresh=False):
+    def get_block(self, url_or_id, force_refresh=False, limit=100):
         """
         Retrieve an instance of a subclass of Block that maps to the block/page identified by the URL or ID passed in.
         """
         block_id = extract_id(url_or_id)
-        block = self.get_record_data("block", block_id, force_refresh=force_refresh)
+        block = self.get_record_data("block", block_id, force_refresh=force_refresh, limit=limit)
         if not block:
             return None
         if block.get("parent_table") == "collection":
@@ -306,11 +306,11 @@ def in_transaction(self):
         """
         return hasattr(self, "_transaction_operations")
 
-    def search_pages_with_parent(self, parent_id, search=""):
+    def search_pages_with_parent(self, parent_id, search="", limit=100):
         data = {
             "query": search,
             "parentId": parent_id,
-            "limit": 10000,
+            "limit": limit,
             "spaceId": self.current_space.id,
         }
         response = self.post("searchPagesWithParent", data).json()
diff --git a/notion/collection.py b/notion/collection.py
index 748cc067..fc1de31d 100644
--- a/notion/collection.py
+++ b/notion/collection.py
@@ -360,6 +360,7 @@ def __init__(
         sort=[],
         calendar_by="",
         group_by="",
+        limit=100
     ):
         assert not (
             aggregate and aggregations
@@ -374,6 +375,7 @@ def __init__(
         self.sort = _normalize_query_data(sort, collection)
         self.calendar_by = _normalize_property_name(calendar_by, collection)
         self.group_by = _normalize_property_name(group_by, collection)
+        self.limit = limit
         self._client = collection._client
 
     def execute(self):
@@ -393,6 +395,7 @@ def execute(self):
                 sort=self.sort,
                 calendar_by=self.calendar_by,
                 group_by=self.group_by,
+                limit= self.limit
             ),
             self,
         )
diff --git a/notion/store.py b/notion/store.py
index 57620c96..b50d5003 100644
--- a/notion/store.py
+++ b/notion/store.py
@@ -174,14 +174,14 @@ def get_role(self, table, id, force_refresh=False):
         self.get(table, id, force_refresh=force_refresh)
         return self._role[table].get(id, None)
 
-    def get(self, table, id, force_refresh=False):
+    def get(self, table, id, force_refresh=False, limit=100):
         id = extract_id(id)
         # look up the record in the current local dataset
         result = self._get(table, id)
         # if it's not found, try refreshing the record from the server
         if result is Missing or force_refresh:
             if table == "block":
-                self.call_load_page_chunk(id)
+                self.call_load_page_chunk(id,limit=limit)
             else:
                 self.call_get_record_values(**{table: id})
             result = self._get(table, id)
@@ -269,7 +269,7 @@ def get_current_version(self, table, id):
         else:
             return -1
 
-    def call_load_page_chunk(self, page_id):
+    def call_load_page_chunk(self, page_id, limit=100):
 
         if self._client.in_transaction():
             self._pages_to_refresh.append(page_id)
@@ -277,7 +277,7 @@ def call_load_page_chunk(self, page_id):
 
         data = {
             "pageId": page_id,
-            "limit": 100000,
+            "limit": limit,
             "cursor": {"stack": []},
             "chunkNumber": 0,
             "verticalColumns": False,
@@ -310,6 +310,7 @@ def call_query_collection(
         sort=[],
         calendar_by="",
         group_by="",
+        limit=50
     ):
 
         assert not (
@@ -326,7 +327,7 @@ def call_query_collection(
             "collectionId": collection_id,
             "collectionViewId": collection_view_id,
             "loader": {
-                "limit": 10000,
+                "limit": limit,
                 "loadContentCover": True,
                 "searchQuery": search,
                 "userLocale": "en",

From 4fabfb3151129bd2f3123e66341c79d533660db8 Mon Sep 17 00:00:00 2001
From: c0j0s <c.junsheng@hotmail.com>
Date: Tue, 9 Mar 2021 23:20:26 +0800
Subject: [PATCH 2/6] Add additional query to fetch table total size

---
 notion/collection.py | 37 +++++++++++++++++++++++++------------
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/notion/collection.py b/notion/collection.py
index fc1de31d..912332c2 100644
--- a/notion/collection.py
+++ b/notion/collection.py
@@ -382,20 +382,33 @@ def execute(self):
 
         result_class = QUERY_RESULT_TYPES.get(self.type, QueryResult)
 
+        kwargs = {
+            'collection_id':self.collection.id,
+            'collection_view_id':self.collection_view.id,
+            'search':self.search,
+            'type':self.type,
+            'aggregate':self.aggregate,
+            'aggregations':self.aggregations,
+            'filter':self.filter,
+            'sort':self.sort,
+            'calendar_by':self.calendar_by,
+            'group_by':self.group_by,
+            'limit':0
+        }
+
+        if self.limit == -1:
+            # fetch remote total 
+            result = self._client.query_collection(
+                **kwargs
+            )
+            self.limit = result.get("total",-1)
+
+        kwargs['limit'] = self.limit
+
         return result_class(
             self.collection,
             self._client.query_collection(
-                collection_id=self.collection.id,
-                collection_view_id=self.collection_view.id,
-                search=self.search,
-                type=self.type,
-                aggregate=self.aggregate,
-                aggregations=self.aggregations,
-                filter=self.filter,
-                sort=self.sort,
-                calendar_by=self.calendar_by,
-                group_by=self.group_by,
-                limit= self.limit
+                **kwargs
             ),
             self,
         )
@@ -707,6 +720,7 @@ def __init__(self, collection, result, query):
         self.collection = collection
         self._client = collection._client
         self._block_ids = self._get_block_ids(result)
+        self.total = result.get("total", -1)
         self.aggregates = result.get("aggregationResults", [])
         self.aggregate_ids = [
             agg.get("id") for agg in (query.aggregate or query.aggregations)
@@ -757,7 +771,6 @@ def __contains__(self, item):
             return False
         return item_id in self._block_ids
 
-
 class TableQueryResult(QueryResult):
 
     _type = "table"

From 43bf14459c8be0a5183dac28afb0fb55a7f23401 Mon Sep 17 00:00:00 2001
From: wsykala <38567820+wsykala@users.noreply.github.com>
Date: Thu, 7 Oct 2021 08:06:30 +0200
Subject: [PATCH 3/6] Fix API request/response for queryCollection

---
 notion/collection.py |  2 +-
 notion/store.py      | 29 +++++++++++++++++------------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/notion/collection.py b/notion/collection.py
index 912332c2..12eab046 100644
--- a/notion/collection.py
+++ b/notion/collection.py
@@ -728,7 +728,7 @@ def __init__(self, collection, result, query):
         self.query = query
 
     def _get_block_ids(self, result):
-        return result["blockIds"]
+        return result['reducerResults']['collection_group_results']["blockIds"]
 
     def _get_block(self, id):
         block = CollectionRowBlock(self._client, id)
diff --git a/notion/store.py b/notion/store.py
index b50d5003..79fffa6b 100644
--- a/notion/store.py
+++ b/notion/store.py
@@ -324,21 +324,26 @@ def call_query_collection(
             sort = [sort]
 
         data = {
-            "collectionId": collection_id,
-            "collectionViewId": collection_view_id,
+            "collection": {
+                "id": collection_id,
+                "spaceId": self._client.current_space.id
+            },
+            "collectionView": {
+                "id": collection_view_id,
+                "spaceId": self._client.current_space.id
+            },
             "loader": {
-                "limit": limit,
-                "loadContentCover": True,
+                'filter': filter,
+                'reducers': {
+                    'collection_group_results': {
+                        'limit': limit,
+                        'type': 'results',
+                    },
+                },
                 "searchQuery": search,
-                "userLocale": "en",
+                'sort': sort,
                 "userTimeZone": str(get_localzone()),
-                "type": type,
-            },
-            "query": {
-                "aggregate": aggregate,
-                "aggregations": aggregations,
-                "filter": filter,
-                "sort": sort,
+                "type": 'reducer',
             },
         }
 

From 37e04f8f98e82ac408f3655a44b5641faa32fea7 Mon Sep 17 00:00:00 2001
From: wsykala <38567820+wsykala@users.noreply.github.com>
Date: Wed, 16 Feb 2022 19:50:48 +0100
Subject: [PATCH 4/6] Fix for removed filter parameter

---
 notion/store.py | 1 -
 1 file changed, 1 deletion(-)

diff --git a/notion/store.py b/notion/store.py
index 79fffa6b..84421d94 100644
--- a/notion/store.py
+++ b/notion/store.py
@@ -333,7 +333,6 @@ def call_query_collection(
                 "spaceId": self._client.current_space.id
             },
             "loader": {
-                'filter': filter,
                 'reducers': {
                     'collection_group_results': {
                         'limit': limit,

From d9cec401789b23b434f08da86fee207bf808127f Mon Sep 17 00:00:00 2001
From: Yuxin Chen <chenyuxin.mail@gmail.com>
Date: Thu, 21 Apr 2022 01:54:30 -0500
Subject: [PATCH 5/6] add x-notion-active-user-header in request headers

---
 notion/client.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/notion/client.py b/notion/client.py
index b49cd779..1a46a757 100644
--- a/notion/client.py
+++ b/notion/client.py
@@ -5,7 +5,7 @@
 
 from requests import Session, HTTPError
 from requests.cookies import cookiejar_from_dict
-from urllib.parse import urljoin
+from urllib.parse import unquote, urljoin
 from requests.adapters import HTTPAdapter
 from requests.packages.urllib3.util.retry import Retry
 from getpass import getpass
@@ -53,6 +53,7 @@ def create_session(client_specified_retry=None):
         )
     adapter = HTTPAdapter(max_retries=retry)
     session.mount("https://", adapter)
+    session.headers.update({"content-type": "application/json"})
     return session
 
 
@@ -130,6 +131,10 @@ def _update_user_info(self):
         self._store.store_recordmap(records)
         self.current_user = self.get_user(list(records["notion_user"].keys())[0])
         self.current_space = self.get_space(list(records["space"].keys())[0])
+
+        self.session.headers.update({"x-notion-active-user-header": 
+            json.loads(unquote(self.session.cookies.get("notion_users")))[1]})
+        
         return records
 
     def get_email_uid(self):
@@ -140,7 +145,6 @@ def get_email_uid(self):
         }
 
     def set_user_by_uid(self, user_id):
-        self.session.headers.update({"x-notion-active-user-header": user_id})
         self._update_user_info()
 
     def set_user_by_email(self, email):

From 19f728531de92907dd49f6484df9f65cae893073 Mon Sep 17 00:00:00 2001
From: Yuxin Chen <chenyuxin.mail@gmail.com>
Date: Thu, 21 Apr 2022 01:54:43 -0500
Subject: [PATCH 6/6] fix page id

---
 notion/store.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/notion/store.py b/notion/store.py
index 84421d94..59fe76fc 100644
--- a/notion/store.py
+++ b/notion/store.py
@@ -276,7 +276,9 @@ def call_load_page_chunk(self, page_id, limit=100):
             return
 
         data = {
-            "pageId": page_id,
+            "page": {
+                "id": page_id,
+            },
             "limit": limit,
             "cursor": {"stack": []},
             "chunkNumber": 0,