From 920564ed8b9df3673fa597056a009784927dbaed Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Fri, 20 Jul 2018 10:32:30 +0200
Subject: [PATCH] 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
---
 duniterpy/api/client.py      | 34 ++++++++++++++++------------------
 duniterpy/grammars/output.py |  5 ++++-
 tests/api/bma/test_bma.py    | 12 ++++++------
 3 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py
index ff796ab2..820035c5 100644
--- a/duniterpy/api/client.py
+++ b/duniterpy/api/client.py
@@ -4,7 +4,7 @@
 # vit
 import json
 import logging
-from typing import Callable, Union, Any
+from typing import Callable, Union, Any, Optional
 
 import aiohttp
 import jsonschema
@@ -89,18 +89,16 @@ class API(object):
     """
     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.
 
         :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.headers = {}
+        self.headers = {} if headers is None else headers
 
     def reverse_url(self, scheme: str, path: str) -> str:
         """
@@ -110,19 +108,19 @@ class API(object):
         :param path: Path of the url
         :return: str
         """
+        # remove starting slash in path if present
+        path = path.lstrip('/')
 
         server, port = self.connection_handler.server, self.connection_handler.port
         if self.connection_handler.path:
-            url = '{scheme}://{server}:{port}/{path}/{module}'.format(scheme=scheme,
-                                                                      server=server,
-                                                                      port=port,
-                                                                      path=path,
-                                                                      module=self.module)
+            url = '{scheme}://{server}:{port}/{path}'.format(scheme=scheme,
+                                                             server=server,
+                                                             port=port,
+                                                             path=path)
         else:
-            url = '{scheme}://{server}:{port}/{module}'.format(scheme=scheme,
-                                                               server=server,
-                                                               port=port,
-                                                               module=self.module)
+            url = '{scheme}://{server}:{port}/'.format(scheme=scheme,
+                                                       server=server,
+                                                       port=port)
 
         return url + path
 
@@ -227,7 +225,7 @@ class Client:
         if params is None:
             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
         response = await client.requests_get(url_path, **params)
@@ -258,7 +256,7 @@ class Client:
         if params is None:
             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
         response = await client.requests_post(url_path, **params)
@@ -283,7 +281,7 @@ class Client:
         :param path: the url path
         :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)
 
     async def close(self):
diff --git a/duniterpy/grammars/output.py b/duniterpy/grammars/output.py
index 8d6340ec..9e733899 100644
--- a/duniterpy/grammars/output.py
+++ b/duniterpy/grammars/output.py
@@ -1,4 +1,4 @@
-from pypeg2 import *
+from pypeg2 import re, attr, Keyword, Enum, contiguous, maybe_some, whitespace, K
 
 from ..constants import PUBKEY_REGEX, HASH_REGEX
 
@@ -81,6 +81,9 @@ class Operator(Keyword):
 
 
 class Condition(str):
+
+    grammar = None
+
     @classmethod
     def token(cls, left, op=None, right=None):
         condition = cls()
diff --git a/tests/api/bma/test_bma.py b/tests/api/bma/test_bma.py
index b6abe42a..fed236c0 100644
--- a/tests/api/bma/test_bma.py
+++ b/tests/api/bma/test_bma.py
@@ -24,8 +24,8 @@ class TestBmaApi(unittest.TestCase):
         async def go():
             endpoint = BMAEndpoint("test.com", "124.2.2.1", "2001:0db8:0000:85a3:0000:0000:ac1f:8001 ", 9092)
             session = aiohttp.ClientSession()
-            api = API(endpoint.conn_handler(session), "any")
-            self.assertEqual(api.reverse_url("http", "/test/url"), "http://test.com:9092/any/test/url")
+            api = API(endpoint.conn_handler(session), )
+            self.assertEqual(api.reverse_url("http", "/test/url"), "http://test.com:9092/test/url")
             await session.close()
         self.loop.run_until_complete(go())
 
@@ -34,8 +34,8 @@ class TestBmaApi(unittest.TestCase):
             endpoint = BMAEndpoint("", "124.2.2.1", "", 9092)
             session = aiohttp.ClientSession()
 
-            api = API(endpoint.conn_handler(session), "any")
-            self.assertEqual(api.reverse_url("http", "/test/url"), "http://124.2.2.1:9092/any/test/url")
+            api = API(endpoint.conn_handler(session), )
+            self.assertEqual(api.reverse_url("http", "/test/url"), "http://124.2.2.1:9092/test/url")
             await session.close()
         self.loop.run_until_complete(go())
 
@@ -43,9 +43,9 @@ class TestBmaApi(unittest.TestCase):
         async def go():
             endpoint = BMAEndpoint("", "", "2001:0db8:0000:85a3:0000:0000:ac1f:8001", 9092)
             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://[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()
         self.loop.run_until_complete(go())
 
-- 
GitLab