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

issue #52 refactor api.client.API class and grammars.ouput module to pass mypy check

API class parameter module is removed
API class optional parameter headers is added
parent 9a46039f
Branches
Tags
No related merge requests found
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# vit # vit
import json import json
import logging import logging
from typing import Callable, Union, Any from typing import Callable, Union, Any, Optional
import aiohttp import aiohttp
import jsonschema import jsonschema
...@@ -89,18 +89,16 @@ class API(object): ...@@ -89,18 +89,16 @@ class API(object):
""" """
API is a class used as an abstraction layer over the request library (AIOHTTP). API is a class used as an abstraction layer over the request library (AIOHTTP).
""" """
schema = {}
def __init__(self, connection_handler: endpoint.ConnectionHandler, module: str) -> None: def __init__(self, connection_handler: endpoint.ConnectionHandler, headers: Optional[dict] = None) -> None:
""" """
Asks a module in order to create the url used then by derivated classes. Asks a module in order to create the url used then by derivated classes.
:param connection_handler: Connection handler :param connection_handler: Connection handler
:param module: Module path :param headers: Headers dictionary (optional, default None)
""" """
self.module = module
self.connection_handler = connection_handler self.connection_handler = connection_handler
self.headers = {} self.headers = {} if headers is None else headers
def reverse_url(self, scheme: str, path: str) -> str: def reverse_url(self, scheme: str, path: str) -> str:
""" """
...@@ -110,19 +108,19 @@ class API(object): ...@@ -110,19 +108,19 @@ class API(object):
:param path: Path of the url :param path: Path of the url
:return: str :return: str
""" """
# remove starting slash in path if present
path = path.lstrip('/')
server, port = self.connection_handler.server, self.connection_handler.port server, port = self.connection_handler.server, self.connection_handler.port
if self.connection_handler.path: if self.connection_handler.path:
url = '{scheme}://{server}:{port}/{path}/{module}'.format(scheme=scheme, url = '{scheme}://{server}:{port}/{path}'.format(scheme=scheme,
server=server, server=server,
port=port, port=port,
path=path, path=path)
module=self.module)
else: else:
url = '{scheme}://{server}:{port}/{module}'.format(scheme=scheme, url = '{scheme}://{server}:{port}/'.format(scheme=scheme,
server=server, server=server,
port=port, port=port)
module=self.module)
return url + path return url + path
...@@ -227,7 +225,7 @@ class Client: ...@@ -227,7 +225,7 @@ class Client:
if params is None: if params is None:
params = dict() params = dict()
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_get(url_path, **params) response = await client.requests_get(url_path, **params)
...@@ -258,7 +256,7 @@ class Client: ...@@ -258,7 +256,7 @@ class Client:
if params is None: if params is None:
params = dict() params = dict()
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(url_path, **params) response = await client.requests_post(url_path, **params)
...@@ -283,7 +281,7 @@ class Client: ...@@ -283,7 +281,7 @@ class Client:
:param path: the url path :param path: the url path
:rtype: aiohttp.ClientWebSocketResponse :rtype: aiohttp.ClientWebSocketResponse
""" """
client = API(self.endpoint.conn_handler(self.session, self.proxy), '') client = API(self.endpoint.conn_handler(self.session, self.proxy))
return client.connect_ws(path) return client.connect_ws(path)
async def close(self): async def close(self):
......
from pypeg2 import * from pypeg2 import re, attr, Keyword, Enum, contiguous, maybe_some, whitespace, K
from ..constants import PUBKEY_REGEX, HASH_REGEX from ..constants import PUBKEY_REGEX, HASH_REGEX
...@@ -81,6 +81,9 @@ class Operator(Keyword): ...@@ -81,6 +81,9 @@ class Operator(Keyword):
class Condition(str): class Condition(str):
grammar = None
@classmethod @classmethod
def token(cls, left, op=None, right=None): def token(cls, left, op=None, right=None):
condition = cls() condition = cls()
......
...@@ -24,8 +24,8 @@ class TestBmaApi(unittest.TestCase): ...@@ -24,8 +24,8 @@ class TestBmaApi(unittest.TestCase):
async def go(): async def go():
endpoint = BMAEndpoint("test.com", "124.2.2.1", "2001:0db8:0000:85a3:0000:0000:ac1f:8001 ", 9092) endpoint = BMAEndpoint("test.com", "124.2.2.1", "2001:0db8:0000:85a3:0000:0000:ac1f:8001 ", 9092)
session = aiohttp.ClientSession() session = aiohttp.ClientSession()
api = API(endpoint.conn_handler(session), "any") api = API(endpoint.conn_handler(session), )
self.assertEqual(api.reverse_url("http", "/test/url"), "http://test.com:9092/any/test/url") self.assertEqual(api.reverse_url("http", "/test/url"), "http://test.com:9092/test/url")
await session.close() await session.close()
self.loop.run_until_complete(go()) self.loop.run_until_complete(go())
...@@ -34,8 +34,8 @@ class TestBmaApi(unittest.TestCase): ...@@ -34,8 +34,8 @@ class TestBmaApi(unittest.TestCase):
endpoint = BMAEndpoint("", "124.2.2.1", "", 9092) endpoint = BMAEndpoint("", "124.2.2.1", "", 9092)
session = aiohttp.ClientSession() session = aiohttp.ClientSession()
api = API(endpoint.conn_handler(session), "any") api = API(endpoint.conn_handler(session), )
self.assertEqual(api.reverse_url("http", "/test/url"), "http://124.2.2.1:9092/any/test/url") self.assertEqual(api.reverse_url("http", "/test/url"), "http://124.2.2.1:9092/test/url")
await session.close() await session.close()
self.loop.run_until_complete(go()) self.loop.run_until_complete(go())
...@@ -43,9 +43,9 @@ class TestBmaApi(unittest.TestCase): ...@@ -43,9 +43,9 @@ class TestBmaApi(unittest.TestCase):
async def go(): async def go():
endpoint = BMAEndpoint("", "", "2001:0db8:0000:85a3:0000:0000:ac1f:8001", 9092) endpoint = BMAEndpoint("", "", "2001:0db8:0000:85a3:0000:0000:ac1f:8001", 9092)
session = aiohttp.ClientSession() session = aiohttp.ClientSession()
api = API(endpoint.conn_handler(session), "any") api = API(endpoint.conn_handler(session), )
self.assertEqual(api.reverse_url("http", "/test/url"), self.assertEqual(api.reverse_url("http", "/test/url"),
"http://[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:9092/any/test/url") "http://[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:9092/test/url")
await session.close() await session.close()
self.loop.run_until_complete(go()) self.loop.run_until_complete(go())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment