Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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

Merged
merged 2 commits into from almost 3 years ago

1 participant

Mark Hammond
Mark Hammond
Collaborator

No description provided.

Mark Hammond mhammond merged commit e763c4a into from April 26, 2011
Mark Hammond mhammond closed this April 26, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
10  linkoauth/backends/__init__.py
@@ -166,13 +166,13 @@ def __updated(self, domain, *args, **kw):
166 166
         return __updated
167 167
 
168 168
     @_updated
169  
-    def sendmessage(self, domain, account, message, options={}, **kw):
170  
-        return get_requester(domain, account).sendmessage(message, options,
171  
-                                                          **kw)
  169
+    def sendmessage(self, domain, account, *args, **kw):
  170
+        return get_requester(domain, account).sendmessage(*args, **kw)
172 171
 
173 172
     @_updated
174  
-    def getcontacts(self, domain, account, page_data=None, **kw):
175  
-        return get_requester(domain, account, **kw).getcontacts(page_data)
  173
+    def getcontacts(self, domain, account, page_data, headers, **kw):
  174
+        return get_requester(domain, account, **kw).getcontacts(page_data,
  175
+                                                                headers)
176 176
 
177 177
     def request_access(self, domain, request, url, session, **kw):
178 178
         return get_responder(domain, **kw).request_access(request, url,
20  linkoauth/backends/facebook_.py
@@ -236,16 +236,16 @@ def _make_error(self, client, data, resp):
236 236
                       'status': status})
237 237
         return error
238 238
 
239  
-    def rawcall(self, url, body=None, method="GET", params=None):
  239
+    def rawcall(self, url, body=None, method="GET", params=None, headers=None):
240 240
         if params is None:
241 241
             params = {}
242 242
         params['access_token'] = self.access_token
243 243
         url = url + "?" + urllib.urlencode(params)
244  
-        headers = None
  244
+        headers = headers or {}
245 245
         if body:
246 246
             content_type, body = encode_multipart_formdata(body)
247  
-            headers = {'Content-type': content_type,
248  
-                       'Content-Length': str(len(body))}
  247
+            headers.update({'Content-type': content_type,
  248
+                            'Content-Length': str(len(body))})
249 249
 
250 250
         client = HttpRequestor()
251 251
         resp, content = client.request(url, method=method, headers=headers,
@@ -281,9 +281,7 @@ def rawcall(self, url, body=None, method="GET", params=None):
281 281
         'caption': 'caption',
282 282
         'source': 'source'}
283 283
 
284  
-    def sendmessage(self, message, options=None):
285  
-        if options is None:
286  
-            options = {}
  284
+    def sendmessage(self, message, options, headers):
287 285
         share_type = options.get('shareType', None)
288 286
         if share_type == 'groupWall':
289 287
             direct = options.get('to', None)
@@ -300,11 +298,9 @@ def sendmessage(self, message, options=None):
300 298
             if ours in options:
301 299
                 body[yours] = options[ours]
302 300
 
303  
-        return self.rawcall(url, body, "POST")
  301
+        return self.rawcall(url, body, "POST", headers=headers)
304 302
 
305  
-    def getcontacts(self, options=None):
306  
-        if options is None:
307  
-            options = {}
  303
+    def getcontacts(self, options, headers):
308 304
         offset = int(options.get('offset', 0))
309 305
         limit = int(options.get('limit', 25))
310 306
         type_ = options.get('type', 'groups')
@@ -315,7 +311,7 @@ def getcontacts(self, options=None):
315 311
         }
316 312
         # using 'friends' would turn this into posting to a friends wall.
317 313
         url = "https://graph.facebook.com/me/%s" % type_
318  
-        result, error = self.rawcall(url, params=params)
  314
+        result, error = self.rawcall(url, params=params, headers=headers)
319 315
         if error:
320 316
             return result, error
321 317
 
14  linkoauth/backends/google_.py
@@ -333,7 +333,7 @@ def __init__(self, account):
333 333
     def get_name(cls):
334 334
         return domain
335 335
 
336  
-    def sendmessage(self, message, options=None):
  336
+    def sendmessage(self, message, options, headers):
337 337
         if options is None:
338 338
             options = {}
339 339
         result = error = None
@@ -505,11 +505,11 @@ def sendmessage(self, message, options=None):
505 505
 
506 506
         return result, error
507 507
 
508  
-    def getgroup_id(self, group):
  508
