Skip to content

Commit

Permalink
Merge mobolic#327 (add support for requests sessions).
Browse files Browse the repository at this point in the history
  • Loading branch information
martey committed Nov 3, 2016
2 parents 4bcff93 + 08f7e00 commit 1f39b05
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
==============================
Expand Down
21 changes: 11 additions & 10 deletions facebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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$")
Expand Down Expand Up @@ -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,
Expand All @@ -223,7 +224,6 @@ def request(
arguments.
"""
args = args or {}

if post_args is not None:
method = "POST"
Expand All @@ -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)
Expand Down

0 comments on commit 1f39b05

Please sign in to comment.