Skip to content

Commit

Permalink
Merge pull request #49 from ArmandBENETEAU/sandbox/abeneteau/add-nest…
Browse files Browse the repository at this point in the history
…ed-methods-in-server

Allow custom method names other than the default on server side
  • Loading branch information
mosquito authored Jan 11, 2022
2 parents 496a3cb + 4eec9e5 commit bbbdd0b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ Server example
from aiohttp import web
from aiohttp_xmlrpc import handler
from tornado.testing import *
from aiohttp_xmlrpc.handler import rename
class XMLRPCExample(handler.XMLRPCView):
@rename("nested.test")
def rpc_test(self):
return None
Expand All @@ -47,6 +49,7 @@ Server example
def rpc_args_kwargs(self, *args, **kwargs):
return len(args) + len(kwargs)
@rename("nested.exception")
def rpc_exception(self):
raise Exception("YEEEEEE!!!")
Expand All @@ -59,6 +62,7 @@ Server example
Client example
--------------

Expand All @@ -72,11 +76,11 @@ Client example
client = ServerProxy("http://127.0.0.1:8080/", loop=loop)
async def main():
print(await client.test())
# 'nested.test' method call
print(await client.nested.test())
# Or via __getitem__
method = client['args']
print(await method(1, 2, 3))
# 'args' method call
print(await client.args(1, 2, 3))
client.close()
Expand Down
16 changes: 15 additions & 1 deletion aiohttp_xmlrpc/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ def __new__(cls, clsname, superclasses, attributedict):
if not key.startswith(instance.METHOD_PREFIX):
continue

# Get the value of the corresponding function
value = getattr(instance, key)
method_name = key.replace(instance.METHOD_PREFIX, "", 1)

method_name = getattr(value, "__xmlrpc_name__", None)
if method_name is None:
method_name = key.replace(instance.METHOD_PREFIX, "", 1)

allowed_methods[method_name] = key

# Add the arg mapping in all cases
argmapping[method_name] = inspect.getfullargspec(value)

setattr(
Expand Down Expand Up @@ -185,3 +192,10 @@ def _build_xml(cls, tree):
encoding="utf-8",
pretty_print=cls.DEBUG,
)


def rename(new_name):
def decorator(func):
func.__xmlrpc_name__ = new_name
return func
return decorator
11 changes: 5 additions & 6 deletions examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@


async def main():
print(await client.test())
# 'nested.test' method call
print(await client.nested.test())

# Or via __getitem__
method = client['args']
print(await method(1, 2, 3))

client.close()
# 'args' method call
print(await client.args(1, 2, 3))

await client.close()

if __name__ == "__main__":
loop.run_until_complete(main())
4 changes: 4 additions & 0 deletions examples/server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from aiohttp import web
from aiohttp_xmlrpc import handler
from aiohttp_xmlrpc.handler import rename


class XMLRPCExample(handler.XMLRPCView):

@rename("nested.test")
def rpc_test(self):
return None

Expand All @@ -15,6 +18,7 @@ def rpc_kwargs(self, **kwargs):
def rpc_args_kwargs(self, *args, **kwargs):
return len(args) + len(kwargs)

@rename("nested.exception")
def rpc_exception(self):
raise Exception("YEEEEEE!!!")

Expand Down
23 changes: 23 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from lxml import etree
from lxml.builder import E

from aiohttp_xmlrpc.handler import rename

pytest_plugins = (
"aiohttp.pytest_plugin",
Expand All @@ -16,6 +17,7 @@


class XMLRPCMain(handler.XMLRPCView):

def rpc_test(self):
return None

Expand Down Expand Up @@ -52,11 +54,20 @@ def rpc_dict_kwargs(self, d, **kw):
def rpc_dict_kw_only_args(self, d, *, foo, **kw):
return (d, foo, kw)

@rename("method_with.new_name")
def rpc_renamed(self):
return "renamed_function"


class XMLRPCChild(XMLRPCMain):

def rpc_child(self):
return 42

@rename("child.test")
def rpc_child_nested_method(self):
return "My name has the nested format and I am in child class"


def create_app(loop):
app = web.Application()
Expand Down Expand Up @@ -214,3 +225,15 @@ async def test_13_kw_only_args(client):
{"foo": "bar"}, foo=32, spam="egg"
)
assert result == [{"foo": "bar"}, 32, {"spam": "egg"}]


async def test_14_method_renaming(client):
result = await client.method_with.new_name()
assert result == "renamed_function"


async def test_15_nested_method_in_child(aiohttp_xmlrpc_client):
client = await aiohttp_xmlrpc_client(create_app, path="/clone")

result = await client.child.test()
assert result == "My name has the nested format and I am in child class"

0 comments on commit bbbdd0b

Please sign in to comment.