Here are the examples of the python api crossbar.twisted.resource.JsonResource taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
0
Source : router.py
with MIT License
from fbla-competitive-events
with MIT License
from fbla-competitive-events
def _create_resource(self, path_config, nested=True):
"""
Creates child resource to be added to the parent.
:param path_config: Configuration for the new child resource.
:type path_config: dict
:returns: Resource -- the new child resource
"""
# WAMP-WebSocket resource
#
if path_config['type'] == 'websocket':
ws_factory = WampWebSocketServerFactory(self._router_session_factory, self.config.extra.cbdir, path_config, self._templates)
# FIXME: Site.start/stopFactory should start/stop factories wrapped as Resources
ws_factory.startFactory()
return WebSocketResource(ws_factory)
# Static file hierarchy resource
#
elif path_config['type'] == 'static':
static_options = path_config.get('options', {})
if 'directory' in path_config:
static_dir = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))
elif 'package' in path_config:
if 'resource' not in path_config:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing resource")
try:
mod = importlib.import_module(path_config['package'])
except ImportError as e:
emsg = "Could not import resource {} from package {}: {}".format(path_config['resource'], path_config['package'], e)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
else:
try:
static_dir = os.path.abspath(pkg_resources.resource_filename(path_config['package'], path_config['resource']))
except Exception as e:
emsg = "Could not import resource {} from package {}: {}".format(path_config['resource'], path_config['package'], e)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
else:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing web spec")
static_dir = static_dir.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
# create resource for file system hierarchy
#
if static_options.get('enable_directory_listing', False):
static_resource_class = StaticResource
else:
static_resource_class = StaticResourceNoListing
cache_timeout = static_options.get('cache_timeout', DEFAULT_CACHE_TIMEOUT)
static_resource = static_resource_class(static_dir, cache_timeout=cache_timeout)
# set extra MIME types
#
static_resource.contentTypes.update(EXTRA_MIME_TYPES)
if 'mime_types' in static_options:
static_resource.contentTypes.update(static_options['mime_types'])
patchFileContentTypes(static_resource)
# render 404 page on any concrete path not found
#
static_resource.childNotFound = Resource404(self._templates, static_dir)
return static_resource
# WSGI resource
#
elif path_config['type'] == 'wsgi':
if not _HAS_WSGI:
raise ApplicationError(u"crossbar.error.invalid_configuration", "WSGI unsupported")
if 'module' not in path_config:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing WSGI app module")
if 'object' not in path_config:
raise ApplicationError(u"crossbar.error.invalid_configuration", "missing WSGI app object")
# import WSGI app module and object
mod_name = path_config['module']
try:
mod = importlib.import_module(mod_name)
except ImportError as e:
raise ApplicationError(u"crossbar.error.invalid_configuration", "WSGI app module '{}' import failed: {} - Python search path was {}".format(mod_name, e, sys.path))
else:
obj_name = path_config['object']
if obj_name not in mod.__dict__:
raise ApplicationError(u"crossbar.error.invalid_configuration", "WSGI app object '{}' not in module '{}'".format(obj_name, mod_name))
else:
app = getattr(mod, obj_name)
# Create a threadpool for running the WSGI requests in
pool = ThreadPool(maxthreads=path_config.get("maxthreads", 20),
minthreads=path_config.get("minthreads", 0),
name="crossbar_wsgi_threadpool")
self._reactor.addSystemEventTrigger('before', 'shutdown', pool.stop)
pool.start()
# Create a Twisted Web WSGI resource from the user's WSGI application object
try:
wsgi_resource = WSGIResource(self._reactor, pool, app)
if not nested:
wsgi_resource = WSGIRootResource(wsgi_resource, {})
except Exception as e:
raise ApplicationError(u"crossbar.error.invalid_configuration", "could not instantiate WSGI resource: {}".format(e))
else:
return wsgi_resource
# Redirecting resource
#
elif path_config['type'] == 'redirect':
redirect_url = path_config['url'].encode('ascii', 'ignore')
return RedirectResource(redirect_url)
# Reverse proxy resource
#
elif path_config['type'] == 'reverseproxy':
host = path_config['host']
port = int(path_config.get('port', 80))
path = path_config.get('path', '').encode('ascii', 'ignore')
return ReverseProxyResource(host, port, path)
# JSON value resource
#
elif path_config['type'] == 'json':
value = path_config['value']
return JsonResource(value)
# CGI script resource
#
elif path_config['type'] == 'cgi':
cgi_processor = path_config['processor']
cgi_directory = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))
cgi_directory = cgi_directory.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
return CgiDirectory(cgi_directory, cgi_processor, Resource404(self._templates, cgi_directory))
# WAMP-Longpoll transport resource
#
elif path_config['type'] == 'longpoll':
path_options = path_config.get('options', {})
lp_resource = WampLongPollResource(self._router_session_factory,
timeout=path_options.get('request_timeout', 10),
killAfter=path_options.get('session_timeout', 30),
queueLimitBytes=path_options.get('queue_limit_bytes', 128 * 1024),
queueLimitMessages=path_options.get('queue_limit_messages', 100),
debug_transport_id=path_options.get('debug_transport_id', None)
)
lp_resource._templates = self._templates
return lp_resource
# Publisher resource (part of REST-bridge)
#
elif path_config['type'] == 'publisher':
# create a vanilla session: the publisher will use this to inject events
#
publisher_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
publisher_session = ApplicationSession(publisher_session_config)
# add the publisher session to the router
#
self._router_session_factory.add(publisher_session, authrole=path_config.get('role', 'anonymous'))
# now create the publisher Twisted Web resource
#
return PublisherResource(path_config.get('options', {}), publisher_session)
# Webhook resource (part of REST-bridge)
#
elif path_config['type'] == 'webhook':
# create a vanilla session: the webhook will use this to inject events
#
webhook_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
webhook_session = ApplicationSession(webhook_session_config)
# add the webhook session to the router
#
self._router_session_factory.add(webhook_session, authrole=path_config.get('role', 'anonymous'))
# now create the webhook Twisted Web resource
#
return WebhookResource(path_config.get('options', {}), webhook_session)
# Caller resource (part of REST-bridge)
#
elif path_config['type'] == 'caller':
# create a vanilla session: the caller will use this to inject calls
#
caller_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
caller_session = ApplicationSession(caller_session_config)
# add the calling session to the router
#
self._router_session_factory.add(caller_session, authrole=path_config.get('role', 'anonymous'))
# now create the caller Twisted Web resource
#
return CallerResource(path_config.get('options', {}), caller_session)
# File Upload resource
#
elif path_config['type'] == 'upload':
upload_directory = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['directory']))
upload_directory = upload_directory.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
if not os.path.isdir(upload_directory):
emsg = "configured upload directory '{}' in file upload resource isn't a directory".format(upload_directory)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
if 'temp_directory' in path_config:
temp_directory = os.path.abspath(os.path.join(self.config.extra.cbdir, path_config['temp_directory']))
temp_directory = temp_directory.encode('ascii', 'ignore') # http://stackoverflow.com/a/20433918/884770
else:
temp_directory = os.path.abspath(tempfile.gettempdir())
temp_directory = os.path.join(temp_directory, 'crossbar-uploads')
if not os.path.exists(temp_directory):
os.makedirs(temp_directory)
if not os.path.isdir(temp_directory):
emsg = "configured temp directory '{}' in file upload resource isn't a directory".format(temp_directory)
self.log.error(emsg)
raise ApplicationError(u"crossbar.error.invalid_configuration", emsg)
# file upload progress and finish events are published via this session
#
upload_session_config = ComponentConfig(realm=path_config['realm'], extra=None)
upload_session = ApplicationSession(upload_session_config)
self._router_session_factory.add(upload_session, authrole=path_config.get('role', 'anonymous'))
self.log.info("File upload resource started. Uploads to {upl} using temp folder {tmp}.", upl=upload_directory, tmp=temp_directory)
return FileUploadResource(upload_directory, temp_directory, path_config['form_fields'], upload_session, path_config.get('options', {}))
# Generic Twisted Web resource
#
elif path_config['type'] == 'resource':
try:
klassname = path_config['classname']
self.log.debug("Starting class '{}'".format(klassname))
c = klassname.split('.')
module_name, klass_name = '.'.join(c[:-1]), c[-1]
module = importlib.import_module(module_name)
make = getattr(module, klass_name)
return make(path_config.get('extra', {}))
except Exception as e:
emsg = "Failed to import class '{}' - {}".format(klassname, e)
self.log.error(emsg)
self.log.error("PYTHONPATH: {pythonpath}", pythonpath=sys.path)
raise ApplicationError(u"crossbar.error.class_import_failed", emsg)
# Schema Docs resource
#
elif path_config['type'] == 'schemadoc':
realm = path_config['realm']
if realm not in self.realm_to_id:
raise ApplicationError(u"crossbar.error.no_such_object", "No realm with URI '{}' configured".format(realm))
realm_id = self.realm_to_id[realm]
realm_schemas = self.realms[realm_id].session._schemas
return SchemaDocResource(self._templates, realm, realm_schemas)
# Nested subpath resource
#
elif path_config['type'] == 'path':
nested_paths = path_config.get('paths', {})
if '/' in nested_paths:
nested_resource = self._create_resource(nested_paths['/'])
else:
nested_resource = Resource404(self._templates, b'')
# nest subpaths under the current entry
#
self._add_paths(nested_resource, nested_paths)
return nested_resource
else:
raise ApplicationError(u"crossbar.error.invalid_configuration",
"invalid Web path type '{}' in {} config".format(path_config['type'],
'nested' if nested else 'root'))
def stop_router_transport(self, id, details=None):