Search code examples
pythoneve

Easiest way to find if a GET request does not find a resource


In the python-eve framework, what would be the easiest way to find in a post GET hook that the GET request failed to find any resources (for example, the given filtering parameters failed to match any resources)?

Thanks!


Solution

  • Since payload is a Flask Response object, you can take profit of its features. One option would be to simply investigate the _items key, which, on a collection endpoint, returns the actual documents:

    import json
    
    def on_post_get(resoure, request, payload):
        # get the actual response json out of Flask Response
        json = json.loads(payload.get_data())
    
        documents = json['_items']
        assert(len(documents) == 0)
    
    app = Eve()
    app.on_post_GET += on_post_get
    
    if __name__ == '__main__':
        app.run()