From 0191f9228842c04873f7182887e389ed455cc469 Mon Sep 17 00:00:00 2001 From: Vincent Texier <vit@free.fr> Date: Fri, 18 Dec 2020 09:14:33 +0100 Subject: [PATCH] [fix] #140 fix errors after merge resolve conflicts --- duniterpy/api/client.py | 4 ++-- duniterpy/helpers/network.py | 6 +++--- examples/request_available_nodes.py | 15 ++++----------- tests/api/bma/test_network.py | 3 +-- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py index 788cb6e5..37ff1ecd 100644 --- a/duniterpy/api/client.py +++ b/duniterpy/api/client.py @@ -260,7 +260,7 @@ class API: response = request.urlopen(duniter_request, timeout=15) # type: HTTPResponse if response.status != 200: - content = str(response.read()) + content = response.read().decode("utf-8") if bma_errors: try: error_data = parse_error(content) @@ -276,7 +276,7 @@ class API: # get response content return_response = copy.copy(response) - content = str(response.read()) + content = response.read().decode("utf-8") response.close() # if schema supplied... diff --git a/duniterpy/helpers/network.py b/duniterpy/helpers/network.py index 70da09b1..af151223 100644 --- a/duniterpy/helpers/network.py +++ b/duniterpy/helpers/network.py @@ -26,7 +26,7 @@ from itertools import groupby from duniterpy.key import VerifyingKey -async def get_available_nodes(client: Client) -> List[List[Dict[str, Any]]]: +def get_available_nodes(client: Client) -> List[List[Dict[str, Any]]]: """ Get available nodes grouped and sorted by descending blockstamp @@ -52,8 +52,8 @@ async def get_available_nodes(client: Client) -> List[List[Dict[str, Any]]]: :return: """ # capture heads and peers - heads_response = await client(bma.network.ws2p_heads) - peers_response = await client(bma.network.peers) + heads_response = client(bma.network.ws2p_heads) + peers_response = client(bma.network.peers) # get heads instances from WS2P messages heads = [] diff --git a/examples/request_available_nodes.py b/examples/request_available_nodes.py index 1b9a208e..e4a394bd 100644 --- a/examples/request_available_nodes.py +++ b/examples/request_available_nodes.py @@ -15,8 +15,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ -import asyncio - from duniterpy.helpers import network from duniterpy.api.client import Client @@ -26,20 +24,19 @@ from duniterpy.api.client import Client # or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT] [PATH] # Here we use the secure BASIC_MERKLED_API (BMAS) BMAS_ENDPOINT = "BMAS g1-test.duniter.org 443" -BMAS_ENDPOINT = "BMAS g1.duniter.org 443" ################################################ -async def main(): +def main(): """ Main code """ # Create Client from endpoint string in Duniter format client = Client(BMAS_ENDPOINT) - groups = await network.get_available_nodes(client) + groups = network.get_available_nodes(client) for group in groups: block = group[0]["head"].blockstamp print(f"block {block} shared by {len(group)} nodes") @@ -50,10 +47,6 @@ async def main(): for endpoint in node["endpoints"]: print(endpoint) - # Close client aiohttp session - await client.close() - -# 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()) +if __name__ == "__main__": + main() diff --git a/tests/api/bma/test_network.py b/tests/api/bma/test_network.py index c960697d..ac7f1543 100644 --- a/tests/api/bma/test_network.py +++ b/tests/api/bma/test_network.py @@ -21,10 +21,9 @@ import jsonschema from jsonschema import ValidationError, SchemaError from duniterpy.api.bma import network -from tests.api.webserver import WebFunctionalSetupMixin, web -class TestBmaNetwork(WebFunctionalSetupMixin, unittest.TestCase): +class TestBmaNetwork(unittest.TestCase): def test_peers(self): json_sample = { "peers": [ -- GitLab