Code Monkey home page Code Monkey logo

Comments (7)

ArmandBENETEAU avatar ArmandBENETEAU commented on July 19, 2024

Actually I've just noticed that a workaround exists for this problem!
I just have to call explicitly the getattr() function...

# Get the list of available devices

# First attempt (not working)
# devices = await async_client.scheduler.devices.list()

# Second attempt (working)
devices = await getattr(async_client, "scheduler.devices.list")()

from aiohttp-xmlrpc.

mosquito avatar mosquito commented on July 19, 2024

It is not very difficult to implement, rather it is difficult to implement correctly.

For example, there is a class A with the foo method, should a call to the A.foo instantiate class A and with what parameters?

from aiohttp-xmlrpc.

ArmandBENETEAU avatar ArmandBENETEAU commented on July 19, 2024

I am not sure that I completely understood your question.

For my specific need, the full name of the method (with the point) needs to be passed to the <methodName> field in the body of the HTTP request.

So, no needs to instantiate the "class A" method, only pass the following method in the HTTP request:

<?xml version='1.0' encoding='ASCII'?>
<methodCall><methodName>A.foo</methodName><params/></methodCall>

It is basically what the (synchronous) xmlrpc library built-in python 3 does. As we can see here:

from aiohttp-xmlrpc.

mosquito avatar mosquito commented on July 19, 2024

Well, try to imagine a code example that would work both from the server-side and from the client-side, and write it here.

from aiohttp-xmlrpc.

ArmandBENETEAU avatar ArmandBENETEAU commented on July 19, 2024

Okay, I thing I get your point now. It will be easier to do that on the client-side than on the server-side.
I will see what I can do about that.

from aiohttp-xmlrpc.

ArmandBENETEAU avatar ArmandBENETEAU commented on July 19, 2024

Hi again Mosquito,

So I've seen with my hierarchy today and since we have a lot of work currently and that we are not using the server-side of aiohttp-xmlrpc, I cannot spend time "right now" on the server-side of this feature. Nevertheless, I could go back on it later when the peak of activities will have been passed.

In the mid-time, I can do a pull request with the correction I have done on the client side. The modifications I plan to add are in the following patch:

From f47ace384e953db25a8f95560cf32645bcbe68e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Armand=20B=C3=A9n=C3=A9teau?= <[email protected]>
Date: Mon, 15 Nov 2021 15:25:46 +0000
Subject: [PATCH] Allow nested methods to be called on client side
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Armand Bénéteau <[email protected]>
---
 aiohttp_xmlrpc/client.py | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/aiohttp_xmlrpc/client.py b/aiohttp_xmlrpc/client.py
index 2047d74..ed1440d 100644
--- a/aiohttp_xmlrpc/client.py
+++ b/aiohttp_xmlrpc/client.py
@@ -12,6 +12,20 @@ from .exceptions import xml2py_exception
 log = logging.getLogger(__name__)
 
 
+class _Method:
+    # some magic to bind an XML-RPC method to an RPC server.
+    # supports "nested" methods (e.g. examples.getStateName)
+    def __init__(self, send, name):
+        self.__send = send
+        self.__name = name
+
+    def __getattr__(self, name):
+        return _Method(self.__send, "%s.%s" % (self.__name, name))
+
+    def __call__(self, *args, **kwargs):
+        return self.__send(self.__name, args, kwargs)
+
+
 class ServerProxy(object):
     __slots__ = "client", "url", "loop", "headers", "encoding", "huge_tree"
 
@@ -107,13 +121,12 @@ class ServerProxy(object):
             return self._parse_response((await response.read()), method_name)
 
     def __getattr__(self, method_name):
-        return self[method_name]
-
-    def __getitem__(self, method_name):
-        def method(*args, **kwargs):
-            return self.__remote_call(method_name, *args, **kwargs)
-
-        return method
+        # Trick to keep the "close" method available
+        if method_name == "close":
+            return self.__close
+        else:
+            # Magic method dispatcher
+            return _Method(self.__remote_call, method_name)
 
     def __aenter__(self):
         return self.client.__aenter__()
@@ -121,5 +134,5 @@ class ServerProxy(object):
     def __aexit__(self, exc_type, exc_val, exc_tb):
         return self.client.__aexit__(exc_type, exc_val, exc_tb)
 
-    def close(self):
+    def __close(self):
         return self.client.close()
-- 
2.27.0

from aiohttp-xmlrpc.

mosquito avatar mosquito commented on July 19, 2024

Ok. Just open PR and let’s continue this dialogue there.

from aiohttp-xmlrpc.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.