Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 49: Add register_api to SimpleJSONRPCServer #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jsonrpclib/SimpleJSONRPCServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ def __init__(self, addr, requestHandler=SimpleJSONRPCRequestHandler,
flags |= fcntl.FD_CLOEXEC
fcntl.fcntl(self.fileno(), fcntl.F_SETFD, flags)

def register_api(self, api_obj):
methods = dir(api_obj)
apis = filter(lambda m: not m.startswith('_'), methods)
[self.register_function(getattr(api_obj, api)) for api in apis]


class CGIJSONRPCRequestHandler(SimpleJSONRPCDispatcher):

Expand Down
15 changes: 15 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,12 @@ def test_proxy_object_reuse_is_allowed(self):
result = sub_service_proxy.add(21, 21)
self.assertTrue(result == 42)

def test_registered_by_register_api(self):
client = self.get_client()
result = client.echo(['hello, world'])
self.assertEqual(result, ['hello, world'])


if jsonrpc.USE_UNIX_SOCKETS:
# We won't do these tests unless Unix Sockets are supported

Expand Down Expand Up @@ -450,6 +456,13 @@ def ping():
return True


class ExampleApi(object):

@staticmethod
def echo(args):
return args


class ExampleAggregateService(ExampleService):
"""
Exposes the inherited ExampleService and a second copy as sub_service
Expand All @@ -473,6 +486,8 @@ def log_request(self, *args, **kwargs):
server.register_function(service.summation, 'sum')
server.register_function(service.summation, 'notify_sum')
server.register_function(service.summation, 'namespace.sum')
example_api = ExampleApi()
server.register_api(example_api)
server_proc = Thread(target=server.serve_forever)
server_proc.daemon = True
server_proc.start()
Expand Down