+    def getgroup_id(self, group, headers):
509 509
         url = 'https://www.google.com/m8/feeds/groups/default/full?v=2'
510 510
         method = 'GET'
511 511
         client = oauth.Client(self.consumer, self.oauth_token)
512  
-        resp, content = client.request(url, method)
  512
+        resp, content = client.request(url, method, headers=headers)
513 513
         feed = gdata.contacts.GroupsFeedFromString(content)
514 514
         for entry in feed.entry:
515 515
             this_group = entry.content.text
@@ -518,9 +518,7 @@ def getgroup_id(self, group):
518 518
             if group == this_group:
519 519
                 return entry.id.text
520 520
 
521  
-    def getcontacts(self, options=None):
522  
-        if options is None:
523  
-            options = {}
  521
+    def getcontacts(self, options, headers):
524 522
         start = int(options.get('start', 0))
525 523
         page = int(options.get('page', 25))
526 524
         group = options.get('group', None)
@@ -547,7 +545,7 @@ def getcontacts(self, options=None):
547 545
         if start > 0:
548 546
             url = url + "&start-index=%d" % (start,)
549 547
         if group:
550  
-            gid = self.getgroup_id(group)
  548
+            gid = self.getgroup_id(group, headers)
551 549
             if not gid:
552 550
                 error = {"provider": domain,
553 551
                          "message": "Group '%s' not available" % group}
@@ -556,7 +554,7 @@ def getcontacts(self, options=None):
556 554
 
557 555
         # itemsPerPage, startIndex, totalResults
558 556
         requestor = OAuth2Requestor(self.consumer, self.oauth_token)
559  
-        resp, content = requestor.request(url, method)
  557
+        resp, content = requestor.request(url, method, headers=headers)
560 558
 
561 559
         if int(resp.status) != 200:
562 560
             requestor.save_capture("contact fetch failure")
14  linkoauth/backends/linkedin_.py
@@ -119,9 +119,9 @@ def __init__(self, account):
119 119
     def get_name(cls):
120 120
         return domain
121 121
 
122  
-    def rawcall(self, url, body=None, method="GET"):
  122
+    def rawcall(self, url, body=None, method="GET", headers=None):
123 123
         client = OAuth2Requestor(self.consumer, self.oauth_token)
124  
-        headers = {}
  124
+        headers = headers or {}
125 125
         headers['x-li-format'] = 'json'
126 126
 
127 127
         if body is not None:
@@ -143,7 +143,7 @@ def rawcall(self, url, body=None, method="GET"):
143 143
 
144 144
         return result, error
145 145
 
146  
-    def sendmessage(self, message, options=None):
  146
+    def sendmessage(self, message, options, headers):
147 147
         if options is None:
148 148
             options = {}
149 149
         share_type = str(options.get('shareType', ''))
@@ -224,11 +224,9 @@ def sendmessage(self, message, options=None):
224 224
                 'subject': subject,
225 225
                 'body': text_message}
226 226
 
227  
-        return self.rawcall(url, body, method="POST")
  227
+        return self.rawcall(url, body, method="POST", headers=headers)
228 228
 
229  
-    def getcontacts(self, options=None):
230  
-        if options is None:
231  
-            options = {}
  229
+    def getcontacts(self, options, headers):
232 230
         start = int(options.get('start', 0))
233 231
         page = int(options.get('page', 25))
234 232
         contacts = []
@@ -236,7 +234,7 @@ def getcontacts(self, options=None):
236 234
         if start > 0:
237 235
             url = url + "&start=%d" % (start,)
238 236
 
239  
-        result, error = self.rawcall(url, method="GET")
  237
+        result, error = self.rawcall(url, method="GET", headers=headers)
240 238
         if error:
241 239
             return result, error
242 240
 
16  linkoauth/backends/twitter_.py
@@ -194,14 +194,14 @@ def _make_error(self, client, data, resp):
194 194
                       'status': status})
195 195
         return error
196 196
 
197  
-    def rawcall(self, url, params=None, method="GET"):
  197
+    def rawcall(self, url, params=None, method="GET", headers=None):
198 198
         client = OAuth2Requestor(self.consumer, self.oauth_token)
199 199
         if method == "POST":
200 200
             body = urlencode(params)
201 201
         else:
202 202
             assert params is None
203 203
             body = ''
204  
-        resp, content = client.request(url, method, body=body)
  204
+        resp, content = client.request(url, method, body=body, headers=headers)
205 205
 
206 206
         data = content and json.loads(content) or resp
207 207
 
@@ -213,9 +213,7 @@ def rawcall(self, url, params=None, method="GET"):
213 213
             result = data
214 214
         return result, error
215 215
 
216  
-    def sendmessage(self, message, options=None):
217  
-        if options is None:
218  
-            options = {}
  216
+    def sendmessage(self, message, options, headers):
219 217
         # insert the url if it is not already in the message
220 218
         longurl = options.get('link')
221 219
         shorturl = options.get('shorturl')
@@ -248,15 +246,13 @@ def sendmessage(self, message, options=None):
248 246
             return None, {'code': 400,
249 247
                           'provider': domain,
250 248
                           'message': 'Share type is missing'}
251  
-        return self.rawcall(url, params=body, method="POST")
  249
+        return self.rawcall(url, params=body, method="POST", headers=headers)
252 250
 
253 251
     def profile(self):
254 252
         url = 'https://api.twitter.com/1/account/verify_credentials.json'
255 253
         return self.rawcall(url)
256 254
 
257  
-    def getcontacts(self, options=None):
258  
-        if options is None:
259  
-            options = {}
  255
+    def getcontacts(self, options, headers):
260 256
         cursor = int(options.get('cursor', -1))
261 257
         url = ('https://api.twitter.com/1/statuses/followers.json'
262 258
                '?screen_name=%s&cursor=%s'
@@ -265,7 +261,7 @@ def getcontacts(self, options=None):
265 261
         # since this data is used for direct messaging
266 262
         contacts = []
267 263
 
268  
-        data, error = self.rawcall(url)
  264
+        data, error = self.rawcall(url, headers=headers)
269 265
         if error:
270 266
             return None, error
271 267
         for follower in data.get('users', []):
32  linkoauth/backends/yahoo_.py
@@ -150,12 +150,12 @@ def _maybe_throw_response_exception(self, resp, content):
150 150
         if status >= 500:
151 151
             raise ServiceUnavailableException(debug_message=content)
152 152
 
153  
-    def jsonrpc(self, url, method, args, options=None):
154  
-        if options is None:
155  
-            options = {}
156  
-        headers = {'Content-Type': 'application/json',
157  
-                   'Accept': 'application/json'}
158  
-
  153
+    def jsonrpc(self, url, method, args, options=None, headers=None):
  154
+        headers = headers or {}
  155
+        headers.update({
  156
+            'Content-Type': 'application/json',
  157
+            'Accept': 'application/json'
  158
+        })
159 159
         if options.get('HumanVerification'):
160 160
             headers['X-HumanVerification-ImageUrl'] = \
161 161
                     options.get('HumanVerificationImage')
@@ -207,10 +207,12 @@ def jsonrpc(self, url, method, args, options=None):
207 207
 
208 208
         return result, error
209 209
 
210  
-    def restcall(self, url, method="GET", body=None, params=None):
211  
-        headers = {
  210
+    def restcall(self, url, method="GET", body=None, params=None, headers=None):
  211
+        headers = headers or {}
  212
+        headers.update({
212 213
             'Content-Type': 'application/json',
213  
-            'Accept': 'application/json'}
  214
+            'Accept': 'application/json'
  215
+        })
214 216
 
215 217
         if params:
216 218
             url = url + "?" + urllib.urlencode(params)
@@ -243,9 +245,7 @@ def restcall(self, url, method="GET", body=None, params=None):
243 245
 
244 246
         return result, error
245 247
 
246  
-    def sendmessage(self, message, options=None):
247  
-        if options is None:
248  
-            options = {}
  248
+    def sendmessage(self, message, options, headers):
249 249
         profile = self.account.get('profile', {})
250 250
         from_ = profile.get('verifiedEmail')
251 251
         fullname = profile.get('displayName', None)
@@ -321,11 +321,9 @@ def sendmessage(self, message, options=None):
321 321
                 "savecopy":1}]
322 322
 
323 323
         return self.jsonrpc(self.endpoints['mail'],
324  
-                            'SendMessage', params, options)
  324
+                            'SendMessage', params, options, headers)
325 325
 
326  
-    def getcontacts(self, options=None):
327  
-        if options is None:
328  
-            options = {}
  326
+    def getcontacts(self, options, headers):
329 327
         profile = self.account.get('profile', {})
330 328
         guid = profile.get('xoauth_yahoo_guid')
331 329
         params = {
@@ -334,7 +332,7 @@ def getcontacts(self, options=None):
334 332
         }
