duniterpy.api package

Submodules

duniterpy.api.client module

class duniterpy.api.client.API(connection_handler: duniterpy.api.endpoint.ConnectionHandler, headers: Union[dict, NoneType] = None)

Bases: object

API is a class used as an abstraction layer over the request library (AIOHTTP).

connect_ws(path: str) → aiohttp.client._WSRequestContextManager

Connect to a websocket in order to use API parameters

In reality, aiohttp.session.ws_connect returns a aiohttp.client._WSRequestContextManager instance. It must be used in a with statement to get the ClientWebSocketResponse instance from it (__aenter__). At the end of the with statement, aiohttp.client._WSRequestContextManager.__aexit__ is called and close the ClientWebSocketResponse in it.

Parameters:path – the url path
Returns:
requests_get(path: str, **kwargs) → aiohttp.client_reqrep.ClientResponse

Requests GET wrapper in order to use API parameters.

Parameters:path – the request path
Returns:
requests_post(path: str, **kwargs) → aiohttp.client_reqrep.ClientResponse

Requests POST wrapper in order to use API parameters.

Parameters:path – the request path
Returns:
reverse_url(scheme: str, path: str) → str

Reverses the url using scheme and path given in parameter.

Parameters:
  • scheme – Scheme of the url
  • path – Path of the url
Returns:

class duniterpy.api.client.Client(_endpoint: Union[str, duniterpy.api.endpoint.Endpoint], session: aiohttp.client.ClientSession = None, proxy: str = None)

Bases: object

Main class to create an API client

close()

Close aiohttp session

Returns:
connect_ws(path: str) → aiohttp.client._WSRequestContextManager

Connect to a websocket in order to use API parameters

Parameters:path – the url path
Returns:
get(url_path: str, params: dict = None, rtype: str = 'json', schema: dict = None) → Any

GET request on self.endpoint + url_path

Parameters:
  • url_path – Url encoded path following the endpoint
  • params – Url query string parameters dictionary
  • rtype – Response type
  • schema – Json Schema to validate response (optional, default None)
Returns:

post(url_path: str, params: dict = None, rtype: str = 'json', schema: dict = None) → Any

POST request on self.endpoint + url_path

Parameters:
  • url_path – Url encoded path following the endpoint
  • params – Url query string parameters dictionary
  • rtype – Response type
  • schema – Json Schema to validate response (optional, default None)
Returns:

duniterpy.api.client.parse_error(text: str) → Any

Validate and parse the BMA answer from websocket

Parameters:text – the bma error
Returns:the json data
duniterpy.api.client.parse_response(response: aiohttp.client_reqrep.ClientResponse, schema: dict) → Any

Validate and parse the BMA answer

Parameters:
  • response – Response of aiohttp request
  • schema – The expected response structure
Returns:

the json data

duniterpy.api.client.parse_text(text: str, schema: dict) → Any

Validate and parse the BMA answer from websocket

Parameters:
  • text – the bma answer
  • schema – dict for jsonschema
Returns:

the json data

duniterpy.api.endpoint module

class duniterpy.api.endpoint.BMAEndpoint(server: str, ipv4: str, ipv6: str, port: int)

Bases: duniterpy.api.endpoint.Endpoint

API = 'BASIC_MERKLED_API'
conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler

Return connection handler instance for the endpoint

Parameters:
  • session – AIOHTTP client session instance
  • proxy – Proxy url
Returns:

classmethod from_inline(inline: str) → BMAEndpointType

Return BMAEndpoint instance from endpoint string

Parameters:inline – Endpoint string
Returns:
inline() → str

Return endpoint string

