From a1a88ce3632abc94fc6f18f440ffb415917f8cc8 Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Wed, 11 Nov 2020 00:07:43 +0100
Subject: [PATCH] [enh] #59 modify graphQL example to request gva prototype

Improve client.query output on http error
---
 duniterpy/api/client.py     | 10 +++++++---
 examples/request_graphql.py | 21 +++++++++++----------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py
index 1df2880a..a5fa7fa2 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 d50e956e..8264876c 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()
-- 
GitLab