Move pre_request to a more usable place, followup to #382
authorChristopher Grebs <cg@webshox.org>
Tue, 27 Mar 2012 13:38:56 +0000 (15:38 +0200)
committerChristopher Grebs <cg@webshox.org>
Tue, 27 Mar 2012 13:38:56 +0000 (15:38 +0200)
Having the hook called so late makes it impossible to overwrite the HTTP request
calling behavior and set self.sent to True in case of possible cache matches
for example.

This way we can interfere before we waste any CPU time to calculate
a request.

requests/models.py

index 881b7c1fc4e3dd243f7b283d1a6adbc7e8c988ff..dd73743c8146ee6c4462fd974f0c6d7778322425 100644 (file)
@@ -408,6 +408,10 @@ class Request(object):
         # Build the URL
         url = self.full_url
 
+        # Pre-request hook.
+        r = dispatch_hook('pre_request', self.hooks, self)
+        self.__dict__.update(r.__dict__)
+
         # Logging
         if self.config.get('verbose'):
             self.config.get('verbose').write('%s   %s   %s\n' % (
@@ -537,10 +541,6 @@ class Request(object):
                     # Attach Cookie header to request.
                     self.headers['Cookie'] = cookie_header
 
-            # Pre-request hook.
-            r = dispatch_hook('pre_request', self.hooks, self)
-            self.__dict__.update(r.__dict__)
-
             try:
                 # The inner try .. except re-raises certain exceptions as
                 # internal exception types; the outer suppresses exceptions