Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit d04a8d2

Browse files
committed
docs: Add 'Fetch Data' tutorial
- Introduce numbering in headings too.
1 parent 5a3aecd commit d04a8d2

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

docs/tutorials.rst

+64-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Let's get to it then!
1313

1414
.. py:currentmodule:: iota
1515
16-
Hello Node
17-
----------
16+
1. Hello Node
17+
-------------
1818
In this example, you will learn how to:
1919

2020
- **Import the** ``iota`` **package into your application.**
@@ -73,8 +73,8 @@ we could list the ``features`` of the node::
7373

7474
pprint(response['features'])
7575

76-
Send Data
77-
---------
76+
2. Send Data
77+
------------
7878
In this example, you will learn how to:
7979

8080
- **Encode data to be stored on the Tangle.**
@@ -167,6 +167,66 @@ Observe how we extract the transaction hash from the response ``dict``. We take
167167
the first element of the bundle, as it is just a sequence of transactions, and
168168
access its ``hash`` attribute.
169169

170+
3. Fetch Data
171+
-------------
172+
In this example, you will learn how to:
173+
174+
- **Fetch transaction objects from the Tangle based on a criteria.**
175+
- **Decode messages from transactions.**
176+
177+
Code
178+
~~~~
179+
.. literalinclude:: ../examples/tutorials/03_fetch_data.py
180+
:linenos:
181+
182+
Discussion
183+
~~~~~~~~~~
184+
.. literalinclude:: ../examples/tutorials/03_fetch_data.py
185+
:lines: 1-5
186+
:lineno-start: 1
187+
188+
The usual part again, but we also import ``TrytesDecodeError`` from
189+
``iota.codec``. We will use this to detect if the fetched trytes contain
190+
encoded text.
191+
192+
.. literalinclude:: ../examples/tutorials/03_fetch_data.py
193+
:lines: 7-10
194+
:lineno-start: 7
195+
196+
We declare an IOTA address on the Tangle to fetch data from. You can replace
197+
this address with your own from the previous example `2. Send Data`_, or just
198+
run it as it is.
199+
200+
.. literalinclude:: ../examples/tutorials/03_fetch_data.py
201+
:lines: 12-14
202+
:lineno-start: 12
203+
204+
We use :py:meth:`~Iota.find_transaction_objects` extended API method to gather
205+
the transactions that belong to our address. This method is also capable of
206+
returning :py:class:`Transaction` objects for bundle hashes, tags or approving
207+
transactions. Note that you can supply multiple of these, the method always
208+
returns a ``dict`` with a list of transactions.
209+
210+
.. note::
211+
212+
Remember, that the parameters need to be supplied as lists, even if
213+
there is only one value.
214+
215+
.. literalinclude:: ../examples/tutorials/03_fetch_data.py
216+
:lines: 16-29
217+
:lineno-start: 16
218+
219+
Finally, we extract the data we our looking for from the transaction objects.
220+
A :py:class:`Transaction` has several attributes, one of which is the
221+
``signature_message_fragment``. This contains the payload message for zero-value
222+
transactions, and the digital signature that authorizes spending for value
223+
transactions.
224+
225+
Since we are interested in data now, we try to decode its content (raw trytes)
226+
into text. We make use of ``TrytesDecodeError`` to detect if the attribute can
227+
be interpreted as text, and ``UnicodeDecodeError`` to filter out invalid
228+
text encoding.
229+
170230
.. _PyOTA Bug Tracker: https://github.com/iotaledger/iota.py/issues
171231
.. _bytestring: https://docs.python.org/3/library/stdtypes.html#bytes
172232
.. _tryte alphabet: https://docs.iota.org/docs/getting-started/0.1/introduction/ternary#tryte-encoding

examples/tutorials/03_fetch_data.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from iota import Iota, Address
2+
from iota.codecs import TrytesDecodeError
3+
4+
# Declare an API object
5+
api = Iota('https://nodes.devnet.iota.org:443', testnet=True)
6+
7+
# Address to fetch data from
8+
# Replace with your random generated address from Tutorial 2. to fetch the data
9+
# that you uploaded.
10+
addy = Address(b'WWO9DRAUDDSDSTTUPKJRNPSYLWAVQBBXISLKLTNDPVKOPMUERDUELLUPHNT9L9YWBDKOLYVWRAFRKIBLP')
11+
12+
print('Fetching data from the Tangle...')
13+
# Fetch the transaction objects of the address from the Tangle
14+
response = api.find_transaction_objects(addresses=[addy])
15+
16+
if not response['transactions']:
17+
print('Couldn\'t find data for the given address.')
18+
else:
19+
print('Found:')
20+
# Iterate over the fetched transaction objects
21+
for tx in response['transactions']:
22+
# data is in the signature_message_fragment attribute as trytes, we need
23+
# to decode it into a unicode string
24+
try:
25+
data = tx.signature_message_fragment.decode()
26+
# Ignore invalid or not unicode encoded data
27+
except (TrytesDecodeError, UnicodeDecodeError):
28+
continue
29+
print(data)

0 commit comments

Comments
 (0)