w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python Requests: Hook or no Hook?

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.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.