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

issue #56 WIP - Client(_endpoint) accept Endpoint instances

parent 0049a93c
No related branches found
No related tags found
No related merge requests found
# 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
"""
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:
......
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
......@@ -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]))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment