Search code examples
python-2.7python-requestsgrequests

How to pass parameters to hooks in python grequests


According the Requests documentation, event hooks can be added to .get() function.

requests.get('http://httpbin.org', hooks=dict(response=print_url))
def print_url(r, *args, **kwargs):
    print(r.url)

This is fine but how to set *args with custom parameters, for example, I want to pass some custom values to print_url(), how to set those in *args ? Something like this fails :

args = ("search_item", search_item)
rs = (grequests.get(u, hooks={'response': [parse_books],'args': [args]}) for u in urls)

Solution

  • You cannot specify extra arguments to pass to a response hook. If you want extra information to be specified you should make a function that you call which then returns a function to be passed as a hook, e.g.,

    def hook_factory(*factory_args, **factory_kwargs):
        def response_hook(response, *request_args, **request_kwargs):
            # use factory_kwargs
            # etc.
            return None # or the modified response
        return response_hook
    
    grequests.get(u, hooks={'response': [hook_factory(search_item=search_item)]})