Skip to content

Commit 1f27b30

Browse files
authored
Fetch full space data for guest users
1 parent 5cc4ab7 commit 1f27b30

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

notion/client.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,33 @@ def __init__(
7777

7878
def start_monitoring(self):
7979
self._monitor.poll_async()
80+
81+
def _fetch_guest_space_data(self, records):
82+
"""
83+
guest users have an empty `space` dict, so get the space_id from the `space_view` dict instead,
84+
and fetch the space data from the getPublicSpaceData endpoint.
85+
86+
Note: This mutates the records dict
87+
"""
88+
space_id = list(records["space_view"].values())[0]["value"]["space_id"]
89+
90+
space_data = self.post(
91+
"getPublicSpaceData", {"type": "space-ids", "spaceIds": [space_id]}
92+
).json()
93+
94+
records["space"] = {
95+
space["id"]: {"value": space} for space in space_data["results"]
96+
}
97+
8098

8199
def _update_user_info(self):
82100
records = self.post("loadUserContent", {}).json()["recordMap"]
101+
if not records["space"]:
102+
self._fetch_guest_space_data(records)
103+
83104
self._store.store_recordmap(records)
84105
self.current_user = self.get_user(list(records["notion_user"].keys())[0])
85-
86-
try:
87-
self.current_space = self.get_space(list(records["space"].keys())[0])
88-
except:
89-
# if guest user, use fallback value for space_id
90-
self.current_space = self.get_space(
91-
list(records["space_view"].values())[0]["value"]["space_id"]
92-
)
106+
self.current_space = self.get_space(list(records["space"].keys())[0])
93107
return records
94108

95109
def get_email_uid(self):

0 commit comments

Comments
 (0)