This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f601596
commit 4ad7219
Showing
1 changed file
with
85 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,112 @@ | ||
# coding=utf-8 | ||
from __future__ import absolute_import, division, print_function, \ | ||
unicode_literals | ||
unicode_literals | ||
|
||
from abc import ABCMeta, abstractmethod as abstract_method | ||
from typing import Dict, Text | ||
|
||
from iota.adapter import AdapterSpec, BaseAdapter, resolve_adapter | ||
from six import with_metaclass | ||
|
||
from iota.adapter import AdapterSpec, BaseAdapter, resolve_adapter | ||
|
||
__all__ = [ | ||
'RoutingWrapper', | ||
'RoutingWrapper', | ||
] | ||
|
||
|
||
class BaseWrapper(with_metaclass(ABCMeta, BaseAdapter)): | ||
""" | ||
Base functionality for "adapter wrappers", used to extend the | ||
functionality of IOTA adapters. | ||
""" | ||
def __init__(self, adapter): | ||
# type: (AdapterSpec) -> None | ||
super(BaseWrapper, self).__init__() | ||
""" | ||
Base functionality for "adapter wrappers", used to extend the | ||
functionality of IOTA adapters. | ||
""" | ||
|
||
if not isinstance(adapter, BaseAdapter): | ||
adapter = resolve_adapter(adapter) | ||
def __init__(self, adapter): | ||
# type: (AdapterSpec) -> None | ||
super(BaseWrapper, self).__init__() | ||
|
||
self.adapter = adapter # type: BaseAdapter | ||
if not isinstance(adapter, BaseAdapter): | ||
adapter = resolve_adapter(adapter) | ||
|
||
def get_uri(self): | ||
# type: () -> Text | ||
return self.adapter.get_uri() | ||
self.adapter = adapter # type: BaseAdapter | ||
|
||
@abstract_method | ||
def send_request(self, payload, **kwargs): | ||
# type: (dict, dict) -> dict | ||
raise NotImplementedError( | ||
'Not implemented in {cls}.'.format(cls=type(self).__name__), | ||
) | ||
def get_uri(self): | ||
# type: () -> Text | ||
return self.adapter.get_uri() | ||
|
||
@abstract_method | ||
def send_request(self, payload, **kwargs): | ||
# type: (dict, dict) -> dict | ||
raise NotImplementedError( | ||
'Not implemented in {cls}.'.format(cls=type(self).__name__), | ||
) | ||
|
||
class RoutingWrapper(BaseWrapper): | ||
""" | ||
Routes commands to different nodes. | ||
This allows you to, for example, send POW requests to a local node, | ||
while routing all other requests to a remote one. | ||
Example:: | ||
# Route POW to localhost, everything else to 12.34.56.78. | ||
iota = Iota( | ||
RoutingWrapper('http://12.34.56.78:14265') | ||
.add_route('attachToTangle', 'http://localhost:14265') | ||
.add_route('interruptAttachingToTangle', 'http://localhost:14265') | ||
), | ||
) | ||
""" | ||
def __init__(self, default_adapter): | ||
# type: (AdapterSpec) -> None | ||
""" | ||
:param default_adapter: | ||
Adapter to use for any routes not listed in ``routes``. | ||
""" | ||
super(RoutingWrapper, self).__init__(default_adapter) | ||
|
||
# Try to limit the number of distinct adapter instances we create | ||
# when resolving URIs. | ||
self.adapter_aliases = {} # type: Dict[AdapterSpec, BaseAdapter] | ||
|
||
self.routes = {} # type: Dict[Text, BaseAdapter] | ||
|
||
def add_route(self, command, adapter): | ||
# type: (Text, AdapterSpec) -> RoutingWrapper | ||
class RoutingWrapper(BaseWrapper): | ||
""" | ||
Adds a route to the wrapper. | ||
Routes commands to different nodes. | ||
:param command: | ||
The name of the command to route (e.g., "attachToTangle"). | ||
This allows you to, for example, send POW requests to a local node, | ||
while routing all other requests to a remote one. | ||
:param adapter: | ||
The adapter object or URI to route requests to. | ||
""" | ||
if not isinstance(adapter, BaseAdapter): | ||
try: | ||
adapter = self.adapter_aliases[adapter] | ||
except KeyError: | ||
self.adapter_aliases[adapter] = adapter = resolve_adapter(adapter) | ||
Example: | ||
self.routes[command] = adapter | ||
.. code-block:: python | ||
return self | ||
|
||
def get_adapter(self, command): | ||
# type: (Text) -> BaseAdapter | ||
""" | ||
Return the adapter for the specified command. | ||
# Route POW to localhost, everything else to 12.34.56.78. | ||
iota = Iota( | ||
RoutingWrapper('http://12.34.56.78:14265') | ||
.add_route('attachToTangle', 'http://localhost:14265') | ||
.add_route('interruptAttachingToTangle', 'http://localhost:14265') | ||
), | ||
) | ||
""" | ||
return self.routes.get(command, self.adapter) | ||
|
||
def send_request(self, payload, **kwargs): | ||
# type: (dict, dict) -> dict | ||
command = payload.get('command') | ||
|
||
return self.get_adapter(command).send_request(payload, **kwargs) | ||
def __init__(self, default_adapter): | ||
# type: (AdapterSpec) -> None | ||
""" | ||
:param default_adapter: | ||
Adapter to use for any routes not listed in ``routes``. | ||
""" | ||
super(RoutingWrapper, self).__init__(default_adapter) | ||
|
||
# Try to limit the number of distinct adapter instances we create | ||
# when resolving URIs. | ||
self.adapter_aliases = {} # type: Dict[AdapterSpec, BaseAdapter] | ||
|
||
self.routes = {} # type: Dict[Text, BaseAdapter] | ||
|
||
def add_route(self, command, adapter): | ||
# type: (Text, AdapterSpec) -> RoutingWrapper | ||
""" | ||
Adds a route to the wrapper. | ||
:param command: | ||
The name of the command to route (e.g., "attachToTangle"). | ||
:param adapter: | ||
The adapter object or URI to route requests to. | ||
""" | ||
if not isinstance(adapter, BaseAdapter): | ||
try: | ||
adapter = self.adapter_aliases[adapter] | ||
except KeyError: | ||
self.adapter_aliases[adapter] = adapter = resolve_adapter( | ||
adapter | ||
) | ||
|
||
self.routes[command] = adapter | ||
|
||
return self | ||
|
||
def get_adapter(self, command): | ||
# type: (Text) -> BaseAdapter | ||
""" | ||
Return the adapter for the specified command. | ||
""" | ||
return self.routes.get(command, self.adapter) | ||
|
||
def send_request(self, payload, **kwargs): | ||
# type: (dict, dict) -> dict | ||
command = payload.get('command') | ||
|
||
return self.get_adapter(command).send_request(payload, **kwargs) |