Exemple #1
0
    def __init__(self, adapter, testnet=False, local_pow=False):
        # type: (AdapterSpec, bool, bool) -> None
        """
        :param adapter:
            URI string or BaseAdapter instance.

        :param testnet:
            Whether to use testnet settings for this instance.
        """
        super(StrictIota, self).__init__()

        if not isinstance(adapter, BaseAdapter):
            adapter = resolve_adapter(adapter)

        self.adapter = adapter  # type: BaseAdapter
        # Note that the `local_pow` parameter is passed to adapter,
        # the api class has no notion about it. The reason being,
        # that this parameter is used in `AttachToTangeCommand` calls,
        # that is called from various api calls (`attach_to_tangle`,
        # `send_trytes` or `send_transfer`). Inside `AttachToTangeCommand`,
        # we no longer have access to the attributes of the API class, therefore
        # `local_pow` needs to be associated with the adapter.
        # Logically, `local_pow` will decide if the api call does pow
        # via pyota-pow extension, or sends the request to a node.
        # But technically, the parameter belongs to the adapter.
        self.adapter.set_local_pow(local_pow)
        self.testnet = testnet
Exemple #2
0
  def execute(self, api, **arguments):
    # type: (Iota, ...) -> int
    debug_requests  = arguments['debug_requests']
    pow_uri         = arguments['pow_uri']

    # If ``pow_uri`` is specified, route POW requests to a separate
    # node.
    if pow_uri:
      pow_adapter = resolve_adapter(pow_uri)

      api.adapter =\
        RoutingWrapper(api.adapter)\
          .add_route('attachToTangle', pow_adapter)\
          .add_route('interruptAttachingToTangle', pow_adapter)

    # If ``debug_requests`` is specified, log HTTP requests/responses.
    if debug_requests:
      # Inject a logger into the IOTA HTTP adapter.
      basicConfig(level=DEBUG, stream=stderr)

      logger = getLogger(__name__)
      logger.setLevel(DEBUG)

      api.adapter.set_logger(logger)

      # Turn on debugging for the underlying HTTP library.
      http_client.HTTPConnection.debuglevel = 1

    try:
      self._start_repl(api)
    except KeyboardInterrupt:
      pass

    return 0
Exemple #3
0
    def execute(self, api, **arguments):
        # type: (Iota, ...) -> int
        debug_requests = arguments['debug_requests']
        pow_uri = arguments['pow_uri']

        # If ``pow_uri`` is specified, route POW requests to a separate
        # node.
        if pow_uri:
            pow_adapter = resolve_adapter(pow_uri)

            api.adapter =\
              RoutingWrapper(api.adapter)\
                .add_route('attachToTangle', pow_adapter)\
                .add_route('interruptAttachingToTangle', pow_adapter)

        # If ``debug_requests`` is specified, log HTTP requests/responses.
        if debug_requests:
            # Inject a logger into the IOTA HTTP adapter.
            basicConfig(level=DEBUG, stream=stderr)

            logger = getLogger(__name__)
            logger.setLevel(DEBUG)

            api.adapter.set_logger(logger)

            # Turn on debugging for the underlying HTTP library.
            http_client.HTTPConnection.debuglevel = 1

        try:
            self._start_repl(api)
        except KeyboardInterrupt:
            pass

        return 0
Exemple #4
0
    def add_route(self, command, adapter):
        # type: (Text, AdapterSpec) -> RoutingWrapper
        """
        Adds a route to the wrapper.

        :param Text command:
            The name of the command. Note that this is the camelCase version of
            the command name (e.g., ``attachToTangle``, not ``attach_to_tangle``).

        :param AdapterSpec adapter:
            The adapter object or URI to route requests to.

        :return:
            The :py:class:`RoutingWrapper` object it was called on. Useful for
            chaining the operation of adding routes in code.

        See :py:class:`RoutingWrapper` for example usage.
        """
        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
Exemple #5
0
    def execute(self, api: Iota, **arguments: Any) -> int:
        debug_requests = arguments['debug_requests']
        pow_uri = arguments['pow_uri']

        # If ``pow_uri`` is specified, route POW requests to a separate
        # node.
        if pow_uri:
            pow_adapter = resolve_adapter(pow_uri)

            api.adapter = RoutingWrapper(api.adapter)
            api.adapter.add_route('attachToTangle', pow_adapter)
            api.adapter.add_route('interruptAttachingToTangle', pow_adapter)

        # If ``debug_requests`` is specified, log HTTP requests/responses.
        if debug_requests:
            # Inject a logger into the IOTA HTTP adapter.
            # This will turn on logging for underlying httpx client as well
            basicConfig(level=DEBUG, stream=stderr)

            logger = getLogger(__name__)
            logger.setLevel(DEBUG)

            api.adapter.set_logger(logger)

        try:
            self._start_repl(api)
        except KeyboardInterrupt:
            pass

        return 0
