def render_POST(self, request): """ Renders a POST request. This calls :meth:`update_value`. """ if "value" not in request.json_content: return response.error("Invalid Request", "You sent an invalid request.") if self.object.config is None: return ERR_CONFIG_NREADY value = request.json_content["value"] original_value = self.object.value defered = self.update_value(value) def on_error(error): """ called on save error. """ self.object.config.config[self.object.name] = original_value request.write(json.dumps(response.error_tb(error)).encode("utf-8")) request.finish() def on_success(*args): """ Called on success. """ print(args) request.write(self.render_GET(request)) request.finish() defered.addCallback(on_success) defered.addErrback(on_error) return server.NOT_DONE_YET
def render_GET(self, request): ''' Renders a json list of all children. ''' if self._config.state is None: return response.error('Not ready', 'State unknown.') result = {} items = [] try: for item in ITorResource(self).get_list(): items.append(self.json_list_class(item)) except RuntimeError as why: result = response.error_exception(why) else: result['objects'] = items return result
def getChild(self, path, request): ''' If the path is 'dns' is returns either dns api resource or an json error if the api is not available because there is no DNSPort. Otherwise :class:`resource.NoResource` object is returned. ''' if path == 'dns': port = self.dns_port if port is None: return response.error("No DNS", "Tor instance has not DNSPort.") else: self.dns.config.resolver = client.Resolver( servers=[(self.host, port)]) return self.dns return resource.NoResource()
from __future__ import absolute_import, print_function, with_statement import json import txtorcon from zope.interface import implements from twisted.web import server from torweb.api.json import configuration from torweb.api.util import request, response from .base import ITorResource, TorResource, TorResourceDetail __all__ = ("ConfigurationRoot", "Configuration", "ConfigurationEntry") ERR_CONFIG_NREADY = response.error("Not ready", "Configuration was not set.") class ConfigurationEntry(object): """ Wrapper for configuration values. :param config: An instance of :class:`txtorcon.TorConfig` :param name: The configuration name. """ #: The :class:`txtorcon.Toconfig` configuration object config = None #: The name of the configuration entry name = None