Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

bug 642863: allow caller-specified headers to be passed to the services #6

Merged
merged 2 commits into from

1 participant

Mark Hammond
Mark Hammond
Owner

No description provided.

Mark Hammond mhammond merged commit e763c4a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 21, 2011
  1. Mark Hammond
Commits on Apr 27, 2011
  1. Mark Hammond

    merge branch 'master'

    mhammond authored
This page is out of date. Refresh to see the latest.
10 linkoauth/backends/__init__.py
View
@@ -166,13 +166,13 @@ def __updated(self, domain, *args, **kw):
return __updated
@_updated
- def sendmessage(self, domain, account, message, options={}, **kw):
- return get_requester(domain, account).sendmessage(message, options,
- **kw)
+ def sendmessage(self, domain, account, *args, **kw):
+ return get_requester(domain, account).sendmessage(*args, **kw)
@_updated
- def getcontacts(self, domain, account, page_data=None, **kw):
- return get_requester(domain, account, **kw).getcontacts(page_data)
+ def getcontacts(self, domain, account, page_data, headers, **kw):
+ return get_requester(domain, account, **kw).getcontacts(page_data,
+ headers)
def request_access(self, domain, request, url, session, **kw):
return get_responder(domain, **kw).request_access(request, url,
20 linkoauth/backends/facebook_.py
View
@@ -236,16 +236,16 @@ def _make_error(self, client, data, resp):
'status': status})
return error
- def rawcall(self, url, body=None, method="GET", params=None):
+ def rawcall(self, url, body=None, method="GET", params=None, headers=None):
if params is None:
params = {}
params['access_token'] = self.access_token
url = url + "?" + urllib.urlencode(params)
- headers = None
+ headers = headers or {}
if body:
content_type, body = encode_multipart_formdata(body)
- headers = {'Content-type': content_type,
- 'Content-Length': str(len(body))}
+ headers.update({'Content-type': content_type,
+ 'Content-Length': str(len(body))})
client = HttpRequestor()
resp, content = client.request(url, method=method, headers=headers,
@@ -281,9 +281,7 @@ def rawcall(self, url, body=None, method="GET", params=None):
'caption': 'caption',
'source': 'source'}
- def sendmessage(self, message, options=None):
- if options is None:
- options = {}
+ def sendmessage(self, message, options, headers):
share_type = options.get('shareType', None)
if share_type == 'groupWall':
direct = options.get('to', None)
@@ -300,11 +298,9 @@ def sendmessage(self, message, options=None):
if ours in options:
body[yours] = options[ours]
- return self.rawcall(url, body, "POST")
+ return self.rawcall(url, body, "POST", headers=headers)
- def getcontacts(self, options=None):
- if options is None:
- options = {}
+ def getcontacts(self, options, headers):
offset = int(options.get('offset', 0))
limit = int(options.get('limit', 25))
type_ = options.get('type', 'groups')
@@ -315,7 +311,7 @@ def getcontacts(self, options=None):
}
# using 'friends' would turn this into posting to a friends wall.
url = "https://graph.facebook.com/me/%s" % type_
- result, error = self.rawcall(url, params=params)
+ result, error = self.rawcall(url, params=params, headers=headers)
if error:
return result, error
14 linkoauth/backends/google_.py
View
@@ -333,7 +333,7 @@ def __init__(self, account):
def get_name(cls):
return domain
- def sendmessage(self, message, options=None):
+ def sendmessage(self, message, options, headers):
if options is None:
options = {}
result = error = None
@@ -505,11 +505,11 @@ def sendmessage(self, message, options=None):
return result, error
- def getgroup_id(self, group):
+ def getgroup_id(self, group, headers):
url = 'https://www.google.com/m8/feeds/groups/default/full?v=2'
method = 'GET'
client = oauth.Client(self.consumer, self.oauth_token)
- resp, content = client.request(url, method)
+ resp, content = client.request(url, method, headers=headers)
feed = gdata.contacts.GroupsFeedFromString(content)
for entry in feed.entry:
this_group = entry.content.text
@@ -518,9 +518,7 @@ def getgroup_id(self, group):
if group == this_group:
return entry.id.text
- def getcontacts(self, options=None):
- if options is None:
- options = {}
+ def getcontacts(self, options, headers):
start = int(options.get('start', 0))
page = int(options.get('page', 25))
group = options.get('group', None)
@@ -547,7 +545,7 @@ def getcontacts(self, options=None):
if start > 0:
url = url + "&start-index=%d" % (start,)
if group:
- gid = self.getgroup_id(group)
+ gid = self.getgroup_id(group, headers)
if not gid:
error = {"provider": domain,
"message": "Group '%s' not available" % group}
@@ -556,7 +554,7 @@ def getcontacts(self, options=None):
# itemsPerPage, startIndex, totalResults
requestor = OAuth2Requestor(self.consumer, self.oauth_token)
- resp, content = requestor.request(url, method)
+ resp, content = requestor.request(url, method, headers=headers)
if int(resp.status) != 200:
requestor.save_capture("contact fetch failure")
14 linkoauth/backends/linkedin_.py
View
@@ -119,9 +119,9 @@ def __init__(self, account):
def get_name(cls):
return domain
- def rawcall(self, url, body=None, method="GET"):
+ def rawcall(self, url, body=None, method="GET", headers=None):
client = OAuth2Requestor(self.consumer, self.oauth_token)
- headers = {}
+ headers = headers or {}
headers['x-li-format'] = 'json'
if body is not None:
@@ -143,7 +143,7 @@ def rawcall(self, url, body=None, method="GET"):
return result, error
- def sendmessage(self, message, options=None):
+ def sendmessage(self, message, options, headers):
if options is None:
options = {}
share_type = str(options.get('shareType', ''))
@@ -224,11 +224,9 @@ def sendmessage(self, message, options=None):
'subject': subject,
'body': text_message}
- return self.rawcall(url, body, method="POST")
+ return self.rawcall(url, body, method="POST", headers=headers)
- def getcontacts(self, options=None):
- if options is None:
- options = {}
+ def getcontacts(self, options, headers):
start = int(options.get('start', 0))
page = int(options.get('page', 25))
contacts = []
@@ -236,7 +234,7 @@ def getcontacts(self, options=None):
if start > 0:
url = url + "&start=%d" % (start,)
- result, error = self.rawcall(url, method="GET")
+ result, error = self.rawcall(url, method="GET", headers=headers)
if error:
return result, error
16 linkoauth/backends/twitter_.py
View
@@ -194,14 +194,14 @@ def _make_error(self, client, data, resp):
'status': status})
return error
- def rawcall(self, url, params=None, method="GET"):
+ def rawcall(self, url, params=None, method="GET", headers=None):
client = OAuth2Requestor(self.consumer, self.oauth_token)
if method == "POST":
body = urlencode(params)
else:
assert params is None
body = ''
- resp, content = client.request(url, method, body=body)
+ resp, content = client.request(url, method, body=body, headers=headers)
data = content and json.loads(content) or resp
@@ -213,9 +213,7 @@ def rawcall(self, url, params=None, method="GET"):
result = data
return result, error
- def sendmessage(self, message, options=None):
- if options is None:
- options = {}
+ def sendmessage(self, message, options, headers):
# insert the url if it is not already in the message
longurl = options.get('link')
shorturl = options.get('shorturl')
@@ -248,15 +246,13 @@ def sendmessage(self, message, options=None):
return None, {'code': 400,
'provider': domain,
'message': 'Share type is missing'}
- return self.rawcall(url, params=body, method="POST")
+ return self.rawcall(url, params=body, method="POST", headers=headers)
def profile(self):
url = 'https://api.twitter.com/1/account/verify_credentials.json'
return self.rawcall(url)
- def getcontacts(self, options=None):
- if options is None:
- options = {}
+ def getcontacts(self, options, headers):
cursor = int(options.get('cursor', -1))
url = ('https://api.twitter.com/1/statuses/followers.json'
'?screen_name=%s&cursor=%s'
@@ -265,7 +261,7 @@ def getcontacts(self, options=None):
# since this data is used for direct messaging
contacts = []
- data, error = self.rawcall(url)
+ data, error = self.rawcall(url, headers=headers)
if error:
return None, error
for follower in data.get('users', []):
32 linkoauth/backends/yahoo_.py
View
@@ -150,12 +150,12 @@ def _maybe_throw_response_exception(self, resp, content):
if status >= 500:
raise ServiceUnavailableException(debug_message=content)
- def jsonrpc(self, url, method, args, options=None):
- if options is None:
- options = {}
- headers = {'Content-Type': 'application/json',
- 'Accept': 'application/json'}
-
+ def jsonrpc(self, url, method, args, options=None, headers=None):
+ headers = headers or {}
+ headers.update({
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json'
+ })
if options.get('HumanVerification'):
headers['X-HumanVerification-ImageUrl'] = \
options.get('HumanVerificationImage')
@@ -207,10 +207,12 @@ def jsonrpc(self, url, method, args, options=None):
return result, error
- def restcall(self, url, method="GET", body=None, params=None):
- headers = {
+ def restcall(self, url, method="GET", body=None, params=None, headers=None):
+ headers = headers or {}
+ headers.update({
'Content-Type': 'application/json',
- 'Accept': 'application/json'}
+ 'Accept': 'application/json'
+ })
if params:
url = url + "?" + urllib.urlencode(params)
@@ -243,9 +245,7 @@ def restcall(self, url, method="GET", body=None, params=None):
return result, error
- def sendmessage(self, message, options=None):
- if options is None:
- options = {}
+ def sendmessage(self, message, options, headers):
profile = self.account.get('profile', {})
from_ = profile.get('verifiedEmail')
fullname = profile.get('displayName', None)
@@ -321,11 +321,9 @@ def sendmessage(self, message, options=None):
"savecopy":1}]
return self.jsonrpc(self.endpoints['mail'],
- 'SendMessage', params, options)
+ 'SendMessage', params, options, headers)
- def getcontacts(self, options=None):
- if options is None:
- options = {}
+ def getcontacts(self, options, headers):
profile = self.account.get('profile', {})
guid = profile.get('xoauth_yahoo_guid')
params = {
@@ -334,7 +332,7 @@ def getcontacts(self, options=None):
}
result, error = self.restcall(self.endpoints['contacts'] % (guid,),
- params=params)
+ params=params, headers=headers)
if error:
return result, error
ycontacts = result.get('contacts')
6 linkoauth/tests/invalid_requests/test_facebook.py
View
@@ -23,15 +23,15 @@ def check_error(self, res, error, expected_code=400):
self.assertEqual(error['code'], expected_code)
def test_no_share_type(self):
- res, error = self.requester.sendmessage('', self.get_args())
+ res, error = self.requester.sendmessage('', self.get_args(), None)
self.check_error(res, error)
def test_invalid_share_type(self):
res, error = self.requester.sendmessage(
- '', self.get_args(shareType="invalid"))
+ '', self.get_args(shareType="invalid"), None)
self.check_error(res, error)
def test_no_wall_name(self):
args = self.get_args(shareType="groupWall")
- res, error = self.requester.sendmessage('', args)
+ res, error = self.requester.sendmessage('', args, None)
self.check_error(res, error)
10 linkoauth/tests/invalid_requests/test_linkedin.py
View
@@ -25,25 +25,25 @@ def check_error(self, res, error, expected_code=400):
self.assertEqual(error['code'], expected_code)
def test_no_share_type(self):
- res, error = self.requester.sendmessage('', self.get_args())
+ res, error = self.requester.sendmessage('', self.get_args(), None)
self.check_error(res, error)
def test_invalid_share_type(self):
res, error = self.requester.sendmessage(
- '', self.get_args(shareType="invalid"))
+ '', self.get_args(shareType="invalid"), None)
self.check_error(res, error)
def test_invalid_public_share_type(self):
args = self.get_args(shareType="public", to="invalid")
- res, error = self.requester.sendmessage('', args)
+ res, error = self.requester.sendmessage('', args, None)
self.check_error(res, error)
def test_invalid_connections_share_type(self):
args = self.get_args(shareType="myConnections", to="invalid")
- res, error = self.requester.sendmessage('', args)
+ res, error = self.requester.sendmessage('', args, None)
self.check_error(res, error)
def test_contact_no_to(self):
args = self.get_args(shareType='contact', to='')
- res, error = self.requester.sendmessage('', args)
+ res, error = self.requester.sendmessage('', args, None)
self.check_error(res, error)
6 linkoauth/tests/invalid_requests/test_twitter.py
View
@@ -25,15 +25,15 @@ def check_error(self, res, error, expected_code=400):
self.assertEqual(error['code'], expected_code)
def test_no_share_type(self):
- res, error = self.requester.sendmessage('', self.get_args())
+ res, error = self.requester.sendmessage('', self.get_args(), None)
self.check_error(res, error)
def test_invalid_share_type(self):
res, error = self.requester.sendmessage(
- '', self.get_args(shareType="invalid"))
+ '', self.get_args(shareType="invalid"), None)
self.check_error(res, error)
def test_direct_no_to(self):
args = self.get_args(shareType='direct', to='')
- res, error = self.requester.sendmessage('', args)
+ res, error = self.requester.sendmessage('', args, None)
self.check_error(res, error)
2  linkoauth/tests/test_base.py
View
@@ -153,7 +153,7 @@ def test_disabled(self):
services = Services(['google.com'], feedback_enabled=False)
services.initialize('google.com')
res, error = services.sendmessage('google.com', _ACCOUNT,
- message, args)
+ message, args, None)
status = services.get_status('google.com')
self.assertEquals(status, (True, 0, 0))
Something went wrong with that request. Please try again.