Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Merge pull request #17 from mozilla-services/requests-hawk-work-around
Browse files Browse the repository at this point in the history
Attempt to fix the put_record method.
  • Loading branch information
Natim committed Oct 12, 2015
2 parents 741a5d2 + 55a640b commit 6e92a53
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'PyFxA',
'requests',
'requests-hawk',
'six',
]

setup(name='syncclient',
Expand Down
18 changes: 16 additions & 2 deletions syncclient/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from hashlib import sha256
from binascii import hexlify
import json
import six
import sys

import requests
Expand Down Expand Up @@ -35,7 +37,9 @@ def get_browserid_assertion(login, password, fxa_server_url=FXA_SERVER_URL,
session = client.login(login, password, keys=True)
bid_assertion = session.get_identity_assertion(tokenserver_url)
_, keyB = session.fetch_keys()
return bid_assertion, hexlify(sha256(keyB.encode('utf-8')).digest()[0:16])
if isinstance(keyB, six.text_type): # pragma: no cover
keyB = keyB.encode('utf-8')
return bid_assertion, hexlify(sha256(keyB).digest()[0:16])


class SyncClientError(Exception):
Expand Down Expand Up @@ -250,10 +254,20 @@ def put_record(self, collection, record, **kwargs):
Note that the server may impose a limit on the amount of data
submitted for storage in a single BSO.
"""
# XXX: Workaround until request-hawk supports the json parameter. (#17)
if isinstance(record, six.string_types):
record = json.loads(record)
record = record.copy()
record_id = record.pop('id')
headers = {}
if 'headers' in kwargs:
headers = kwargs.pop('headers')

headers['Content-Type'] = 'application/json; charset=utf-8'

return self._request('put', '/storage/%s/%s' % (
collection.lower(), record_id), json=record, **kwargs)
collection.lower(), record_id), data=json.dumps(record),
headers=headers, **kwargs)

def post_records(self, collection, records, **kwargs):
"""
Expand Down
18 changes: 14 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,26 @@ def test_put_record(self):
self.client.put_record('myCollection', record)
self.client._request.assert_called_with(
'put', '/storage/mycollection/1234',
json={'foo': 'bar'})
data='{"foo": "bar"}',
headers={'Content-Type': 'application/json; charset=utf-8'})

def test_put_record_handle_json_string_parameter(self):
record = '{"id": 1234, "foo": "bar"}'
self.client.put_record('myCollection', record)
self.client._request.assert_called_with(
'put', '/storage/mycollection/1234',
data='{"foo": "bar"}',
headers={'Content-Type': 'application/json; charset=utf-8'})

def test_put_record_can_receive_requests_parameters(self):
record = {'id': 1234, 'foo': 'bar'}
self.client.put_record('myCollection', record,
headers=mock.sentinel.headers)
headers={'Sentinel': 'true'})
self.client._request.assert_called_with(
'put', '/storage/mycollection/1234',
json={'foo': 'bar'},
headers=mock.sentinel.headers)
data='{"foo": "bar"}',
headers={'Content-Type': 'application/json; charset=utf-8',
'Sentinel': 'true'})

def test_put_record_doesnt_modify_the_passed_object(self):
record = {'id': 1234, 'foo': 'bar'}
Expand Down

0 comments on commit 6e92a53

Please sign in to comment.