Skip to content
Snippets Groups Projects
Commit be9c65b7 authored by Vincent Texier's avatar Vincent Texier
Browse files

issue #56 WIP - More clean syntax to call REST wrappers packages

Thanks to python __call__ magic method !
parent 2929764d
No related branches found
No related tags found
No related merge requests found
# Authors: # Authors:
# Caner Candan <caner@candan.fr>, http://caner.candan.fr # Caner Candan <caner@candan.fr>, http://caner.candan.fr
# Inso <insomniak.fr at gmail.com> # Inso <insomniak.fr at gmail.com>
from typing import Callable
import json import json
import logging import logging
import aiohttp import aiohttp
import jsonschema import jsonschema
from .errors import DuniterError from .errors import DuniterError
from duniterpy.api.endpoint import endpoint import duniterpy.api.endpoint as endpoint
logger = logging.getLogger("duniter") logger = logging.getLogger("duniter")
...@@ -180,7 +180,7 @@ class Client: ...@@ -180,7 +180,7 @@ class Client:
:param proxy: Proxy server as hostname:port :param proxy: Proxy server as hostname:port
""" """
# Endpoint Protocol detection # Endpoint Protocol detection
self.endpoint = endpoint(_endpoint) self.endpoint = endpoint.endpoint(_endpoint)
# if no user session... # if no user session...
if session is None: if session is None:
...@@ -219,3 +219,14 @@ class Client: ...@@ -219,3 +219,14 @@ class Client:
""" """
await self.session.close() 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)
...@@ -24,19 +24,19 @@ async def main(): ...@@ -24,19 +24,19 @@ async def main():
print(response) print(response)
# Get the node summary infos by dedicated method (with json schema validation) # 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) print(response)
# Get the money parameters located in the first block # Get the money parameters located in the first block
response = await bma.blockchain.parameters(client) response = await client(bma.blockchain.parameters)
print(response) print(response)
# Get the current block # Get the current block
response = await bma.blockchain.current(client) response = await client(bma.blockchain.current)
print(response) print(response)
# Get the block number 10 # Get the block number 10
response = await bma.blockchain.block(client, 10) response = await client(bma.blockchain.block, 10)
print(response) print(response)
# Close client aiohttp session # Close client aiohttp session
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment