Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-app-usage response headers to result #427

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Version 3.0.0 (unreleased)
- Add search method (#362).
- Rename `auth_url` method to `get_auth_url` and move it into the Graph API
object (#377, #378, #422).
- Add `x-app-usage` Graph API response headers to result returned from
GraphAPI's request method

Version 2.0.0 (2016-08-08)
==========================
Expand Down
3 changes: 3 additions & 0 deletions facebook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ def request(
else:
raise GraphAPIError('Maintype was not text, image, or querystring')

if headers.get('x-app-usage'):
result['x-app-usage'] = json.loads(headers['x-app-usage'])

if result and isinstance(result, dict) and result.get("error"):
raise GraphAPIError(result)
return result
Expand Down
15 changes: 13 additions & 2 deletions test/test_facebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import inspect

try:
from urllib.parse import parse_qs, urlencode, urlparse
from urllib.parse import parse_qs, urlencode, urlparse, quote
except ImportError:
from urlparse import parse_qs, urlparse
from urllib import urlencode
from urllib import urlencode, quote


class FacebookTestCase(unittest.TestCase):
Expand Down Expand Up @@ -332,6 +332,17 @@ def test_request_access_tokens_are_unique_to_instances(self):
self.assertEqual(graph1.request.__defaults__[0], None)
self.assertEqual(graph2.request.__defaults__[0], None)

def test_request_result_has_x_app_usage_key(self):
"""Verify that result has x-app-usage key from response headers"""
# using website's url as ID
# https://developers.facebook.com/docs/graph-api/reference/v3.1/url
FB_OBJECT_ID = quote('http://www.example.com')
token = facebook.GraphAPI().get_app_access_token(
self.app_id, self.secret, True)
graph = facebook.GraphAPI(access_token=token)
result = graph.request(FB_OBJECT_ID)
self.assertIn('x-app-usage', result.keys())


class TestGetUserPermissions(FacebookTestCase):
"""
Expand Down