async def socket_handler(self, request): ws = aiohttp.web.WebSocketResponse() ok, protocol = ws.can_prepare(request) if not ok: return aiohttp.web.Response(text='Somthing went wrong') await ws.prepare(request) request.app['sockets'].add(ws) try: async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: try: req = json.loads(msg.data) if 'admin' in req: await self.cb(req) elif 'config' in req: log.info('Recieved new configuration: {}', req) await self.cb(req) except Exception as e: log.exception('Unexpected Error: {}', e) await ws.close() elif msg.type == aiohttp.WSMsgType.ERROR: log.info('Conn closed w/ exception: {}', ws.exception()) await ws.close() finally: request.app['sockets'].remove(ws) return ws
async def cancel_order(self, order): try: await self._session.cancel(order) except oe_exceptions.CancelNack as nack: self._log_error('cancel_nack', nack, construct_error('cancel_order', order)) except Exception as e: log.exception('Unhandled err [{}] when trying to cancel order.', e)
async def cancel_all(self): try: await self._session.cancel_all() await self._log_balance() await self._log_orders() except oe_exceptions.CancelNack as nack: self._log_error('cancel_all_nack', nack, construct_error('cancel_all')) except Exception as e: log.exception('Unhandled err [{}] when trying to cancel all.', e)
async def submit_order(self, **kwargs): try: order = await self._session.submit(**kwargs) if (order): await self._log_ack("submit_ack", order.dump()) return order except oe_exceptions.SubmitNack as e: self._log_error('submit_nack', e, construct_error('submit')) except oe_exceptions.LimitError as e: self._log_error('limit_error', e, construct_error('submit')) except oe_exceptions.InternalNack as e: self._log_error('internal_nack', e, construct_error('submit')) except oe_exceptions.OrderError as e: self._log_error('order_error', e, construct_error('submit')) except Exception as e: log.exception('Unhandled err [{}] when trying to submit order.', e)