6

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)
Paolo
  • 20,112
  • 21
  • 72
  • 113
Ved
  • 3,380
  • 6
  • 40
  • 51

2 Answers2

17

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)]})
Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
2

the response_hook function has to return a response-object. the simplest workaround would be to modify the respose object you get from the hook_factory calling the hook.

def response_hook(response, *request_args, **request_kwargs):
    # use factory_kwargs
    # etc.
    response.meta1='meta1' #add data 
    response.meta2='meta2'

    #etc.

    return response# or the modified response

return response_hook

hope this helps.