Skip to content
Advertisement

Python Requests: Hook or no Hook?

I ‘.get‘ a request and process the response like:

resp = requests.get('url')
resp = resp.text
.. # do stuff with resp

After reading the package’s docs, I saw that there is a hook functionality which would permit me to do:

def process_r(resp, **kwargs): .. do stuff with resp

resp = requests.get('url', hooks = {'response': process_r})

My questions:

When should I use hooks? Or, why should I use hooks?

I wish to initiate an object (a parser) after a request’s response is returned using the requests resp.text procedure.

What is the Pythonic, correct approach here for this scenario?

Thank you

Advertisement

Answer

Hooks are not a million miles from ‘magic’. They have the effect of making your code potentially do things that will surprise other people (thereby violating “Explicit is better than implicit”).

Hooks should really therefore only be used to drive behaviours that will make things more predictable, not less. For example, Requests uses them internally to handle 401 responses for various kinds of authentication.

You should therefore be guided by the restrictions on hooks. The relevant part of the documentation states that hooks need to return a Response object. This leads to a few obvious possible behaviours: you could make additional requests (like the 401 hook above does), or you could mutate the Response in some way.

Initiating a parser is exactly the kind of thing that you shouldn’t do with a hook. It should be part of your business logic. I would write a utility function instead.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement