<s> """<STR_LIT>""" <EOL> import sys <EOL> from flask import Blueprint , jsonify , make_response , request <EOL> __author__ = '<STR_LIT>' <EOL> __license__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> class FlaskWebhookHandleException ( BaseException ) : pass <EOL> class WebHook ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url_prefix , name = None , log = None , app = None ) : <EOL> """<STR_LIT>""" <EOL> self . _app = app <EOL> self . _log = log <EOL> self . _name = name <EOL> self . _blueprint = Blueprint ( self . _name , __name__ , url_prefix = url_prefix ) <EOL> self . handlers = dict ( ) <EOL> if app is not None : <EOL> self . init_app ( app ) <EOL> def add_route ( self , route , methods ) : <EOL> self . _blueprint . add_url_rule ( route , view_func = self . _run_endpoint_handlers , methods = methods ) <EOL> if self . _app : <EOL> self . init_app ( self . _app ) <EOL> @ property <EOL> def hook ( self ) : <EOL> return self . _blueprint <EOL> def _run_endpoint_handlers ( self ) : <EOL> """<STR_LIT>""" <EOL> for key , func in self . handlers . items ( ) : <EOL> try : <EOL> func ( request ) <EOL> except Exception : <EOL> if self . _log : <EOL> self . _log . error ( "<STR_LIT>" % ( key , sys . exc_info ( ) ) ) <EOL> raise FlaskWebhookHandleException ( "<STR_LIT>" % ( key , sys . exc_info ( ) ) ) <EOL> return_message = ( dict ( status = '<STR_LIT:success>' , message = "<STR_LIT>" % self . _name ) <EOL> if self . _name else dict ( status = '<STR_LIT:success>' ) ) <EOL> if self . _log : <EOL> self . _log . info ( return_message ) <EOL> return make_response ( jsonify ( return_message ) , <NUM_LIT:200> ) <EOL> def init_app ( self , app ) : <EOL> """<STR_LIT>""" <EOL> app . register_blueprint ( self . _blueprint ) </s>
