def test_rpc(self): '''Send a message to the rpc''' loop = self.http._loop ws = yield from self.http.get(self.ws, websocket_handler=Message(loop)) self.assertEqual(ws.status_code, 101) ws.write('Hello there!') data = yield from ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hello there!') result = yield from self.rpc.message('Hi!') self.assertEqual(result, 'OK') data = yield from ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hi!')
def test_rpc(self): '''Send a message to the rpc''' ws = yield self.http.get(self.ws, websocket_handler=MessageHandler()).on_headers self.assertEqual(ws.handshake.status_code, 101) ws.write('Hello there!') data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hello there!') result = yield self.rpc.message('Hi!') self.assertEqual(result, 'OK') data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hi!')
def test_rpc(self): '''Send a message to the rpc''' ws = yield self.http.get(self.ws, websocket_handler=MessageHandler() ).on_headers self.assertEqual(ws.handshake.status_code, 101) ws.write('Hello there!') data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hello there!') result = yield self.rpc.message('Hi!') self.assertEqual(result, 'OK') data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hi!')
def _data_and_files(self, data=True, files=True, future=None): result = {}, None chunk = None if future is None: stream = self.environ.get('wsgi.input') if self.method not in ENCODE_URL_METHODS and stream: chunk = stream.read() if isinstance(chunk, Future): return chain_future( chunk, partial(self._data_and_files, data, files)) else: chunk = future if chunk is not None: content_type, options = self.content_type_options charset = options.get('charset', 'utf-8') if content_type in JSON_CONTENT_TYPES: result = json.loads(chunk.decode(charset)), None elif content_type: self.environ['wsgi.input'] = BytesIO(chunk) result = parse_form_data(self.environ, charset) else: result = chunk, None self.environ['wsgi.input'] = BytesIO(chunk) self.cache.data_and_files = result return self.data_and_files(data, files)
def _ready(self, data): self.environ['wsgi.input'] = BytesIO(data) try: self.result = (json.loads(data.decode(self.charset)), None) except Exception as exc: raise BadRequest('Could not decode JSON') from exc return self.result
def process_data(self, response, **kw): '''Callback passed to :class:`HttpClient` for processing streaming data. ''' if response.status_code == 200: messages = [] data = response.recv_body() while data: idx = data.find(b'\r\n') if idx < 0: # incomplete data - add to buffer self.buffer.append(data) data = None else: self.buffer.append(data[:idx]) data = data[idx+2:] msg = b''.join(self.buffer) self.buffer = [] if msg: body = json.loads(msg.decode('utf-8')) if 'disconnect' in body: msg = body['disconnect'] self.logger.warning('Disconnecting (%d): %s', msg['code'], msg['reason']) elif 'warning' in body: message = body['warning']['message'] self.logger.warning(message) else: messages.append(body) if messages: # a list of messages is available if self.cfg.callable: self.cfg.callable(self, messages)
def process_data(self, response, **kw): if response.status_code == 200: messages = [] data = response.recv_body() while data: idx = data.find(b'\r\n') if idx < 0: self.buffer.append(data) data = None else: self.buffer.append(data[:idx]) data = data[idx + 2:] msg = b''.join(self.buffer) self.buffer = [] if msg: body = json.loads(msg.decode('utf-8')) if 'disconnect' in body: msg = body['disconnect'] self.logger.warning('Disconnecting (%d): %s', msg['code'], msg['reason']) elif 'warning' in body: message = body['warning']['message'] self.logger.warning(message) else: messages.append(body) if messages: # a list of messages is available if self.cfg.callable: self.cfg.callable(self, messages)
def parse_frame(self, websocket, frame): parser = frame_parser(kind=1) frame = parser.decode(frame) wsclient = websocket.cache.wsclient websocket.connection.reset_mock() msg = _json.loads(frame.body[1:])[0] return wsclient.protocol.decode(msg)
def design_delete(self, name): '''Delete an existing design document at ``name``. ''' response = yield self.request('head', self._database, '_design', name) if response.status_code == 200: rev = json.loads(response.headers['etag']) yield self.request('delete', self._database, '_design', name, rev=rev)
def on_message(self, websocket, msg): request = websocket.request client = request.cache.mailclient if msg and client: msg = json.loads(msg) if 'mailbox' in msg: mailbox = yield client.examine(msg['mailbox']) self.write(request, json.dumps({'mailbox': mailbox}))
def test_websocket(self): ws = yield self.http.get(self.ws, websocket_handler=MessageHandler()) response = ws.handshake self.assertEqual(response.status_code, 101) self.assertEqual(response.headers["upgrade"], "websocket") self.assertEqual(response.connection, ws.connection) self.assertTrue(ws.connection) self.assertIsInstance(ws.handler, MessageHandler) # data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data["message"], "joined") # ws.write("Hello there!") data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data["message"], "Hello there!")
def test_websocket(self): ws = yield self.http.get(self.ws, websocket_handler=MessageHandler() ).on_headers self.assertTrue(ws) self.assertIsInstance(ws.handler, MessageHandler) ws.write('Hello there!') data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hello there!')
def test_websocket(self): c = self.http ws = yield c.get(self.ws, websocket_handler=MessageHandler(c._loop)) response = ws.handshake self.assertEqual(response.status_code, 101) self.assertEqual(response.headers['upgrade'], 'websocket') self.assertEqual(response.connection, ws.connection) self.assertTrue(ws.connection) self.assertIsInstance(ws.handler, MessageHandler) # data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'joined') # ws.write('Hello there!') data = yield ws.handler.get() data = json.loads(data) self.assertEqual(data['message'], 'Hello there!')
def to_python(self, value, store=None): if value is None: return self.get_default() if isinstance(value, str): try: return json.loads(value) except TypeError: return None else: return value
def media_libraries(): global _media_libraries if _media_libraries is None: if os.path.isfile('libs.json'): # pragma nocover with open('libs.json') as f: data = f.read() _media_libraries = json.loads(data) else: from pulsar import new_event_loop from pulsar.apps.http import HttpClient http = HttpClient(loop=new_event_loop()) try: response = http.get(_libs_url) _media_libraries = response.json() except Exception: # pragma nocover http.logger.error('Could not import media library', exc_info=True) _media_libraries = {'libs': {}, 'deps': {}} return _media_libraries
def _data_and_files(self): result = {}, None stream = self.environ.get('wsgi.input') try: if self.method not in ENCODE_URL_METHODS and stream: chunk = yield stream.read() content_type, options = self.content_type_options charset = options.get('charset', 'utf-8') if content_type in JSON_CONTENT_TYPES: result = json.loads(chunk.decode(charset)), None else: self.environ['wsgi.input'] = BytesIO(chunk) result = parse_form_data(self.environ, charset) # set the wsgi.input to a readable file-like object for # third-parties application (django or any other web-framework) self.environ['wsgi.input'] = BytesIO(chunk) finally: self._cached_data_and_files = result coroutine_return(result)
def _data_and_files(self, data=True, files=True): result = {}, None stream = self.environ.get("wsgi.input") chunk = None try: if self.method not in ENCODE_URL_METHODS and stream: chunk = stream.read() if isinstance(chunk, Future): chunk = yield chunk content_type, options = self.content_type_options charset = options.get("charset", "utf-8") if content_type in JSON_CONTENT_TYPES: result = json.loads(chunk.decode(charset)), None else: self.environ["wsgi.input"] = BytesIO(chunk) result = parse_form_data(self.environ, charset) finally: self.cache.data_and_files = result if chunk is not None: self.environ["wsgi.input"] = BytesIO(chunk) coroutine_return(self.data_and_files(data, files))
def json(self, charset=None): '''Decode content as a JSON object.''' return json.loads(self.content_string(charset))
def json(self, charset=None): """Decode content as a JSON object. """ return _json.loads(self.text(charset))
def json(self, charset=None): """Decode content as a JSON object. """ return json.loads(self.text(charset))
def json(self): """Decode content as a JSON object. """ return _json.loads(self.text)
def xhr_send(self, request): data = yield request.body_data() msg = json.loads(data) self.handle.on_message(WebSocketProxy(request), msg) request.response.status_code = 204 return request.response
def _ready(self, data): self.result = (json.loads(data.decode(self.charset)), None) return self.result
def _ready(self, data): self.environ["wsgi.input"] = BytesIO(data) self.result = (json.loads(data.decode(self.charset)), None) return self.result
def __call__(self, channels, message): self.callback(json.loads(message))
def load(cls, data, method=None): method = method or 'json' if method == 'json': return cls(**json.loads(to_string(data))) else: raise ImproperlyConfigured('Unknown serialisation "%s"' % method)
def load_pkg(name, dir=None): p = os.path dir = dir or JS_DIR with open(os.path.join(dir, name)) as f: data = f.read() return json.loads(data)