335 333
 
336 334
         result, error = self.restcall(self.endpoints['contacts'] % (guid,),
337  
-                                      params=params)
  335
+                                      params=params, headers=headers)
338 336
         if error:
339 337
             return result, error
340 338
         ycontacts = result.get('contacts')
6  linkoauth/tests/invalid_requests/test_facebook.py
@@ -23,15 +23,15 @@ def check_error(self, res, error, expected_code=400):
23 23
         self.assertEqual(error['code'], expected_code)
24 24
 
25 25
     def test_no_share_type(self):
26  
-        res, error = self.requester.sendmessage('', self.get_args())
  26
+        res, error = self.requester.sendmessage('', self.get_args(), None)
27 27
         self.check_error(res, error)
28 28
 
29 29
     def test_invalid_share_type(self):
30 30
         res, error = self.requester.sendmessage(
31  
-            '', self.get_args(shareType="invalid"))
  31
+            '', self.get_args(shareType="invalid"), None)
32 32
         self.check_error(res, error)
33 33
 
34 34
     def test_no_wall_name(self):
35 35
         args = self.get_args(shareType="groupWall")
36  
-        res, error = self.requester.sendmessage('', args)
  36
+        res, error = self.requester.sendmessage('', args, None)
37 37
         self.check_error(res, error)
10  linkoauth/tests/invalid_requests/test_linkedin.py
@@ -25,25 +25,25 @@ def check_error(self, res, error, expected_code=400):
25 25
         self.assertEqual(error['code'], expected_code)
26 26
 
27 27
     def test_no_share_type(self):
28  
-        res, error = self.requester.sendmessage('', self.get_args())
  28
+        res, error = self.requester.sendmessage('', self.get_args(), None)
29 29
         self.check_error(res, error)
30 30
 
31 31
     def test_invalid_share_type(self):
32 32
         res, error = self.requester.sendmessage(
33  
-            '', self.get_args(shareType="invalid"))
  33
+            '', self.get_args(shareType="invalid"), None)
34 34
         self.check_error(res, error)
35 35
 
36 36
     def test_invalid_public_share_type(self):
37 37
         args = self.get_args(shareType="public", to="invalid")
38  
-        res, error = self.requester.sendmessage('', args)
  38
+        res, error = self.requester.sendmessage('', args, None)
39 39
         self.check_error(res, error)
40 40
 
41 41
     def test_invalid_connections_share_type(self):
42 42
         args = self.get_args(shareType="myConnections", to="invalid")
43  
-        res, error = self.requester.sendmessage('', args)
  43
+        res, error = self.requester.sendmessage('', args, None)
44 44
         self.check_error(res, error)
45 45
 
46 46
     def test_contact_no_to(self):
47 47
         args = self.get_args(shareType='contact', to='')
48  
-        res, error = self.requester.sendmessage('', args)
  48
+        res, error = self.requester.sendmessage('', args, None)
49 49
         self.check_error(res, error)
6  linkoauth/tests/invalid_requests/test_twitter.py
@@ -25,15 +25,15 @@ def check_error(self, res, error, expected_code=400):
25 25
         self.assertEqual(error['code'], expected_code)
26 26
 
27 27
     def test_no_share_type(self):
28  
-        res, error = self.requester.sendmessage('', self.get_args())
  28
+        res, error = self.requester.sendmessage('', self.get_args(), None)
29 29
         self.check_error(res, error)
30 30
 
31 31
     def test_invalid_share_type(self):
32 32
         res, error = self.requester.sendmessage(
33  
-            '', self.get_args(shareType="invalid"))
  33
+            '', self.get_args(shareType="invalid"), None)
34 34
         self.check_error(res, error)
35 35
 
36 36
     def test_direct_no_to(self):
37 37
         args = self.get_args(shareType='direct', to='')
38  
-        res, error = self.requester.sendmessage('', args)
  38
+        res, error = self.requester.sendmessage('', args, None)
39 39
         self.check_error(res, error)
2  linkoauth/tests/test_base.py
@@ -153,7 +153,7 @@ def test_disabled(self):
153 153
         services = Services(['google.com'], feedback_enabled=False)
154 154
         services.initialize('google.com')
155 155
         res, error = services.sendmessage('google.com', _ACCOUNT,
156  
-                                          message, args)
  156
+                                          message, args, None)
157 157
 
158 158
         status = services.get_status('google.com')
159 159
         self.assertEquals(status, (True, 0, 0))
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.