From 6e2ffb08206f4cab148b10a5c0c9f5a1f29aade8 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 22 Nov 2012 12:11:22 +0100 Subject: [PATCH] jsonrpc: Make dict returns from plain objects work --- mopidy/utils/jsonrpc.py | 4 +++- tests/utils/jsonrpc_test.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/mopidy/utils/jsonrpc.py b/mopidy/utils/jsonrpc.py index 17074239..d8000332 100644 --- a/mopidy/utils/jsonrpc.py +++ b/mopidy/utils/jsonrpc.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals import json import traceback +import pykka + class JsonRpcWrapper(object): """ @@ -167,7 +169,7 @@ class JsonRpcWrapper(object): return 'id' not in request def _is_future(self, result): - return callable(getattr(result, 'get', None)) + return isinstance(result, pykka.Future) class JsonRpcError(Exception): diff --git a/tests/utils/jsonrpc_test.py b/tests/utils/jsonrpc_test.py index 68950b59..c9972d96 100644 --- a/tests/utils/jsonrpc_test.py +++ b/tests/utils/jsonrpc_test.py @@ -22,6 +22,12 @@ class Calculator(object): def sub(self, a, b): return a - b + def describe(self): + return { + 'add': 'Returns the sum of the terms', + 'sub': 'Returns the diff of the terms', + } + def _secret(self): return 'Grand Unified Theory' @@ -118,6 +124,20 @@ class JsonRpcSingleCommandTest(JsonRpcTestBase): self.assertNotIn('error', response) self.assertEqual(response['result'], 'TI83') + def test_call_method_which_returns_dict_from_plain_object(self): + request = { + 'jsonrpc': '2.0', + 'method': 'calculator.describe', + 'id': 1, + } + response = self.jrw.handle_data(request) + + self.assertEqual(response['jsonrpc'], '2.0') + self.assertEqual(response['id'], 1) + self.assertNotIn('error', response) + self.assertIn('add', response['result']) + self.assertIn('sub', response['result']) + def test_call_method_on_actor_root(self): request = { 'jsonrpc': '2.0',