Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 242
Ved Avatar asked Aug 04 '14 09:08

Ved


Video Answer


2 Answers

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)]})
like image 64
Ian Stapleton Cordasco Avatar answered Oct 17 '22 07:10

Ian Stapleton Cordasco


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.

like image 31
user2888219 Avatar answered Oct 17 '22 05:10

user2888219