Repository: laterpay/laterpay-client-python

Issue:
Added support for `requests.Session`
Sometimes it's desirable to use [`requests.Session()`](http://docs.python-requests.org/en/master/user/advanced/#session-objects) over `requests.get()`.

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9257c1d..5da76e2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,9 @@
 
 ## 5.4.0 (under development)
 
+* Added the `connection_handler` to `LaterPayClient` to simplify connection
+  pooling.
+
 ## 5.3.1
 
 * Only passed one of `lptoken` and `muid` to `/access` calls. Passing both is
diff --git a/laterpay/__init__.py b/laterpay/__init__.py
index 6fb563a..45417c3 100644
--- a/laterpay/__init__.py
+++ b/laterpay/__init__.py
@@ -112,7 +112,8 @@ class LaterPayClient(object):
                  api_root='https://api.laterpay.net',
                  web_root='https://web.laterpay.net',
                  lptoken=None,
-                 timeout_seconds=10):
+                 timeout_seconds=10,
+                 connection_handler=None):
         """
         Instantiate a LaterPay API client.
 
@@ -122,6 +123,9 @@ class LaterPayClient(object):
 
         :param timeout_seconds: number of seconds after which backend api
             requests (e.g. /access) will time out (10 by default).
+        :param connection_handler: Defaults to Python requests. Set it to
+            ``requests.Session()`` to use a `Python requests Session object
+            <http://docs.python-requests.org/en/master/user/advanced/#session-objects>`_.
 
         """
         self.cp_key = cp_key
@@ -130,6 +134,7 @@ class LaterPayClient(object):
         self.shared_secret = shared_secret
         self.lptoken = lptoken
         self.timeout_seconds = timeout_seconds
+        self.connection_handler = connection_handler or requests
 
     def get_gettoken_redirect(self, return_to):
         """
@@ -408,9 +413,9 @@ class LaterPayClient(object):
         """
         Perform a request to /access API and return obtained data.
 
-        This method uses ``requests.get`` to fetch the data and then calls
-        ``.raise_for_status()`` on the response. It does not handle any errors
-        raised by ``requests`` API.
+        This method uses ``requests.get`` or ``requests.Session().get`` to
+        fetch the data and then calls ``.raise_for_status()`` on the response.
+        It does not handle any errors raised by ``requests`` API.
 
         :param article_ids: list of article ids or a single article id as a
                             string
@@ -421,7 +426,7 @@ class LaterPayClient(object):
         url = self.get_access_url()
         headers = self.get_request_headers()
 
-        response = requests.get(
+        response = self.connection_handler.get(
             url,
             params=params,
             headers=headers,


Test patch:
diff --git a/tests/test_client.py b/tests/test_client.py
index bc9e37f..f59f98b 100644
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -427,6 +427,34 @@ class TestLaterPayClient(unittest.TestCase):
             method='GET',
         )
 
+    @mock.patch('time.time')
+    def test_get_access_data_connection_handler(self, time_time_mock):
+        time_time_mock.return_value = 123
+        connection_handler = mock.Mock()
+        client = LaterPayClient(
+            'fake-cp-key',
+            'fake-shared-secret',
+            connection_handler=connection_handler,
+        )
+
+        client.get_access_data(
+            ['article-1', 'article-2'],
+            lptoken='fake-lptoken',
+        )
+
+        connection_handler.get.assert_called_once_with(
+            'https://api.laterpay.net/access',
+            headers=client.get_request_headers(),
+            params={
+                'article_id': ['article-1', 'article-2'],
+                'ts': '123',
+                'hmac': '198717d5c98b89ec3b509784758a98323f167ca6d42c363672169cfc',
+                'cp': 'fake-cp-key',
+                'lptoken': 'fake-lptoken',
+            },
+            timeout=10,
+        )
+
     @mock.patch('laterpay.signing.sign')
     @mock.patch('time.time')
     def test_get_access_params(self, time_time_mock, sign_mock):