Skip to content

Commit

Permalink
Merge pull request #51 from Multi-Agent-io/development
Browse files Browse the repository at this point in the history
reqres functionality
  • Loading branch information
Vourhey authored Apr 12, 2022
2 parents 724af30 + 30f0dc4 commit 8df2642
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,37 @@ More info may be found with
$ robonomics_interface --help
```

## REQRES API

There is a functionality for a direct connection to server based on Robonomics node.

```python
interface = RI.RobonomicsInterface()
reqres = RI.ReqRes(interface)
reqres.p2p_get(<Multiaddr of server>,<GET request>)
reqres.p2p_ping(<Multiaddr of server>)
```

#### Example of usage:

Download sample server [here](https://github.com/airalab/robonomics/tree/master/protocol/examples/reqres).
Start this server with local ip: `cargo run "/ip4/127.0.0.1/tcp/61240"`. Then, in other terminal write small execute this
script:

```python
import robonomicsinterface as RI
interface = RI.RobonomicsInterface(remote_ws = "ws://127.0.0.1:9944"
reqres = RI.ReqRes(interface)
print(reqres.p2p_get("/ip4/127.0.0.1/tcp/61240/<PeerId>","GET")) # PeerId - you will see in server logs
```

## JSON RPC
*WARNING: THIS MODULE IS UNDER CONSTRUCTIONS, USE AT YOUR OWN RISK! TO BE UPDATED SOON.*
There is a way to implement robonomics pubsub rpc calls:

```python3
interface = RI.RobonomicsInterface()
pubsub = PubSub(interface)
pubsub = RI.PubSub(interface)
pubsub.peer()
```

Expand Down
37 changes: 36 additions & 1 deletion docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,50 @@ More info may be found with
$ robonomics_interface --help
REQRES API
++++++++++

There is a functionality for a direct connection to server based on Robonomics node.

.. code-block:: python
interface = RI.RobonomicsInterface()
reqres = RI.ReqRes(interface)
reqres.p2p_get(<Multiaddr of server>,<GET request>)
reqres.p2p_ping(<Multiaddr of server>)
Example of usage
~~~~~~~~~~~~~~~~

Download sample server `here <https://github.com/airalab/robonomics/tree/master/protocol/examples/reqres>`__.
Start this server with local ip (Rust (with cargo) installation process described `here <https://www.rust-lang.org/tools/install>`__):

.. code-block:: console
cargo run "/ip4/127.0.0.1/tcp/61240"
Then, in other terminal write small execute this script:

.. code-block:: python
import robonomicsinterface as RI
interface = RI.RobonomicsInterface(remote_ws = "ws://127.0.0.1:9944") # requires local node
reqres = RI.ReqRes(interface)
print(reqres.p2p_get("/ip4/127.0.0.1/tcp/61240/<PeerId>","GET")) # PeerId - you will see in server logs
This code sample requires local node launched. `PeerId` is obtained when launching server.

JSON RPC
++++++++
*WARNING: THIS MODULE IS UNDER CONSTRUCTIONS, USE AT YOUR OWN RISK! TO BE UPDATED SOON.*

There is a way to implement robonomics pubsub rpc calls:

.. code-block:: python
interface = RI.RobonomicsInterface()
pubsub = PubSub(interface)
pubsub = RI.PubSub(interface)
pubsub.peer()
This is an evolving package, it may have errors and lack of functionality, fixes are coming.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "robonomics-interface"
version = "0.10.1"
version = "0.11.1"
description = "Robonomics wrapper over https://github.com/polkascan/py-substrate-interface created to facilitate programming with Robonomics"
authors = ["Pavel Tarasov <p040399@outlook.com>"]
license = "Apache-2.0"
Expand Down
54 changes: 52 additions & 2 deletions robonomicsinterface/RobonomicsInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,12 @@ def __init__(
"""

self._subscriber_interface: RobonomicsInterface = interface
self._subscriber_interface: substrate.SubstrateInterface = substrate.SubstrateInterface(
url=interface.remote_ws,
ss58_format=32,
type_registry_preset="substrate-node-template",
type_registry=interface.type_registry,
)

self._event: SubEvent = subscribed_event
self._callback: callable = subscription_handler
Expand Down Expand Up @@ -1034,7 +1039,7 @@ def _event_callback(self, index_obj: tp.Any, update_nr: int, subscription_id: in
"""

if update_nr != 0:
chain_events: list = self._subscriber_interface.custom_chainstate("System", "Events")
chain_events: list = self._subscriber_interface.query("System", "Events").value
for events in chain_events:
if events["event_id"] == self._event.value:
if self._target_address is None:
Expand All @@ -1044,3 +1049,48 @@ def _event_callback(self, index_obj: tp.Any, update_nr: int, subscription_id: in
in self._target_address
):
self._callback(events["event"]["attributes"]) # address-targeted


class ReqRes:
"""
Class for handling Robonomics reqres rpc requests
"""

def __init__(self, interface: RobonomicsInterface) -> None:
"""
Initiate an instance for further use.
:param interface: RobonomicsInterface instance.
"""
self._reqres_interface = interface

def p2p_get(self, address: str, message: str, result_handler: tp.Optional[tp.Callable] = None):
"""
Returns for p2p rpc get response.
:param address: Multiaddr address of the peer to connect to. For example:
"/ip4/127.0.0.1/tcp/61240/<Peer ID of server>."
This ID may be obtained on node/server initialization.
:param message: Request message. "GET" for example.
:param result_handler: Callback function that processes the result received from the node. This function accepts
one argument - response.
"""

return self._reqres_interface.custom_rpc_request("p2p_get", [address, message], result_handler)

def p2p_ping(self, address: str, result_handler: tp.Optional[tp.Callable] = None):

"""
Returns for reqres p2p rpc ping to server response
:param address: Multiaddr address of the peer to connect to. For example:
"/ip4/127.0.0.1/tcp/61240/<Peer ID of server>."
This ID may be obtained on node/server initialization.
:param result_handler: Callback function that processes the result received from the node. This function accepts
one argument - response.
"""

return self._reqres_interface.custom_rpc_request("p2p_ping", [address], result_handler)
2 changes: 1 addition & 1 deletion robonomicsinterface/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .RobonomicsInterface import PubSub, RobonomicsInterface, SubEvent, Subscriber
from .RobonomicsInterface import PubSub, RobonomicsInterface, SubEvent, Subscriber, ReqRes

0 comments on commit 8df2642

Please sign in to comment.