Skip to content

Commit

Permalink
create a Configuration.Session attribute
Browse files Browse the repository at this point in the history
This partially fixes #48 by allowing developers to benefit from connection reuse (at the expense of thread safety).
  • Loading branch information
Changaco committed Jul 27, 2016
1 parent 5430b15 commit 72ade75
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ You should find that file in `Python_installation_folder\Lib\site-packages\reque
sdk.Config.SSLVerification = 'C:\\Python27\\Lib\\site-packages\\requests\\cacert.pem'


Connection pooling
-------------------------------------------------
To benefit from HTTP connection reuse you need to set `api.Config.Session` like this:

import requests
api.Config.Session = requests.Session()

WARNING: sessions are not guaranteed to be thread-safe!

See the upstream [Session Objects](http://docs.python-requests.org/en/master/user/advanced/#session-objects) documentation for more details.


Sample usage
-------------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions mangopaysdk/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Configuration:
# NB: you can swap this class for one of ours that implement some custom logic
RestToolClass = None

# Session object from the `requests` library
Session = None


# we use DEBUG level for internal debugging
if (Configuration.DebugMode):
Expand Down
3 changes: 2 additions & 1 deletion mangopaysdk/tools/resttool.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self, root = None, authRequired = True):
self._root = root
self._debugMode = self._root.Config.DebugMode
self._sslVerification = self._root.Config.SSLVerification
self._session = self._root.Config.Session

def Request(self, urlMethod, requestType, requestData = None, pagination = None, additionalUrlParams = None):
"""Call request to MangoPay API.
Expand Down Expand Up @@ -107,7 +108,7 @@ def _generateRequestIdempotent(self, idempotencyKey, urlMethod, pagination, addi
def _sendRequest(self, request):
"""Prepare and send the request"""
prepared_request = request.prepare()
session = requests.Session()
session = self._session or requests.Session()
response = session.send(prepared_request, verify=self._sslVerification)
return response

Expand Down

0 comments on commit 72ade75

Please sign in to comment.