Skip to content
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

Fix query handling and example format in response to Notion changes #95

Merged
merged 2 commits into from
Sep 19, 2020

Conversation

jamalex
Copy link
Owner

@jamalex jamalex commented Jan 12, 2020

On Jan 9, Notion made changes to the API format for queries. This PR allows the new query format to be used, but manually constructed queries will need to be updated. See #94.

@kevinjalbert
Copy link
Contributor

👏 Awesome work @jamalex.

I tried out the branch for #96 (which includes this PR) and this does indeed fix the issue with specifying filters to match that of the new format.

I did notice that my default_query() for a collection_view was not updating when I made changes. 🤔 I decided to make a new view and give that a shot, then I was hit with:

  File "/usr/local/lib/python3.7/site-packages/notion/collection.py", line 223, in default_query
    return self.build_query(**self.get("query", {}))
  File "/usr/local/lib/python3.7/site-packages/notion/collection.py", line 219, in build_query
    collection=self.collection, collection_view=self, **kwargs
TypeError: __init__() got an unexpected keyword argument 'filter_operator'

I dug around and started inspecting the returned structure from Notion:

    def default_query(self):
+       print(self.get().keys())
        return self.build_query(**self.get("query", {}))

And saw the following surprise:

dict_keys(['id', 'version', 'type', 'name', 'format', 'parent_id', 'parent_table', 'alive', 'query2'])

Then I checked what I got with my original collection_view:

dict_keys(['id', 'version', 'type', 'name', 'query', 'format', 'parent_id', 'parent_table', 'alive', 'page_sort', 'query2'])

query2 😮

I applied the following change and things started working as expected:

    def default_query(self):
-       return self.build_query(**self.get("query", {}))
+       return self.build_query(**self.get("query2", {}))

TL;DR It seems with their API change, they phased out query as a field on the internal structure of the collection_view (for older views -- although they still exist), and now query2 is the field that is updated for the default_query.

@nachtwerk
Copy link

@jamalex as said Notion added some more changes it seems to the API. I tried going do the road @kevinjalbert suggested, but that openend a can of worms with aggregate and aggregations keywords that are mutually excluded (but by default, are both passed?)
Is there any help that you need?

jean added a commit to jean/notion-py that referenced this pull request Feb 1, 2020
If it's likely that jamalex#95 will go unmerged for a while, it's worthwhile giving new adopters a heads-up.
kevinjalbert added a commit to kevinjalbert/notion-py that referenced this pull request Feb 29, 2020
kevinjalbert added a commit to kevinjalbert/notion-toolbox that referenced this pull request Feb 29, 2020
My own fork of notion-py incorporate:
 - jamalex/notion-py#96
 - jamalex/notion-py#95
 - some small modifications that took the PRs to the finish line for me

This ends up fixing the broken issues with the recent issues:
 - jamalex/notion-py#92
 - jamalex/notion-py#94

I am able to remove some of the 'hack' code I put in recently.

The filter_params for queries had to be reworked to match the new
format.
@jamalex jamalex merged commit 02ea1e3 into master Sep 19, 2020
@jamalex
Copy link
Owner Author

jamalex commented Sep 19, 2020

It looks as if they renamed "query2" back to "query", so this works again. :) I've merged and and will continue doing further testing (and merging of subsequent PRs) on it before releasing.

@jamalex jamalex deleted the 94 branch September 19, 2020 19:31
ChicK00o pushed a commit to ChicK00o/notion-toolbox that referenced this pull request May 12, 2021
My own fork of notion-py incorporate:
 - jamalex/notion-py#96
 - jamalex/notion-py#95
 - some small modifications that took the PRs to the finish line for me

This ends up fixing the broken issues with the recent issues:
 - jamalex/notion-py#92
 - jamalex/notion-py#94

I am able to remove some of the 'hack' code I put in recently.

The filter_params for queries had to be reworked to match the new
format.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants