def test_function(self): def foo(): pass #pylint:disable=multiple-statements methods = Methods() methods.add(foo) self.assertIs(foo, methods['foo'])
def test_function_custom_name(self): def foo(): pass #pylint:disable=multiple-statements methods = Methods() methods.add(foo, 'foobar') self.assertIs(foo, methods['foobar'])
def test_methods_partials(self): multiply = lambda x, y: x * y double = partial(multiply, 2) methods = Methods() methods.add(double, 'double') req = Request({'jsonrpc': '2.0', 'method': 'double', 'params': [3], 'id': 1}) self.assertEqual(6, req.call(methods)['result'])
def test_function(self): def foo(): pass methods = Methods() methods.add(foo) self.assertIs(foo, methods['foo'])
def test_function_custom_name(self): def foo(): pass methods = Methods() methods.add(foo, 'foobar') self.assertIs(foo, methods['foobar'])
def test_instance_method(self): class FooClass(object): #pylint:disable=too-few-public-methods def foo(self): # pylint: disable=no-self-use return 'bar' methods = Methods() methods.add(FooClass().foo) self.assertEqual('bar', methods['foo'].__call__())
def test_static_method(self): class FooClass(object): #pylint:disable=too-few-public-methods @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo) self.assertIs(FooClass.foo, methods['foo'])
def test_methods_object(self): def cat(): pass # pylint: disable=multiple-statements def dog(): pass # pylint: disable=multiple-statements methods = Methods() methods.add(cat) methods.add(dog) self.assertIs(cat, Request._get_method(methods, 'cat')) self.assertIs(dog, Request._get_method(methods, 'dog'))
def test_methods_object(self): def cat(): pass def dog(): pass methods = Methods() methods.add(cat) methods.add(dog) self.assertIs(cat, get_method(methods, 'cat')) self.assertIs(dog, get_method(methods, 'dog'))
def test_instance_method(self): class FooClass(object): def foo(self): return "bar" methods = Methods() methods.add(FooClass().foo) self.assertEqual("bar", methods["foo"].__call__())
def test_instance_method(self): class FooClass(object): def foo(self): return 'bar' methods = Methods() methods.add(FooClass().foo) self.assertEqual('bar', methods['foo'].__call__())
def test_static_method_custom_name(self): class FooClass(object): @staticmethod def foo(): return "bar" methods = Methods() methods.add(FooClass.foo, "custom") self.assertIs(FooClass.foo, methods["custom"])
def test_static_method_custom_name(self): class FooClass(object): @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo, 'custom') self.assertIs(FooClass.foo, methods['custom'])
def test_dispatch(self): def foo(): return "bar" methods = Methods() methods.add(foo) request = {"jsonrpc": "2.0", "method": "foo", "id": 1} response = methods.dispatch(request) self.assertEqual(response["result"], "bar")
def test_static_method_custom_name(self): class FooClass(object): #pylint:disable=too-few-public-methods @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo, 'custom') self.assertIs(FooClass.foo, methods['custom'])
def test_static_method(self): class FooClass(object): @staticmethod def foo(): return 'bar' methods = Methods() methods.add(FooClass.foo) self.assertIs(FooClass.foo, methods['foo'])
def test_positionals(self): methods = Methods() methods.add(lambda x: x * x, 'square') req = Request({ 'jsonrpc': '2.0', 'method': 'square', 'params': [3], 'id': 1 }) self.assertEqual(9, req.call(methods)['result'])
def test_object_method(self): methods = Methods() methods.add(FooClass().foo, 'foo') req = Request({ 'jsonrpc': '2.0', 'method': 'foo', 'params': [1, 2], 'id': 1 }) response = req.call(methods) self.assertIsInstance(response, RequestResponse) self.assertEqual('bar', response['result'])
def test_instance_method_custom_name(self): class Foo(object): #pylint:disable=too-few-public-methods def __init__(self, name): self.name = name def get_name(self): return self.name obj1 = Foo('a') obj2 = Foo('b') methods = Methods() methods.add(obj1.get_name, 'custom1') methods.add(obj2.get_name, 'custom2') # Can't use assertIs, so check the outcome is as expected self.assertEqual('a', methods['custom1'].__call__()) self.assertEqual('b', methods['custom2'].__call__())
def test_instance_method_custom_name(self): class Foo(object): def __init__(self, name): self.name = name def get_name(self): return self.name obj1 = Foo("a") obj2 = Foo("b") methods = Methods() methods.add(obj1.get_name, "custom1") methods.add(obj2.get_name, "custom2") # Can't use assertIs, so check the outcome is as expected self.assertEqual("a", methods["custom1"].__call__()) self.assertEqual("b", methods["custom2"].__call__())
def install(self, api_world: "ApiWorld", methods: Methods): self._parent_world = api_world tracer_binder = _bind_tracer(self) register = {} for method_name in dir(self): if method_name.startswith("_"): continue method = getattr(self, method_name) if not callable(method): continue register_name = getattr(method, _RPC_NAME_ATTR, method_name) register_rpc_name = "%s.%s" % (self.name, register_name) register[register_rpc_name] = tracer_binder(method_name, method, self.log) methods.add(**register)
def build_methods(): methods = Methods() methods.add( **{ 'plug_play_api.' + method.__name__: method for method in (api_endpoints.ping, api_endpoints.get_sync_status, api_endpoints.get_ops_by_block) }) methods.add( **{ 'plug_play_api.community.' + method.__name__: method for method in (community.get_subscribe_ops, community.get_unsubscribe_ops, community.get_flag_post_ops) }) methods.add( **{ 'plug_play_api.follow.' + method.__name__: method for method in (follow.get_follow_ops, follow.get_reblog_ops) }) return methods
def test_lambda_renamed(self): add = lambda x, y: x + y add.__name__ = 'add' methods = Methods() methods.add(add) self.assertIs(add, methods['add'])
def test_methods_lambdas(self): methods = Methods() methods.add(lambda: "bar", "foo") req = Request({"jsonrpc": "2.0", "method": "foo", "id": 1}) self.assertEqual("bar", req.call(methods)["result"])
def test_partial_renamed(self): six = partial(lambda x: x + 1, 5) six.__name__ = 'six' methods = Methods() methods.add(six) self.assertIs(six, methods['six'])
def test_partial_custom_name(self): six = partial(lambda x: x + 1, 5) methods = Methods() methods.add(six, 'six') self.assertIs(six, methods['six'])
def test_lambda_custom_name(self): add = lambda x, y: x + y methods = Methods() methods.add(add, 'add') self.assertIs(add, methods['add'])
def test_partial_no_name(self): six = partial(lambda x: x + 1, 5) methods = Methods() with self.assertRaises(AttributeError): methods.add(six) # Partial has no __name__ !
def test_methods_lambdas(self): methods = Methods() methods.add(lambda: 'bar', 'foo') req = Request({'jsonrpc': '2.0', 'method': 'foo', 'id': 1}) self.assertEqual('bar', req.call(methods)['result'])
def build_methods(): """Register all supported hive_api/condenser_api.calls.""" # pylint: disable=expression-not-assigned, line-too-long methods = Methods() methods.add( **{ 'hive.' + method.__name__: method for method in (hive_api.db_head_state, #hive_api.payouts_total, #hive_api.payouts_last_24h, #hive_api.get_accounts, #hive_api.get_accounts_ac, ) }) methods.add( **{ 'condenser_api.' + method.__name__: method for method in ( condenser_api.get_followers, condenser_api.get_following, condenser_api.get_follow_count, condenser_api.get_content, condenser_api.get_content_replies, condenser_api_get_state, condenser_api_get_trending_tags, condenser_api.get_discussions_by_trending, condenser_api.get_discussions_by_hot, condenser_api.get_discussions_by_promoted, condenser_api.get_discussions_by_created, condenser_api.get_discussions_by_blog, condenser_api.get_discussions_by_feed, condenser_api.get_discussions_by_comments, condenser_api.get_replies_by_last_update, condenser_api.get_discussions_by_author_before_date, condenser_api.get_post_discussions_by_payout, condenser_api.get_comment_discussions_by_payout, condenser_api.get_blog, condenser_api.get_blog_entries, condenser_api.get_account_reputations, condenser_api.get_reblogged_by, ) }) # dummy methods -- serve informational error methods.add( **{ 'condenser_api.get_account_votes': condenser_api.get_account_votes, 'tags_api.get_account_votes': condenser_api.get_account_votes, }) # follow_api aliases methods.add( **{ 'follow_api.get_followers': condenser_api.get_followers, 'follow_api.get_following': condenser_api.get_following, 'follow_api.get_follow_count': condenser_api.get_follow_count, 'follow_api.get_account_reputations': condenser_api.get_account_reputations, 'follow_api.get_blog': condenser_api.get_blog, 'follow_api.get_blog_entries': condenser_api.get_blog_entries, 'follow_api.get_reblogged_by': condenser_api.get_reblogged_by, }) # tags_api aliases methods.add( **{ 'tags_api.get_discussion': condenser_api.get_content, 'tags_api.get_content_replies': condenser_api.get_content_replies, 'tags_api.get_discussions_by_trending': condenser_api.get_discussions_by_trending, 'tags_api.get_discussions_by_hot': condenser_api.get_discussions_by_hot, 'tags_api.get_discussions_by_promoted': condenser_api.get_discussions_by_promoted, 'tags_api.get_discussions_by_created': condenser_api.get_discussions_by_created, 'tags_api.get_discussions_by_blog': condenser_api.get_discussions_by_blog, 'tags_api.get_discussions_by_comments': condenser_api.get_discussions_by_comments, 'tags_api.get_discussions_by_author_before_date': condenser_api.get_discussions_by_author_before_date, 'tags_api.get_post_discussions_by_payout': condenser_api.get_post_discussions_by_payout, 'tags_api.get_comment_discussions_by_payout': condenser_api.get_comment_discussions_by_payout, }) # legacy `call` style adapter methods.add(**{'call': condenser_api_call}) return methods
def test_non_callable(self): methods = Methods() with self.assertRaises(TypeError): methods.add(None, 'ping')
def test_no_name(self): methods = Methods() with self.assertRaises(AttributeError): methods.add(None)
def test_lambda_no_name(self): add = lambda x, y: x + y methods = Methods() methods.add(add) # Lambda's __name__ will be '<lambda>'! self.assertNotIn('add', methods)
def test_add_partial_no_name(): six = partial(lambda x: x + 1, 5) methods = Methods() with pytest.raises(AttributeError): methods.add(six) # Partial has no __name__ !
def test_positionals(self): methods = Methods() methods.add(lambda x: x * x, 'square') req = Request({'jsonrpc': '2.0', 'method': 'square', 'params': [3], 'id': 1}) self.assertEqual(9, req.call(methods)['result'])
def build_methods(): """Register all supported hive_api/condenser_api.calls.""" # pylint: disable=expression-not-assigned, line-too-long methods = Methods() methods.add( **{'hive.' + method.__name__: method for method in (db_head_state, )}) methods.add( **{ 'condenser_api.' + method.__name__: method for method in ( condenser_api.get_followers, condenser_api.get_following, condenser_api.get_follow_count, condenser_api.get_content, condenser_api.get_content_replies, condenser_api_get_state, condenser_api_get_trending_tags, condenser_api.get_discussions_by_trending, condenser_api.get_discussions_by_hot, condenser_api.get_discussions_by_promoted, condenser_api.get_discussions_by_created, condenser_api.get_discussions_by_blog, condenser_api.get_discussions_by_feed, condenser_api.get_discussions_by_comments, condenser_api.get_replies_by_last_update, condenser_api.get_discussions_by_author_before_date, condenser_api.get_post_discussions_by_payout, condenser_api.get_comment_discussions_by_payout, condenser_api.get_blog, condenser_api.get_blog_entries, condenser_api.get_account_reputations, condenser_api.get_reblogged_by, ) }) # dummy methods -- serve informational error methods.add( **{ 'condenser_api.get_account_votes': condenser_api.get_account_votes, 'tags_api.get_account_votes': condenser_api.get_account_votes, }) # follow_api aliases methods.add( **{ 'follow_api.get_followers': condenser_api.get_followers, 'follow_api.get_following': condenser_api.get_following, 'follow_api.get_follow_count': condenser_api.get_follow_count, 'follow_api.get_account_reputations': condenser_api.get_account_reputations, 'follow_api.get_blog': condenser_api.get_blog, 'follow_api.get_blog_entries': condenser_api.get_blog_entries, 'follow_api.get_reblogged_by': condenser_api.get_reblogged_by, }) # tags_api aliases methods.add( **{ 'tags_api.get_discussion': condenser_api.get_content, 'tags_api.get_content_replies': condenser_api.get_content_replies, 'tags_api.get_discussions_by_trending': condenser_api.get_discussions_by_trending, 'tags_api.get_discussions_by_hot': condenser_api.get_discussions_by_hot, 'tags_api.get_discussions_by_promoted': condenser_api.get_discussions_by_promoted, 'tags_api.get_discussions_by_created': condenser_api.get_discussions_by_created, 'tags_api.get_discussions_by_blog': condenser_api.get_discussions_by_blog, 'tags_api.get_discussions_by_comments': condenser_api.get_discussions_by_comments, 'tags_api.get_discussions_by_author_before_date': condenser_api.get_discussions_by_author_before_date, 'tags_api.get_post_discussions_by_payout': condenser_api.get_post_discussions_by_payout, 'tags_api.get_comment_discussions_by_payout': condenser_api.get_comment_discussions_by_payout, }) # legacy `call` style adapter methods.add(**{'call': condenser_api_call}) # bridge_api methods methods.add( **{ 'bridge.' + method.__name__: method for method in ( bridge_api_normalize_post, bridge_api_get_post_header, bridge_api_get_discussion, bridge_api.get_post, bridge_api.get_account_posts, bridge_api.get_ranked_posts, bridge_api.get_profile, bridge_api.get_trending_topics, hive_api_notify.post_notifications, hive_api_notify.account_notifications, hive_api_notify.unread_notifications, hive_api_stats.get_payout_stats, hive_api_community.get_community, hive_api_community.get_community_context, hive_api_community.list_communities, hive_api_community.list_pop_communities, hive_api_community.list_community_roles, hive_api_community.list_subscribers, hive_api_community.list_all_subscriptions, ) }) return methods