def render(self, request): """ :type request: bouser.web.request.BouserRequest :param request: :return: """ self.web.crossdomain(request, allow_credentials=True) main, sub = request.get_content_type() if not main: main, sub = 'application', 'json' content = request.content.getvalue() if sub in ('msgpack', 'application/x-msgpack'): data = load(content) elif sub == 'json': data = json.loads(content, 'utf-8') else: request.setResponseCode(400, 'Unknown Content Type') return '' message = Message.from_json(data) self.simargl.message_received(self, message) request.setHeader('content-type', 'application/json; charset=utf-8') return as_json({'success': True})
def _eb(failure): if not isinstance(failure.value, CancelledError): request.setResponseCode(500, 'Scanning error') request.setHeader('Content-Type', 'text/json;charset=utf-8') request.write(as_json(failure.value)) if not request._disconnected: request.finish()
def sendData(self, message): if self.mode == 'json': data = as_json(message) elif self.mode == 'msgpack': data = msgpack_helpers.dump(message.__json__()) else: self.q.append(message) return self.transport.write(header.pack(len(data))) self.transport.write(data)
def make_event(data, event=None, id_=None, is_json=1): result = [] if id_: result.append(b'id: %s' % id_) if event: result.append(b'event: %s' % event) if is_json: result.extend(map(lambda x: b'data: %s' % x, as_json(data).splitlines())) result.append(b'\n') result = b'\n'.join(result) if isinstance(result, unicode): result = result.encode('utf-8') return result
def get_json(url, *args, **kwargs): """ :param json: JSON data :param url: :param args: :param kwargs: :return: """ j = kwargs.pop('json', None) if j: kwargs['postdata'] = as_json(j) kwargs.setdefault('agent', 'Twisted JSON Adapter') uri = URI.fromBytes(url) factory = HTTPClientFactory(url, *args, **kwargs) factory.noisy = 0 if uri.scheme == b'https': from twisted.internet import ssl contextFactory = ssl.ClientContextFactory() reactor.connectSSL(nativeString(uri.host), uri.port, factory, contextFactory) else: reactor.connectTCP(nativeString(uri.host), uri.port, factory) return factory.deferred.addCallback(_json_loads)
def get_json(url, *args, **kwargs): """ :param json: JSON data :param url: :param args: :param kwargs: :return: """ j = kwargs.pop('json', None) if j: kwargs['postdata'] = as_json(j) kwargs.setdefault('agent', 'Twisted JSON Adapter') uri = URI.fromBytes(url) factory = HTTPClientFactory(url, *args, **kwargs) factory.noisy = 0 if uri.scheme == b'https': from twisted.internet import ssl contextFactory = ssl.ClientContextFactory() reactor.connectSSL( nativeString(uri.host), uri.port, factory, contextFactory) else: reactor.connectTCP(nativeString(uri.host), uri.port, factory) return factory.deferred.addCallback(_json_loads)
def send(self, message): if message.topic != 'heartbeat': log.msg(as_json(message), system='SimarglPrintClient')
def sendEvent(self, event, data): json_data = as_json({ 'event': event, 'data': data, }) self.sendMessage(json_data)
def sendObject(self, o): self.sendMessage(as_json(o))
def __init__(self, body, jsonify=True): from bouser.utils import as_json self.body = as_json(body) if jsonify else body self.length = len(self.body)