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

[fix] #59 fix format with black

parent da6dbcac
No related branches found
No related tags found
No related merge requests found
...@@ -174,12 +174,20 @@ class API: ...@@ -174,12 +174,20 @@ class API:
error_data = parse_error(await response.text()) error_data = parse_error(await response.text())
raise DuniterError(error_data) raise DuniterError(error_data)
except (TypeError, jsonschema.ValidationError): except (TypeError, jsonschema.ValidationError):
raise ValueError('status code != 200 => %d (%s)' % (response.status, (await response.text()))) raise ValueError(
"status code != 200 => %d (%s)"
% (response.status, (await response.text()))
)
return response return response
async def requests(self, method: str = 'GET', path: str = '', data: Optional[dict] = None, async def requests(
_json: Optional[dict] = None) -> ClientResponse: self,
method: str = "GET",
path: str = "",
data: Optional[dict] = None,
_json: Optional[dict] = None,
) -> ClientResponse:
""" """
Generic requests wrapper on aiohttp Generic requests wrapper on aiohttp
...@@ -196,7 +204,7 @@ class API: ...@@ -196,7 +204,7 @@ class API:
elif _json is not None: elif _json is not None:
logging.debug(f"{method} : {url}, json={_json}") logging.debug(f"{method} : {url}, json={_json}")
# http header to send json body # http header to send json body
self.headers['Content-Type'] = 'application/json; charset=utf-8' self.headers["Content-Type"] = "application/json; charset=utf-8"
else: else:
logging.debug(f"{method} : {url}") logging.debug(f"{method} : {url}")
...@@ -207,7 +215,7 @@ class API: ...@@ -207,7 +215,7 @@ class API:
json=_json, json=_json,
headers=self.headers, headers=self.headers,
proxy=self.connection_handler.proxy, proxy=self.connection_handler.proxy,
timeout=15 timeout=15,
) )
return response return response
...@@ -234,8 +242,12 @@ class Client: ...@@ -234,8 +242,12 @@ class Client:
Main class to create an API client Main class to create an API client
""" """
def __init__(self, _endpoint: Union[str, endpoint.Endpoint], session: Optional[ClientSession] = None, def __init__(
proxy: Optional[str] = None) -> None: self,
_endpoint: Union[str, endpoint.Endpoint],
session: Optional[ClientSession] = None,
proxy: Optional[str] = None,
) -> None:
""" """
Init Client instance Init Client instance
...@@ -262,8 +274,13 @@ class Client: ...@@ -262,8 +274,13 @@ class Client:
self.session = session self.session = session
self.proxy = proxy self.proxy = proxy
async def get(self, url_path: str, params: Optional[dict] = None, rtype: str = RESPONSE_JSON, async def get(
schema: Optional[dict] = None) -> Any: self,
url_path: str,
params: Optional[dict] = None,
rtype: str = RESPONSE_JSON,
schema: Optional[dict] = None,
) -> Any:
""" """
GET request on endpoint host + url_path GET request on endpoint host + url_path
...@@ -295,8 +312,13 @@ class Client: ...@@ -295,8 +312,13 @@ class Client:
return result return result
async def post(self, url_path: str, params: Optional[dict] = None, rtype: str = RESPONSE_JSON, async def post(
schema: Optional[dict] = None) -> Any: self,
url_path: str,
params: Optional[dict] = None,
rtype: str = RESPONSE_JSON,
schema: Optional[dict] = None,
) -> Any:
""" """
POST request on endpoint host + url_path POST request on endpoint host + url_path
...@@ -328,8 +350,13 @@ class Client: ...@@ -328,8 +350,13 @@ class Client:
return result return result
async def query(self, query: str, variables: Optional[dict] = None, rtype: str = RESPONSE_JSON, async def query(
schema: Optional[dict] = None) -> Any: self,
query: str,
variables: Optional[dict] = None,
rtype: str = RESPONSE_JSON,
schema: Optional[dict] = None,
) -> Any:
""" """
GraphQL query or mutation request on endpoint GraphQL query or mutation request on endpoint
...@@ -339,17 +366,15 @@ class Client: ...@@ -339,17 +366,15 @@ class Client:
:param schema: Json Schema to validate response (optional, default None) :param schema: Json Schema to validate response (optional, default None)
:return: :return:
""" """
payload = { payload = {"query": query} # type: Dict[str, Union[str, dict]]
'query': query
} # type: Dict[str, Union[str, dict]]
if variables is not None: if variables is not None:
payload['variables'] = variables payload["variables"] = variables
client = API(self.endpoint.conn_handler(self.session, self.proxy)) client = API(self.endpoint.conn_handler(self.session, self.proxy))
# get aiohttp response # get aiohttp response
response = await client.requests('POST', _json=payload) response = await client.requests("POST", _json=payload)
# if schema supplied... # if schema supplied...
if schema is not None: if schema is not None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment