Skip to content
Snippets Groups Projects
Commit a1a88ce3 authored by Vincent Texier's avatar Vincent Texier
Browse files

[enh] #59 modify graphQL example to request gva prototype

Improve client.query output on http error
parent c4db9540
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ import json ...@@ -19,6 +19,7 @@ import json
import logging import logging
from typing import Callable, Union, Any, Optional, Dict from typing import Callable, Union, Any, Optional, Dict
import aiohttp
import jsonschema import jsonschema
from aiohttp import ClientResponse, ClientSession, ClientWebSocketResponse from aiohttp import ClientResponse, ClientSession, ClientWebSocketResponse
from aiohttp.client import _WSRequestContextManager from aiohttp.client import _WSRequestContextManager
...@@ -508,11 +509,14 @@ class Client: ...@@ -508,11 +509,14 @@ class Client:
# return the chosen type # return the chosen type
result = response # type: Any result = response # type: Any
if rtype == RESPONSE_TEXT: if rtype == RESPONSE_TEXT or response.status > 399:
result = await response.text() result = await response.text()
elif rtype == RESPONSE_JSON: elif rtype == RESPONSE_JSON:
try:
result = await response.json() 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 return result
async def connect_ws(self, path: str = "") -> WSConnection: async def connect_ws(self, path: str = "") -> WSConnection:
......
...@@ -11,7 +11,7 @@ from graphql.error import GraphQLSyntaxError ...@@ -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] # 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] # 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 # 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(): ...@@ -27,16 +27,14 @@ async def main():
# convert response dict to schema # convert response dict to schema
schema = build_client_schema(response["data"]) schema = build_client_schema(response["data"])
# create all films query # create currentUd query
query = """query { query = """{
allFilms { currentUd {
title, amount
characters {
name
}
} }
} }
""" """
# check query syntax # check query syntax
try: try:
ast_document = language.parse(query) ast_document = language.parse(query)
...@@ -53,6 +51,9 @@ async def main(): ...@@ -53,6 +51,9 @@ async def main():
# send valid query to api # send valid query to api
response = await client.query(query) response = await client.query(query)
if isinstance(response, str):
print(response)
else:
print(json.dumps(response, indent=2)) print(json.dumps(response, indent=2))
# Close client aiohttp session # Close client aiohttp session
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment