Submitted Wed Jul 15 07:43:01 UTC 2009 at http://code.google.com/p/opensocial-python-client/issues/detail?id=27 Loic Dachary Index: tests/opensocial_tests/partuza_test.py =================================================================== --- tests/opensocial_tests/partuza_test.py (revision 44) +++ tests/opensocial_tests/partuza_test.py (working copy) @@ -20,32 +20,65 @@ import unittest import urllib2 +import types import opensocial from opensocial import oauth from opensocial import request +from opensocial import http +# http.VERBOSE = 1 +http.VERBOSE = 0 class TestPartuza(unittest.TestCase): def setUp(self): - self.config = opensocial.ContainerConfig( - oauth_consumer_key='e2c2d2dd-e6c4-c4df-b2c4-d6efd2dcffd1', - oauth_consumer_secret='eb214eedcda39f3440c43b623806912f', - server_rpc_base='http://modules.partuza.nl/social/rpc', - server_rest_base='http://modules.partuza.nl/social/rest') + self.config = opensocial.ContainerConfig( + oauth_consumer_key='e2c2d2dd-e6c4-c4df-b2c4-d6efd2dcffd1', + oauth_consumer_secret='eb214eedcda39f3440c43b623806912f', + server_rpc_base='http://modules.partuza.nl/social/rpc', + server_rest_base='http://modules.partuza.nl/social/rest') + self.user_id = '1311' # http://www.partuza.nl/profile/1311 + self.app_id = '1103' # http://opensocial-resources.googlecode.com/svn/samples/rest_rpc/sample.xml +# self.config = opensocial.ContainerConfig( +# oauth_consumer_key='deedc0e4-dbcd-cbc8-a5e2-dadafae0d4d8', +# oauth_consumer_secret='282ce3c8e43576efe6d216da3e37a359', +# server_rpc_base='http://shindig.opensocial.dachary.org/social/rpc', +# server_rest_base='http://shindig.opensocial.dachary.org/social/rest') +# self.user_id = '1' +# self.app_id = '1' self.container = opensocial.ContainerContext(self.config) - self.user_id = '1311' def validate_user(self, user): self.assertEquals(self.user_id, user.get_id()) + def validate_app_data(self, app_data): + self.failUnless(type(app_data) == types.ListType or isinstance(app_data, opensocial.data.AppData)) + def do_fetch_person(self, use_rpc, fields=None): self.container.set_allow_rpc(use_rpc) person = self.container.fetch_person(self.user_id, fields) return person + def do_fetch_app_data(self, use_rpc, fields=None): + self.container.set_allow_rpc(use_rpc) + r = request.FetchAppDataRequest(self.user_id, '@self', self.app_id, fields, self.verbose) + app_data = self.container.send_request(r) + return app_data + + def do_update_app_data(self, use_rpc, fields=None): + self.container.set_allow_rpc(use_rpc) + r = request.UpdateAppDataRequest(self.user_id, '@self', self.app_id, fields, { 'a': 'b' }, self.verbose) + result = self.container.send_request(r) + return result + + def do_delete_app_data(self, use_rpc, fields=None): + self.container.set_allow_rpc(use_rpc) + r = request.DeleteAppDataRequest(self.user_id, '@self', self.app_id, [ 'a' ]) + result = self.container.send_request(r) + return result + def test_fetch_person_rpc(self): person = self.do_fetch_person(True) self.validate_user(person) @@ -54,6 +87,22 @@ person = self.do_fetch_person(False) self.validate_user(person) + def test_fetch_app_data_rest(self): + app_data = self.do_fetch_app_data(False) + self.validate_app_data(app_data) + + def test_fetch_app_data_rpc(self): + app_data = self.do_fetch_app_data(True) + self.validate_app_data(app_data) + + def test_update_app_data_rpc(self): + self.do_update_app_data(True) + app_data = self.do_fetch_app_data(True) + self.assertEquals('b', app_data[self.user_id]['a']) + self.do_delete_app_data(True) + app_data = self.do_fetch_app_data(True) + self.validate_app_data(app_data) + def test_fetch_person_fields_rpc(self): person = self.do_fetch_person(True, ['gender']) self.assertEquals('female', person.get_field('gender')) Index: src/opensocial/request.py =================================================================== --- src/opensocial/request.py (revision 44) +++ src/opensocial/request.py (working copy) @@ -22,6 +22,7 @@ import random import time import urlparse +from types import ListType import data import http @@ -177,7 +178,10 @@ Returns: An AppData object. """ - return data.AppData.parse_json(json) + if type(json) == ListType: + return json + else: + return data.AppData.parse_json(json) class UpdateAppDataRequest(Request): @@ -189,7 +193,7 @@ if fields: params['fields'] = ','.join(fields) - params['data'] = simplejson.dumps(data) + params['data'] = data #TODO: add support for rest params.update({'userId': user_id, @@ -223,17 +227,9 @@ user_id) def process_json(self, json): - """Construct the appropriate OpenSocial object from a JSON dict. - - Args: - json: dict The JSON structure. - - Returns: An AppData object. + return json - """ - return data.AppData.parse_json(json) - class RestRequestInfo(object): """Represents a pending REST request.""" Index: src/opensocial/__init__.py =================================================================== --- src/opensocial/__init__.py (revision 44) +++ src/opensocial/__init__.py (working copy) @@ -241,6 +241,8 @@ def _handle_response(self, http_response): """ If status code "OK", then we can safely inspect the returned JSON.""" if http_response.status == httplib.OK: + if http.VERBOSE > 0: + print "http_response.content => " + http_response.content json = simplejson.loads(http_response.content) # Check for any JSON-RPC 2.0 errors. if 'error' in json: Index: src/opensocial/http.py =================================================================== --- src/opensocial/http.py (revision 44) +++ src/opensocial/http.py (working copy) @@ -35,6 +35,7 @@ logging.basicConfig(level=logging.DEBUG) +VERBOSE = 0 def get_default_urlfetch(): """Creates the default UrlFetch interface. @@ -79,6 +80,8 @@ log_request(request) method = request.get_method() headers = request.get_headers() + if VERBOSE > 0: + print "URL => " + request.get_url() req = urllib2.Request(request.get_url(), data=request.get_post_body(), headers=headers) @@ -171,6 +174,8 @@ else: # Otherwise, use the oauth_body_hash extension to sign the request body. if self.post_body: + if VERBOSE > 0: + print "post_body => " + str(self.post_body) body_hash = b64encode(hashlib.sha1(self.get_post_body()).digest()) params['oauth_body_hash'] = body_hash @@ -178,6 +183,11 @@ self.set_parameter("xoauth_requestor_id", None) self.set_parameters(params) + if VERBOSE > 0: + key, raw = signature_method.build_signature_base_string(self.oauth_request, consumer, None) + print "build_signature key => " + key + print "build_signature raw => " + raw + self.oauth_request.sign_request(signature_method, consumer, None) def set_parameter(self, name, value):