Exemple #6
0
  def __init__(self, adapter):
    # type: (AdapterSpec) -> None
    super(BaseWrapper, self).__init__()

    if not isinstance(adapter, BaseAdapter):
      adapter = resolve_adapter(adapter)

    self.adapter = adapter # type: BaseAdapter
Exemple #7
0
    def __init__(self, adapter):
        # type: (AdapterSpec) -> None
        super(BaseWrapper, self).__init__()

        if not isinstance(adapter, BaseAdapter):
            adapter = resolve_adapter(adapter)

        self.adapter = adapter  # type: BaseAdapter
Exemple #8
0
def main(uri, testnet, pow_uri, debug_requests):
    # type: (Text, bool, Text, bool) -> None
    seed = secure_input(
        'Enter seed and press return (typing will not be shown).\n'
        'If no seed is specified, a random one will be used instead.\n')

    if isinstance(seed, text_type):
        seed = seed.encode('ascii')

    adapter_ = resolve_adapter(uri)

    # If ``pow_uri`` is specified, route POW requests to a separate node.
    if pow_uri:
        pow_adapter = resolve_adapter(pow_uri)

        adapter_ =\
          RoutingWrapper(adapter_)\
            .add_route('attachToTangle', pow_adapter)\
            .add_route('interruptAttachingToTangle', pow_adapter)

    # If ``debug_requests`` is specified, log HTTP requests/responses.
    if debug_requests:
        basicConfig(level=DEBUG, stream=stderr)

        logger = getLogger(__name__)
        logger.setLevel(DEBUG)

        adapter_.set_logger(logger)

    iota = Iota(adapter_, seed=seed, testnet=testnet)

    # To speed up certain operations, install an address cache.
    AddressGenerator.cache = MemoryAddressCache()

    _banner = (
        'IOTA API client for {uri} ({testnet}) initialized as variable `iota`.\n'
        'Type `help(iota)` for help.'.format(
            testnet='testnet' if testnet else 'mainnet',
            uri=uri,
        ))

    start_shell(iota, _banner)
Exemple #9
0
  def __init__(self, adapter, testnet=False):
    # type: (AdapterSpec, bool) -> None
    """
    :param adapter:
      URI string or BaseAdapter instance.

    :param testnet:
      Whether to use testnet settings for this instance.
    """
    super(StrictIota, self).__init__()

    if not isinstance(adapter, BaseAdapter):
      adapter = resolve_adapter(adapter)

    self.adapter  = adapter # type: BaseAdapter
    self.testnet = testnet
Exemple #10
0
    def __init__(self, adapter, testnet=False):
        # type: (AdapterSpec, bool) -> None
        """
    :param adapter:
      URI string or BaseAdapter instance.

    :param testnet:
      Whether to use testnet settings for this instance.
    """
        super(StrictIota, self).__init__()

        if not isinstance(adapter, BaseAdapter):
            adapter = resolve_adapter(adapter)

        self.adapter = adapter  # type: BaseAdapter
        self.testnet = testnet
Exemple #11
0
  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
Exemple #12
0
    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
Exemple #13
0
 def test_adapter_instance(self):
   """
   Resolving an adapter instance.
   """
   adapter = MockAdapter()
   self.assertIs(resolve_adapter(adapter), adapter)
Exemple #14
0
 def test_unknown_protocol(self):
     """
 The URI references a protocol that has no associated adapter.
 """
     with self.assertRaises(InvalidUri):
         resolve_adapter('foobar://localhost:14265')
Exemple #15
0
 def test_missing_protocol(self):
     """
 The URI does not include a protocol.
 """
     with self.assertRaises(InvalidUri):
         resolve_adapter('localhost:14265')
Exemple #16
0
 def test_https(self):
     """
 Resolving a valid ``https://`` URI.
 """
     adapter = resolve_adapter('https://localhost:14265/')
     self.assertIsInstance(adapter, HttpAdapter)
Exemple #17
0
 def test_adapter_instance(self):
     """
 Resolving an adapter instance.
 """
     adapter = MockAdapter()
     self.assertIs(resolve_adapter(adapter), adapter)
Exemple #18
0
 def test_missing_protocol(self):
   """
   The URI does not include a protocol.
   """
   with self.assertRaises(InvalidUri):
     resolve_adapter('localhost:14265')
Exemple #19
0
 def test_unknown_protocol(self):
   """
   The URI references a protocol that has no associated adapter.
   """
   with self.assertRaises(InvalidUri):
     resolve_adapter('foobar://localhost:14265')
Exemple #20
0
 def _update_milestone(self, event: Optional[wx.TimerEvent] = None) -> None:
     self.milestone_worker.send(
         GetNodeInfoCommand(resolve_adapter(DEFAULT_URI)),
         callback=self._handle_update_milestone_response,
     )
Exemple #21
0
 def test_https(self):
   """
   Resolving a valid ``https://`` URI.
   """
   adapter = resolve_adapter('https://localhost:14265/')
   self.assertIsInstance(adapter, HttpAdapter)