From be9c65b72fa5105b29b49e859b8b07d37f6cdd68 Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Mon, 9 Jul 2018 10:27:14 +0200
Subject: [PATCH] issue #56 WIP - More clean syntax to call REST wrappers
 packages

Thanks to python __call__ magic method !
---
 duniterpy/api/client.py  | 17 ++++++++++++++---
 examples/request_data.py |  8 ++++----
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py
index 1599fbfe..1df294cc 100644
--- a/duniterpy/api/client.py
+++ b/duniterpy/api/client.py
@@ -1,13 +1,13 @@
 # Authors:
 # Caner Candan <caner@candan.fr>, http://caner.candan.fr
 # Inso <insomniak.fr at gmail.com>
-
+from typing import Callable
 import json
 import logging
 import aiohttp
 import jsonschema
 from .errors import DuniterError
-from duniterpy.api.endpoint import endpoint
+import duniterpy.api.endpoint as endpoint
 
 logger = logging.getLogger("duniter")
 
@@ -180,7 +180,7 @@ class Client:
         :param proxy: Proxy server as hostname:port
         """
         # Endpoint Protocol detection
-        self.endpoint = endpoint(_endpoint)
+        self.endpoint = endpoint.endpoint(_endpoint)
 
         # if no user session...
         if session is None:
@@ -219,3 +219,14 @@ class Client:
         """
         await self.session.close()
 
+    async def __call__(self, _function: Callable, *args: any, **kwargs: any) -> any:
+        """
+        Call the _function given with the args given
+        So we can have use many packages wrapping a REST API
+
+        :param _function: The function to call
+        :param args: The parameters
+        :param kwargs: The key/value parameters
+        :return:
+        """
+        return await _function(self, *args, **kwargs)
diff --git a/examples/request_data.py b/examples/request_data.py
index baac93db..47223516 100644
--- a/examples/request_data.py
+++ b/examples/request_data.py
@@ -24,19 +24,19 @@ async def main():
     print(response)
 
     # Get the node summary infos by dedicated method (with json schema validation)
-    response = await bma.node.summary(client)
+    response = await client(bma.node.summary)
     print(response)
 
     # Get the money parameters located in the first block
-    response = await bma.blockchain.parameters(client)
+    response = await client(bma.blockchain.parameters)
     print(response)
 
     # Get the current block
-    response = await bma.blockchain.current(client)
+    response = await client(bma.blockchain.current)
     print(response)
 
     # Get the block number 10
-    response = await bma.blockchain.block(client, 10)
+    response = await client(bma.blockchain.block, 10)
     print(response)
 
     # Close client aiohttp session
-- 
GitLab