Returns:
re_inline = re.compile('^BASIC_MERKLED_API(?: ([a-z0-9-_.]*(?:.[a-zA-Z])?))?(?: ((?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])))?(?: ((?:(?:[0-9A-Fa-f]{1,4}:))
class duniterpy.api.endpoint.ConnectionHandler(http_scheme: str, ws_scheme: str, server: str, port: int, path: str, session: aiohttp.client.ClientSession, proxy: Union[str, NoneType] = None)

Bases: object

Helper class used by other API classes to ease passing server connection information.

class duniterpy.api.endpoint.ESCoreEndpoint(server: str, port: int)

Bases: duniterpy.api.endpoint.Endpoint

API = 'ES_CORE_API'
conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler

Return connection handler instance for the endpoint

Parameters:
  • session – AIOHTTP client session instance
  • proxy – Proxy url
Returns:

classmethod from_inline(inline: str) → ESCoreEndpointType

Return ESCoreEndpoint instance from endpoint string

Parameters:inline – Endpoint string
Returns:
inline() → str

Return endpoint string

Returns:
re_inline = re.compile('^ES_CORE_API ((?:[a-z0-9-_.]*(?:.[a-zA-Z])?)|(?:(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))) ([0-9]+)$')
class duniterpy.api.endpoint.ESSubscribtionEndpoint(server: str, port: int)

Bases: duniterpy.api.endpoint.Endpoint

API = 'ES_SUBSCRIPTION_API'
conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler

Return connection handler instance for the endpoint

Parameters:
  • session – AIOHTTP client session instance
  • proxy – Proxy url
Returns:

classmethod from_inline(inline: str) → ESSubscribtionEndpointType

Return ESSubscribtionEndpoint instance from endpoint string

Parameters:inline – Endpoint string
Returns:
inline() → str

Return endpoint string

Returns:
re_inline = re.compile('^ES_SUBSCRIPTION_API ((?:[a-z0-9-_.]*(?:.[a-zA-Z])?)|(?:(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))) ([0-9]+)$')
class duniterpy.api.endpoint.ESUserEndpoint(server: str, port: int)

Bases: duniterpy.api.endpoint.Endpoint

API = 'ES_USER_API'
conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler

Return connection handler instance for the endpoint

Parameters:
  • session – AIOHTTP client session instance
  • proxy – Proxy url
Returns:

classmethod from_inline(inline: str) → ESUserEndpointType

Return ESUserEndpoint instance from endpoint string

Parameters:inline – Endpoint string
Returns:
inline() → str

Return endpoint string

Returns:
re_inline = re.compile('^ES_USER_API ((?:[a-z0-9-_.]*(?:.[a-zA-Z])?)|(?:(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))) ([0-9]+)$')
class duniterpy.api.endpoint.Endpoint

Bases: object

conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler
classmethod from_inline(inline: str) → EndpointType
inline() → str
class duniterpy.api.endpoint.SecuredBMAEndpoint(server: str, ipv4: str, ipv6: str, port: int, path: str)

Bases: duniterpy.api.endpoint.BMAEndpoint

API = 'BMAS'
conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler

Return connection handler instance for the endpoint

Parameters:
  • session – AIOHTTP client session instance
  • proxy – Proxy url
Returns:

classmethod from_inline(inline: str) → SecuredBMAEndpointType

Return SecuredBMAEndpoint instance from endpoint string

Parameters:inline – Endpoint string
Returns:
inline() → str

Return endpoint string

Returns:
re_inline = re.compile('^BMAS(?: ([a-z0-9-_.]*(?:.[a-zA-Z])?))?(?: ((?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])))?(?: ((?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-F)
class duniterpy.api.endpoint.UnknownEndpoint(api: str, properties: list)

Bases: duniterpy.api.endpoint.Endpoint

API = None
conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler

Return connection handler from session

Parameters:
  • session – AIOHTTP Session
  • proxy – Proxy server
Returns:

classmethod from_inline(inline: str) → UnknownEndpointType

Return UnknownEndpoint instance from endpoint string

Parameters:inline – Endpoint string
Returns:
inline() → str

Return endpoint string

Returns:
class duniterpy.api.endpoint.WS2PEndpoint(ws2pid: str, server: str, port: int, path: str)

Bases: duniterpy.api.endpoint.Endpoint

API = 'WS2P'
conn_handler(session: aiohttp.client.ClientSession, proxy: str = None) → duniterpy.api.endpoint.ConnectionHandler

Return connection handler instance for the endpoint

Parameters:
  • session – AIOHTTP client session instance
  • proxy – Proxy url
Returns:

classmethod from_inline(inline: str) → WS2PEndpointType

Return WS2PEndpoint instance from endpoint string

Parameters:inline – Endpoint string
Returns:
inline() → str

Return endpoint string

Returns:
re_inline = re.compile('^WS2P ([0-9a-f]{8}) ((?:[a-z0-9-_.]*(?:.[a-zA-Z])?)|(?:(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))) ([0-9]+)?(?: ([/\\w \\.-]*/?))?)
duniterpy.api.endpoint.endpoint(value: Any) → Any

Convert a endpoint string to the corresponding Endpoint instance type

Parameters:value – Endpoint string or subclass
Returns:

duniterpy.api.errors module

exception duniterpy.api.errors.DuniterError(data: dict)

Bases: Exception

Handle duniter error

Module contents