From 8dd3871639bdb0356abb68993958a4130476732e Mon Sep 17 00:00:00 2001 From: Robert Andrew Ditthardt Date: Tue, 20 Dec 2016 14:41:09 -0800 Subject: [PATCH] Use requests session --- easypost/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/easypost/__init__.py b/easypost/__init__.py index 6f496b5f..c644bbde 100644 --- a/easypost/__init__.py +++ b/easypost/__init__.py @@ -6,9 +6,9 @@ import types import re from six.moves.urllib.parse import urlencode, quote_plus, urlparse - from .version import VERSION + # use urlfetch as request_lib on google app engine, otherwise use requests request_lib = None try: @@ -18,6 +18,7 @@ try: import requests request_lib = 'requests' + requests_session = requests.Session() except ImportError: raise ImportError('EasyPost requires an up to date requests library. ' 'Update requests via "pip install -U requests" or ' @@ -308,7 +309,14 @@ def requests_request(self, method, abs_url, headers, params): "Please report to contact@easypost.com." % method) try: - result = requests.request(method, abs_url, headers=headers, data=data, timeout=60, verify=True) + result = requests_session.request( + method, + abs_url, + headers=headers, + data=data, + timeout=60, + verify=True, + ) http_body = result.text http_status = result.status_code except Exception as e: