diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py index 19c9c4af8940ca9dda5f0d0547ee925afd405901..b7a73adc56596d1b7c0aaefae6805902865d9d70 100644 --- a/duniterpy/api/client.py +++ b/duniterpy/api/client.py @@ -1,13 +1,16 @@ # Authors: # Caner Candan <caner@candan.fr>, http://caner.candan.fr # Inso <insomniak.fr at gmail.com> -from typing import Callable +# vit import json import logging +from typing import Callable, Union + import aiohttp import jsonschema -from .errors import DuniterError + import duniterpy.api.endpoint as endpoint +from .errors import DuniterError logger = logging.getLogger("duniter") @@ -177,7 +180,9 @@ class Client: """ Main class to create an API client """ - def __init__(self, _endpoint: str, session: aiohttp.ClientSession = None, proxy: str = None): + + def __init__(self, _endpoint: Union[str, endpoint.Endpoint], session: aiohttp.ClientSession = None, + proxy: str = None): """ Init Client instance @@ -185,8 +190,11 @@ class Client: :param session: Aiohttp client session (optional, default None) :param proxy: Proxy server as hostname:port """ - # Endpoint Protocol detection - self.endpoint = endpoint.endpoint(_endpoint) + if type(endpoint) is str: + # Endpoint Protocol detection + self.endpoint = endpoint.endpoint(_endpoint) + else: + self.endpoint = _endpoint # if no user session... if session is None: @@ -196,7 +204,7 @@ class Client: self.session = session self.proxy = proxy - async def get(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None)-> any: + async def get(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None) -> any: """ GET request on self.endpoint + url_path @@ -227,7 +235,7 @@ class Client: elif rtype == RESPONSE_JSON: return await response.json() - async def post(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None)-> any: + async def post(self, url_path: str, params: dict = None, rtype: str = RESPONSE_JSON, schema: dict = None) -> any: """ POST request on self.endpoint + url_path diff --git a/duniterpy/api/endpoint.py b/duniterpy/api/endpoint.py index e4c4f7963328cb891494077b3c54e8716e30522e..6d7bec8a98d2ef36f8805e3b7b8ebf8364b47e5e 100644 --- a/duniterpy/api/endpoint.py +++ b/duniterpy/api/endpoint.py @@ -1,5 +1,7 @@ import re + import aiohttp + from .constants import * from ..documents import MalformedDocumentError @@ -7,7 +9,8 @@ from ..documents import MalformedDocumentError class ConnectionHandler: """Helper class used by other API classes to ease passing server connection information.""" - def __init__(self, http_scheme: str, ws_scheme: str, server: str, port: int, path: str = "", proxy: str = None, session: aiohttp.ClientSession = None): + def __init__(self, http_scheme: str, ws_scheme: str, server: str, port: int, path: str = "", proxy: str = None, + session: aiohttp.ClientSession = None): """ Init instance of connection handler @@ -27,7 +30,7 @@ class ConnectionHandler: self.proxy = proxy self.session = session - def __str__(self)-> str: + def __str__(self) -> str: return 'connection info: %s:%d' % (self.server, self.port) @@ -39,6 +42,9 @@ class Endpoint: def inline(self) -> str: raise NotImplementedError("inline() is not implemented") + def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + raise NotImplementedError("conn_handler is not implemented") + def __str__(self) -> str: raise NotImplementedError("__str__ is not implemented") @@ -79,6 +85,9 @@ class UnknownEndpoint(Endpoint): doc += " {0}".format(p) return doc + def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + return ConnectionHandler("", "", "", 0, "") + def __str__(self) -> str: return "{0} {1}".format(self.api, ' '.join(["{0}".format(p) for p in self.properties])) @@ -152,10 +161,10 @@ class BMAEndpoint(Endpoint): :return: """ return BMAEndpoint.API + "{DNS}{IPv4}{IPv6}{PORT}" \ - .format(DNS=(" {0}".format(self.server) if self.server else ""), - IPv4=(" {0}".format(self.ipv4) if self.ipv4 else ""), - IPv6=(" {0}".format(self.ipv6) if self.ipv6 else ""), - PORT=(" {0}".format(self.port) if self.port else "")) + .format(DNS=(" {0}".format(self.server) if self.server else ""), + IPv4=(" {0}".format(self.ipv4) if self.ipv4 else ""), + IPv6=(" {0}".format(self.ipv6) if self.ipv6 else ""), + PORT=(" {0}".format(self.port) if self.port else "")) # fixme: session must be mandatory def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: @@ -179,7 +188,7 @@ class BMAEndpoint(Endpoint): def __eq__(self, other): if isinstance(other, BMAEndpoint): return self.server == other.server and self.ipv4 == other.ipv4 \ - and self.ipv6 == other.ipv6 and self.port == other.port + and self.ipv6 == other.ipv6 and self.port == other.port else: return False @@ -304,7 +313,7 @@ class WS2PEndpoint(Endpoint): def __eq__(self, other): if isinstance(other, WS2PEndpoint): return self.server == other.server and self.ws2pid == other.ws2pid \ - and self.port == other.port and self.path == other.path + and self.port == other.port and self.path == other.path else: return False