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
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:
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:
......
......@@ -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,6 +51,9 @@ async def main():
# send valid query to api
response = await client.query(query)
if isinstance(response, str):
print(response)
else:
print(json.dumps(response, indent=2))
# 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