diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py index 788cb6e59bb6406841960fb941a698e3ba91e6ab..37ff1ecdf19a3de3b46e915af6a9b78d83cc4aec 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 70da09b1d1276d9e91c79d48c4cd6f192d9f5230..af15122335c4378d126f127fc17a8fa4676a4e93 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 1b9a208ed07b83f7d869f3541025e5bb4607aa4c..e4a394bd7e2ea350dc7f393297678c75e37db261 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 c960697d0f7d082531d30127263ab2612ea1c668..ac7f1543dabd6854c03e05073154326c7f01aa81 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": [