From 8920f1840fc19686a30d87be3b8fe80eae0ee5c2 Mon Sep 17 00:00:00 2001 From: tipishev Date: Wed, 11 Mar 2015 18:12:30 +0500 Subject: [PATCH 1/2] Use API-wide requests.Session Signed-off-by: Martey Dodoo --- facebook/__init__.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/facebook/__init__.py b/facebook/__init__.py index 3cc28373..f43fec65 100755 --- a/facebook/__init__.py +++ b/facebook/__init__.py @@ -78,13 +78,14 @@ class GraphAPI(object): """ def __init__(self, access_token=None, timeout=None, version=None, - proxies=None): + proxies=None, session=None): # The default version is only used if the version kwarg does not exist. default_version = VALID_API_VERSIONS[0] self.access_token = access_token self.timeout = timeout self.proxies = proxies + self.session = session or requests.Session() if version: version_regex = re.compile("^\d\.\d$") @@ -197,7 +198,7 @@ def get_version(self): """Fetches the current version number of the Graph API being used.""" args = {"access_token": self.access_token} try: - response = requests.request( + response = self.session.request( "GET", FACEBOOK_GRAPH_URL + self.version + "/me", params=args, @@ -223,7 +224,6 @@ def request( arguments. """ - args = args or {} if post_args is not None: method = "POST" @@ -239,13 +239,14 @@ def request( args["access_token"] = self.access_token try: - response = requests.request(method or "GET", - FACEBOOK_GRAPH_URL + path, - timeout=self.timeout, - params=args, - data=post_args, - proxies=self.proxies, - files=files) + response = self.session.request( + method or "GET", + FACEBOOK_GRAPH_URL + path, + timeout=self.timeout, + params=args, + data=post_args, + proxies=self.proxies, + files=files) except requests.HTTPError as e: response = json.loads(e.read()) raise GraphAPIError(response) From 08f7e0088c6290c2ffd44a31bf7e6f09d5559faa Mon Sep 17 00:00:00 2001 From: Martey Dodoo Date: Thu, 3 Nov 2016 01:44:19 -0400 Subject: [PATCH 2/2] Update documentation for sessions. --- docs/api.rst | 2 ++ docs/changes.rst | 1 + 2 files changed, 3 insertions(+) diff --git a/docs/api.rst b/docs/api.rst index 60c328c6..25b2386c 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -28,11 +28,13 @@ You can read more about `Facebook's Graph API here`_. use`_. The default version is the oldest current version. It is used if the version keyword argument is not provided. * ``proxies`` - A ``dict`` with proxy-settings that Requests should use. `See Requests documentation`_. +* ``session`` - A `Requests Session object`_. .. _Read more about access tokens here: https://developers.facebook.com/docs/facebook-login/access-tokens .. _See more here: http://docs.python-requests.org/en/latest/user/quickstart/#timeouts .. _version of Facebook's Graph API to use: https://developers.facebook.com/docs/apps/changelog#versions .. _See Requests documentation: http://www.python-requests.org/en/latest/user/advanced/#proxies +.. _Requests Session object: http://docs.python-requests.org/en/master/user/advanced/#session-objects **Example** diff --git a/docs/changes.rst b/docs/changes.rst index 28949539..a6eafc36 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -8,6 +8,7 @@ Version 3.0.0 (unreleased) - Add support for Graph API version 2.8. - Remove support for Graph API version 2.1. - Change default Graph API version to 2.2. + - Add support for requests' sessions. Version 2.0.0 (2016-08-08) ==============================