diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py index 1df2880ac650763e786f454f8a6250bbc6bd059a..a5fa7fa20d6fc7c0fe9aeba8a2763297cb3c0eef 100644 --- a/duniterpy/api/client.py +++ b/duniterpy/api/client.py @@ -19,6 +19,7 @@ import json import logging from typing import Callable, Union, Any, Optional, Dict +import aiohttp import jsonschema from aiohttp import ClientResponse, ClientSession, ClientWebSocketResponse from aiohttp.client import _WSRequestContextManager @@ -508,11 +509,14 @@ class Client: # return the chosen type result = response # type: Any - if rtype == RESPONSE_TEXT: + if rtype == RESPONSE_TEXT or response.status > 399: result = await response.text() elif rtype == RESPONSE_JSON: - result = await response.json() - + try: + result = await response.json() + except aiohttp.client_exceptions.ContentTypeError as exception: + logging.error("Response is not a json format") + # return response to debug... return result async def connect_ws(self, path: str = "") -> WSConnection: diff --git a/examples/request_graphql.py b/examples/request_graphql.py index d50e956e0c41d0d5259a28800509837c4802377a..8264876c22283fcb64c8139d0befb2174c3bee20 100644 --- a/examples/request_graphql.py +++ b/examples/request_graphql.py @@ -11,7 +11,7 @@ from graphql.error import GraphQLSyntaxError # You can either use a complete defined endpoint : [NAME_OF_THE_API] [DOMAIN] [IPv4] [IPv6] [PORT] # or the simple definition : [NAME_OF_THE_API] [DOMAIN] [PORT] # Here we use the secure BASIC_MERKLED_API (BMAS) for standard http over ssl requests -SWAPI_ENDPOINT = "BMAS swapi.graph.cool 443" +SWAPI_ENDPOINT = "BMAS g1.librelois.fr 443 gva" ################################################ @@ -27,16 +27,14 @@ async def main(): # convert response dict to schema schema = build_client_schema(response["data"]) - # create all films query - query = """query { - allFilms { - title, - characters { - name + # create currentUd query + query = """{ + currentUd { + amount + } } - } - } """ + # check query syntax try: ast_document = language.parse(query) @@ -53,7 +51,10 @@ async def main(): # send valid query to api response = await client.query(query) - print(json.dumps(response, indent=2)) + if isinstance(response, str): + print(response) + else: + print(json.dumps(response, indent=2)) # Close client aiohttp session await client.close()