From ef5a1ff3ea25ee2a48ac6388f64edce368cf6217 Mon Sep 17 00:00:00 2001
From: inso <insomniak.fr@gmaiL.com>
Date: Fri, 18 Nov 2016 16:11:14 +0100
Subject: [PATCH] Fix examples
---
duniterpy/api/bma/api.py | 5 ++--
duniterpy/api/bma/blockchain.py | 5 ++--
duniterpy/api/bma/network.py | 6 ++---
duniterpy/api/bma/node.py | 4 ++--
examples/create_and_publish_identity.py | 32 +++++++++++--------------
examples/create_public_key.py | 22 +++++------------
examples/save_revoke_document.py | 12 ++++------
examples/send_certification.py | 17 +++++++------
examples/send_membership.py | 17 ++++++++-----
examples/send_transaction.py | 13 ++++++----
10 files changed, 64 insertions(+), 69 deletions(-)
diff --git a/duniterpy/api/bma/api.py b/duniterpy/api/bma/api.py
index 8d213cd6..47b577fd 100644
--- a/duniterpy/api/bma/api.py
+++ b/duniterpy/api/bma/api.py
@@ -101,11 +101,12 @@ async def parse_response(response, schema):
"""
try:
data = await response.json()
+ response.close()
if schema is not None:
jsonschema.validate(data, schema)
return data
- except (TypeError, json.decoder.JSONDecodeError):
- raise jsonschema.ValidationError("Could not parse json")
+ except (TypeError, json.decoder.JSONDecodeError) as e:
+ raise jsonschema.ValidationError("Could not parse json : {0}".format(str(e)))
class API(object):
diff --git a/duniterpy/api/bma/blockchain.py b/duniterpy/api/bma/blockchain.py
index d5bfb335..e4cb26a0 100644
--- a/duniterpy/api/bma/blockchain.py
+++ b/duniterpy/api/bma/blockchain.py
@@ -326,7 +326,7 @@ async def membership(connection, membership):
:param duniterpy.api.bma.ConnectionHandler connection: Connection handler instance
:param str membership: Membership signed raw document
- :rtype: dict
+ :rtype: aiohttp.ClientResponse
"""
client = API(connection, URL_PATH)
@@ -350,7 +350,8 @@ async def block(connection, number=0, block=None, signature=None):
# GET block
r = await client.requests_get('/block/%d' % number)
- return await parse_response(r, BLOCK_SCHEMA)
+ data = await parse_response(r, BLOCK_SCHEMA)
+ return data
async def current(connection):
"""
diff --git a/duniterpy/api/bma/network.py b/duniterpy/api/bma/network.py
index 66887671..b8a63998 100644
--- a/duniterpy/api/bma/network.py
+++ b/duniterpy/api/bma/network.py
@@ -16,7 +16,7 @@
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
-from duniterpy.api.bma import API, logging
+from duniterpy.api.bma import API, logging, parse_response
logger = logging.getLogger("duniter/network")
@@ -110,7 +110,7 @@ async def peering(connection):
client = API(connection, URL_PATH)
r = await client.requests_get('/peering')
- return await client.parse_response(r, PEERING_SCHEMA)
+ return await parse_response(r, PEERING_SCHEMA)
async def peers(connection, entry=None, signature=None):
"""
@@ -130,7 +130,7 @@ async def peers(connection, entry=None, signature=None):
# GET Peers
r = await client.requests_get('/peering/peers')
- return await client.parse_response(r, PEERS_SCHEMA)
+ return await parse_response(r, PEERS_SCHEMA)
# async def status(connection):
# """
diff --git a/duniterpy/api/bma/node.py b/duniterpy/api/bma/node.py
index cd48e1b1..c6b26411 100644
--- a/duniterpy/api/bma/node.py
+++ b/duniterpy/api/bma/node.py
@@ -16,7 +16,7 @@
# Caner Candan <caner@candan.fr>, http://caner.candan.fr
#
-from duniterpy.api.bma import API, logging
+from duniterpy.api.bma import API, logging, parse_response
logger = logging.getLogger("duniter/node")
@@ -53,4 +53,4 @@ async def summary(connection):
client = API(connection, URL_PATH)
r = await client.requests_get('/summary')
- return await client.parse_response(r, schema)
+ return await parse_response(r, schema)
diff --git a/examples/create_and_publish_identity.py b/examples/create_and_publish_identity.py
index c7837667..164fe5dc 100644
--- a/examples/create_and_publish_identity.py
+++ b/examples/create_and_publish_identity.py
@@ -1,5 +1,6 @@
import asyncio
import aiohttp
+import getpass
import duniterpy.api.bma as bma
from duniterpy.documents import BMAEndpoint, BlockUID, Identity
@@ -13,16 +14,10 @@ from duniterpy.key import SigningKey
# Here we use the BASIC_MERKLED_API
BMA_ENDPOINT = "BASIC_MERKLED_API cgeek.fr 9330"
-# Credentials should be prompted or kept in a separate secure file
-# create a file with the salt on the first line and the password on the second line
-# the script will load them from the file
-FROM_CREDENTIALS_FILE = "/home/username/.credentials.txt"
-
# Your unique identifier in the Web of Trust
UID = "MyIdentity"
################################################
-
# Latest duniter-python-api is asynchronous and you have to create an aiohttp session to send request
# ( http://pythonhosted.org/aiohttp )
AIOHTTP_SESSION = aiohttp.ClientSession()
@@ -65,30 +60,31 @@ async def main():
"""
Main code
"""
- # connection handler from BMA endpoint
- connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()
+ # connection handler from BMA endpoint
+ connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION)
# capture current block to get version and currency and blockstamp
current_block = await bma.blockchain.current(connection)
- # load credentials from a text file
- salt, password = open(FROM_CREDENTIALS_FILE).readlines()
+ # prompt hidden user entry
+ salt = getpass.getpass("Enter your passphrase (salt): ")
- # cleanup newlines
- salt, password = salt.strip(), password.strip()
+ # prompt hidden user entry
+ password = getpass.getpass("Enter your password: ")
# create our signed identity document
identity = get_identity_document(current_block, UID, salt, password)
# send the identity document to the node
response = await bma.wot.add(connection, identity.signed_raw())
-
- print(response)
-
+ if response.status == 200:
+ print(await response.text())
+ else:
+ print("Error while publishing identity : {0}".format(response.text()))
response.close()
-with AIOHTTP_SESSION:
+# Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.
+# ( https://docs.python.org/3/library/asyncio.html )
- # Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.
- # ( https://docs.python.org/3/library/asyncio.html )
+with AIOHTTP_SESSION:
asyncio.get_event_loop().run_until_complete(main())
diff --git a/examples/create_public_key.py b/examples/create_public_key.py
index ea7ab135..8ef741e1 100644
--- a/examples/create_public_key.py
+++ b/examples/create_public_key.py
@@ -1,25 +1,15 @@
+import getpass
from duniterpy.key import SigningKey
-# CONFIG #######################################
-
-# CREDENTIALS in Duniter are a couple of strings:
-# - A secret pass-phrase
-# - A password
-# They create a seed which create keys (some are private and one is public)
-
-# Credentials should be prompted or kept in a separate secure file
-# create a file with the salt on the first line and the password on the second line
-# the script will load them from the file
-CREDENTIALS_FILE_PATH = "/home/username/.credentials.txt"
-
################################################
if __name__ == '__main__':
- # Load your credentials from a text file
- salt, password = open(CREDENTIALS_FILE_PATH).readlines()
- # Cleanup newlines
- salt, password = salt.strip(), password.strip()
+ # prompt hidden user entry
+ salt = getpass.getpass("Enter your passphrase (salt): ")
+
+ # prompt hidden user entry
+ password = getpass.getpass("Enter your password: ")
# Create key object
key = SigningKey(salt, password)
diff --git a/examples/save_revoke_document.py b/examples/save_revoke_document.py
index ce1a4d32..589a8188 100644
--- a/examples/save_revoke_document.py
+++ b/examples/save_revoke_document.py
@@ -29,9 +29,6 @@ BMA_ENDPOINT = "BASIC_MERKLED_API cgeek.fr 9330"
REVOKE_DOCUMENT_FILE_PATH = os.path.join(home_path, "duniter_account_revoke_document.txt")
################################################
-
-# Latest duniter-python-api is asynchronous and you have to create an aiohttp session to send request
-# ( http://pythonhosted.org/aiohttp )
AIOHTTP_SESSION = aiohttp.ClientSession()
# Current protocole version
@@ -76,7 +73,7 @@ async def get_identity_document(connection, currency, pubkey):
)
-async def get_revoke_document(identity, salt, password):
+def get_revoke_document(identity, salt, password):
"""
Generate account revocation document for given identity
@@ -115,8 +112,7 @@ async def main():
exit(0)
# connection handler from BMA endpoint
- connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()
-
+ connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION)
# capture current block to get currency name
current_block = await bma.blockchain.current(connection)
@@ -124,7 +120,7 @@ async def main():
identity_document = await get_identity_document(connection, current_block['currency'], pubkey)
# get the revoke document
- revoke_document = await get_revoke_document(identity_document, salt, password)
+ revoke_document = get_revoke_document(identity_document, salt, password)
# save revoke document in a file
fp = open(REVOKE_DOCUMENT_FILE_PATH, 'w')
@@ -134,8 +130,8 @@ async def main():
# document saved
print("Revoke document saved in %s" % REVOKE_DOCUMENT_FILE_PATH)
-with AIOHTTP_SESSION:
+with AIOHTTP_SESSION:
# Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.
# ( https://docs.python.org/3/library/asyncio.html )
asyncio.get_event_loop().run_until_complete(main())
diff --git a/examples/send_certification.py b/examples/send_certification.py
index 42d890c2..bb9b57f8 100644
--- a/examples/send_certification.py
+++ b/examples/send_certification.py
@@ -1,5 +1,6 @@
import asyncio
import aiohttp
+import getpass
import duniterpy.api.bma as bma
from duniterpy.documents import BMAEndpoint, BlockUID, Identity, Certification
from duniterpy.key import SigningKey
@@ -100,7 +101,7 @@ async def main():
Main code
"""
# connection handler from BMA endpoint
- connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()
+ connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION)
# capture current block to get version and currency and blockstamp
current_block = await bma.blockchain.current(connection)
@@ -108,11 +109,11 @@ async def main():
# create our SelfCertification document to sign the Certification document
identity = await get_identity_document(connection, current_block, TO_PUBKEY)
- # load credentials from a text file
- salt, password = open(FROM_CREDENTIALS_FILE).readlines()
+ # prompt hidden user entry
+ salt = getpass.getpass("Enter your passphrase (salt): ")
- # cleanup newlines
- salt, password = salt.strip(), password.strip()
+ # prompt hidden user entry
+ password = getpass.getpass("Enter your password: ")
# send the Certification document to the node
certification = get_certification_document(current_block, identity, FROM_PUBKEY, salt, password)
@@ -120,8 +121,10 @@ async def main():
# Here we request for the path wot/certify
response = await bma.wot.certify(connection, certification.signed_raw(identity))
- print(response)
-
+ if response.status_code == 200:
+ print(await response.text())
+ else:
+ print("Error while publishing certification : {0}".format(response.text()))
response.close()
with AIOHTTP_SESSION:
diff --git a/examples/send_membership.py b/examples/send_membership.py
index 171a35e9..f3ca5be1 100644
--- a/examples/send_membership.py
+++ b/examples/send_membership.py
@@ -1,5 +1,6 @@
import asyncio
import aiohttp
+import getpass
import duniterpy.api.bma as bma
from duniterpy.documents import BMAEndpoint, BlockUID, Identity, Membership
@@ -103,16 +104,16 @@ async def main():
Main code
"""
# connection handler from BMA endpoint
- connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()
+ connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION)
# capture current block to get version and currency and blockstamp
current_block = await bma.blockchain.current(connection)
- # load credentials from a text file
- salt, password = open(FROM_CREDENTIALS_FILE).readlines()
+ # prompt hidden user entry
+ salt = getpass.getpass("Enter your passphrase (salt): ")
- # cleanup newlines
- salt, password = salt.strip(), password.strip()
+ # prompt hidden user entry
+ password = getpass.getpass("Enter your password: ")
# create our signed identity document
identity = get_identity_document(current_block, UID, salt, password)
@@ -123,7 +124,11 @@ async def main():
# send the membership document to the node
response = await bma.blockchain.membership(connection, membership.signed_raw())
- print(response)
+ if response.status == 200:
+ print(await response.text())
+ else:
+ print("Error while publishing membership : {0}".format(response.text()))
+
response.close()
diff --git a/examples/send_transaction.py b/examples/send_transaction.py
index 2bb237e8..4facb786 100644
--- a/examples/send_transaction.py
+++ b/examples/send_transaction.py
@@ -1,8 +1,8 @@
import asyncio
-
+import getpass
import aiohttp
-import duniterpy.api.bma as bma
+from duniterpy.api import bma
from duniterpy.documents import BMAEndpoint, BlockUID, Transaction
from duniterpy.documents.transaction import InputSource, OutputSource, Unlock, SIGParameter
from duniterpy.grammars.output import Condition, SIG
@@ -105,7 +105,7 @@ async def main():
Main code
"""
# connection handler from BMA endpoint
- connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler()
+ connection = BMAEndpoint.from_inline(BMA_ENDPOINT).conn_handler(AIOHTTP_SESSION)
# capture current block to get version and currency and blockstamp
current_block = await bma.blockchain.current(connection)
@@ -138,10 +138,13 @@ async def main():
# send the Transaction document to the node
response = await bma.tx.process(connection, transaction.signed_raw())
- print(response)
-
+ if response.status == 200:
+ print(await response.text())
+ else:
+ print("Error while publishing transaction : {0}".format(response.text()))
response.close()
+
with AIOHTTP_SESSION:
# Latest duniter-python-api is asynchronous and you have to use asyncio, an asyncio loop and a "as" on the data.
--
GitLab