From 318300848f0ef96d603e25ae185e6f0a08fd9cc9 Mon Sep 17 00:00:00 2001 From: Chris Lee Date: Sun, 27 Jan 2013 19:01:39 -0800 Subject: [PATCH] Use timedelta and change attribute name back to elapsed --- requests/adapters.py | 8 ++++---- requests/models.py | 6 ++++-- test_requests.py | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index 55a8726..d4f5d5b 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -9,7 +9,7 @@ and maintain connections. """ import socket -import time +import datetime from .models import Response from .packages.urllib3.poolmanager import PoolManager, ProxyManager @@ -156,7 +156,7 @@ class HTTPAdapter(BaseAdapter): url = self.request_url(request, proxies) chunked = not (request.body is None or 'Content-Length' in request.headers) - start = time.time() + start = datetime.datetime.utcnow() try: if not chunked: @@ -200,7 +200,7 @@ class HTTPAdapter(BaseAdapter): preload_content=False, decode_content=False ) - finish = time.time() + finish = datetime.datetime.utcnow() except socket.error as sockerr: raise ConnectionError(sockerr) @@ -217,7 +217,7 @@ class HTTPAdapter(BaseAdapter): raise Timeout('Request timed out.') r = self.build_response(request, resp) - r.time_taken = finish - start + r.elapsed = finish - start if not stream: r.content diff --git a/requests/models.py b/requests/models.py index b3574b2..a9e8c5b 100644 --- a/requests/models.py +++ b/requests/models.py @@ -9,6 +9,7 @@ This module contains the primary objects that power Requests. import collections import logging +import datetime from io import BytesIO from .hooks import default_hooks @@ -471,8 +472,9 @@ class Response(object): #: A CookieJar of Cookies the server sent back. self.cookies = cookiejar_from_dict({}) - #: The amount of time taken for the response to arrive (in seconds). - self.time_taken = 0.0 + #: The amount of time elapsed between sending the request + #: and the arrival of the response (as a timedelta) + self.elapsed = datetime.timedelta(0) def __repr__(self): return '' % (self.status_code) diff --git a/test_requests.py b/test_requests.py index d18d386..d5409aa 100644 --- a/test_requests.py +++ b/test_requests.py @@ -323,9 +323,9 @@ class RequestsTestCase(unittest.TestCase): self.assertEqual(cookie.domain, domain) self.assertEqual(cookie._rest['HttpOnly'], rest['HttpOnly']) - def test_time_taken_blank(self): + def test_time_elapsed_blank(self): r = requests.get(httpbin('get')) - self.assertTrue(r.time_taken > 0.0) + self.assertTrue(r.elapsed.total_seconds() > 0.0) if __name__ == '__main__': -- 2.7.4