Merge lp:~mterry/ubuntuone-couch/extra-headers into lp:ubuntuone-couch

Proposed by Michael Terry
Status: Merged
Approved by: dobey
Approved revision: 9
Merged at revision: 8
Proposed branch: lp:~mterry/ubuntuone-couch/extra-headers
Merge into: lp:ubuntuone-couch
Diff against target: 22 lines (+4/-2)
1 file modified
ubuntuone/couch/auth.py (+4/-2)
To merge this branch: bzr merge lp:~mterry/ubuntuone-couch/extra-headers
Reviewer Review Type Date Requested Status
dobey (community) Approve
Eric Casteleijn (community) Approve
Review via email: mp+61648@code.launchpad.net

Commit message

Allow extra headers in auth.request()

To post a comment you must log in.
Revision history for this message
Eric Casteleijn (thisfred) wrote :

Looks good, except this is a bad idea:

+ token_secret=None, headers={}):

As default values in Python should as a rule not be mutable types. Default values are evaluated at import time, making this empty dictionary a 'global' variable, so that in this case the oauth headers for one call to request() will pollute the dictionary for any subsequent calls.

Please change it to:

+ token_secret=None, headers=None):

and then at the beginning of the function implementation do:

headers = headers or {}

Also a test case would be awesome, but if you cannot think of one, I can look at that.

review: Needs Fixing
9. By Michael Terry

use None instead of {} in signature

Revision history for this message
Michael Terry (mterry) wrote :

OK, updated to use your None method.

Could you please look at a test? I looked briefly and would copy the test_request_plaintext method and just add a test header like "Foo": "Bar", but I wasn't sure how to test whether the outgoing request was what I expected.

Revision history for this message
Eric Casteleijn (thisfred) wrote :

Looks great now, I'll think of a test and add it in a separate branch.

review: Approve
Revision history for this message
dobey (dobey) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntuone/couch/auth.py'
2--- ubuntuone/couch/auth.py 2011-03-11 16:38:11 +0000
3+++ ubuntuone/couch/auth.py 2011-05-19 19:53:27 +0000
4@@ -90,7 +90,7 @@
5
6 def request(url, sigmeth='HMAC_SHA1', http_method='GET', request_body=None,
7 consumer_key=None, consumer_secret=None, access_token=None,
8- token_secret=None):
9+ token_secret=None, headers=None):
10 """Make an OAuth signed request."""
11 # Set the signature method. This should be HMAC unless you have a jolly
12 # good reason for it to not be.
13@@ -109,6 +109,8 @@
14
15 oauth_header = get_oauth_request_header(
16 consumer, token, url, http_method, signature_method)
17+ headers = headers or {}
18+ headers.update(oauth_header)
19 http = httplib2.Http()
20 return http.request(
21- url, method=http_method, headers=oauth_header, body=request_body)
22+ url, method=http_method, headers=headers, body=request_body)

Subscribers

People subscribed via source and target branches