<s> import json <EOL> from flask import Response <EOL> from http_api . plugin import Plugin <EOL> from http_api . response import ResponseRaise , JSON_P <EOL> class JsonPRaise ( ResponseRaise ) : <EOL> """<STR_LIT>""" <EOL> def response ( self ) : <EOL> ret = { '<STR_LIT:code>' : self . code , '<STR_LIT:message>' : self . view . codes [ self . code ] , '<STR_LIT:data>' : self . data } <EOL> content = self . view . json_p_callback_name + '<STR_LIT:(>' + json . dumps ( ret ) + '<STR_LIT:)>' <EOL> return self . code , ret , Response ( content , content_type = JSON_P , status = self . status ) <EOL> class JsonP ( Plugin ) : <EOL> """<STR_LIT>""" <EOL> error_codes = { } <EOL> def __init__ ( self , params ) : <EOL> self . callback_field_name = params [ '<STR_LIT>' ] <EOL> def init_view ( self , view_class , method ) : <EOL> if method . __name__ is not '<STR_LIT>' : <EOL> raise Exception ( '<STR_LIT>' % <EOL> ( view_class . __name__ , method . __name__ ) ) <EOL> def before_request ( self , view ) : <EOL> if self . callback_field_name in view . params : <EOL> view . json_p_callback_name = view . params [ self . callback_field_name ] <EOL> view . raise_response = JsonPRaise <EOL> del view . params [ self . callback_field_name ] <EOL> if '<STR_LIT:_>' in view . params : <EOL> del view . params [ '<STR_LIT:_>' ] <EOL> if '<STR_LIT>' in view . params : <EOL> del view . params [ '<STR_LIT>' ] </s>
