<s> """<STR_LIT>""" <EOL> import asyncio <EOL> import json <EOL> from base64 import b64encode , b64decode <EOL> from autobahn . asyncio . websocket import WebSocketServerProtocol , WebSocketServerFactory <EOL> from aiorest_ws . abstract import AbstractRouter <EOL> from aiorest_ws . routers import SimpleRouter <EOL> from aiorest_ws . validators import check_and_set_subclass <EOL> from aiorest_ws . wrappers import Request <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , ) <EOL> class RequestHandlerProtocol ( WebSocketServerProtocol ) : <EOL> """<STR_LIT>""" <EOL> def _decode_message ( self , payload , isBinary = False ) : <EOL> """<STR_LIT>""" <EOL> if isBinary : <EOL> payload = b64decode ( payload ) <EOL> input_data = json . loads ( payload . decode ( '<STR_LIT:utf-8>' ) ) <EOL> return Request ( ** input_data ) <EOL> def _encode_message ( self , response , isBinary = False ) : <EOL> """<STR_LIT>""" <EOL> if isBinary : <EOL> response = b64encode ( response ) <EOL> return response <EOL> @ asyncio . coroutine <EOL> def onMessage ( self , payload , isBinary ) : <EOL> """<STR_LIT>""" <EOL> request = self . _decode_message ( payload , isBinary ) <EOL> response = self . factory . router . process_request ( request ) <EOL> out_payload = self . _encode_message ( response , isBinary ) <EOL> self . sendMessage ( out_payload , isBinary = isBinary ) <EOL> class RequestHandlerFactory ( WebSocketServerFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( RequestHandlerFactory , self ) . __init__ ( * args , ** kwargs ) <EOL> self . _router = kwargs . get ( '<STR_LIT>' , SimpleRouter ( * args , ** kwargs ) ) <EOL> @ property <EOL> def router ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _router <EOL> @ router . setter <EOL> def router ( self , router ) : <EOL> """<STR_LIT>""" <EOL> if router : <EOL> check_and_set_subclass ( self , '<STR_LIT>' , router , AbstractRouter ) </s>
