<s> import traceback <EOL> from flask import Response as FlaskResponse , request as flaskrequest <EOL> from werkzeug . exceptions import BadRequest as WerkzeugBadRequest <EOL> from . import Thorium , errors <EOL> from . request import Request <EOL> from . resources import VALID_METHODS , VALID_QUERY_PARAMETERS <EOL> from . parameters import ParametersMetaClass <EOL> from . crossdomain_decorator import crossdomain <EOL> class ThoriumFlask ( Thorium ) : <EOL> def __init__ ( self , settings , route_manager , flask_app ) : <EOL> self . _flask_app = flask_app <EOL> super ( ThoriumFlask , self ) . __init__ ( <EOL> settings = settings , <EOL> route_manager = route_manager , <EOL> debug = self . _flask_app . debug , <EOL> ) <EOL> def _bind_routes ( self ) : <EOL> routes = self . _route_manager . get_all_routes ( ) <EOL> for r in routes : <EOL> if r . path : <EOL> fep = FlaskEndpoint ( <EOL> dispatcher = r . dispatcher , <EOL> exception_handler = self . exception_handler , <EOL> flask_config = self . _flask_app . config , <EOL> ) <EOL> fep . __name__ = r . name <EOL> self . _flask_app . add_url_rule ( <EOL> rule = r . path , <EOL> endpoint = r . name , <EOL> view_func = fep . endpoint_target , <EOL> methods = VALID_METHODS , <EOL> ) <EOL> class FlaskEndpoint ( object ) : <EOL> def __init__ ( self , dispatcher , exception_handler , flask_config ) : <EOL> self . flask_config = flask_config <EOL> self . dispatcher = dispatcher <EOL> self . exception_handler = exception_handler <EOL> @ crossdomain ( origin = '<STR_LIT:*>' ) <EOL> def endpoint_target ( self , ** kwargs ) : <EOL> url = method = '<STR_LIT>' <EOL> request = None <EOL> try : <EOL> request = self . build_request ( ) <EOL> url = request . url <EOL> method = request . method <EOL> response , serialized_body = self . dispatcher . dispatch ( request ) <EOL> return FlaskResponse ( <EOL> response = serialized_body , <EOL> headers = response . headers , <EOL> status = response . status_code , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> except errors . HttpErrorBase as e : <EOL> error_body = self . exception_handler . handle_http_exception ( <EOL> http_error = e , <EOL> request = request , <EOL> ) <EOL> return FlaskResponse ( <EOL> response = error_body , <EOL> status = e . status_code , <EOL> headers = e . headers , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> except Exception as e : <EOL> error_body = self . exception_handler . handle_general_exception ( <EOL> url = url , <EOL> method = method , <EOL> e = e , <EOL> request = request , <EOL> ) <EOL> if self . flask_config [ '<STR_LIT>' ] : <EOL> raise e <EOL> return FlaskResponse ( <EOL> response = error_body , <EOL> status = <NUM_LIT> , <EOL> headers = { } , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> ) <EOL> def build_request ( self ) : <EOL> try : <EOL> resource = None <EOL> resources = [ ] <EOL> if flaskrequest . method . lower ( ) in { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } : <EOL> if flaskrequest . mimetype == '<STR_LIT:application/json>' : <EOL> json_data = flaskrequest . json or { } <EOL> partial = True if flaskrequest . method == '<STR_LIT>' else False <EOL> if isinstance ( json_data , list ) : <EOL> for i in json_data : <EOL> resources . append ( self . _create_resource ( i , partial ) ) <EOL> else : <EOL> resource = self . _create_resource ( json_data , partial ) <EOL> else : <EOL> raise errors . BadRequestError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return Request ( <EOL> dispatcher = self . dispatcher , <EOL> method = flaskrequest . method , <EOL> identifiers = flaskrequest . view_args , <EOL> query_params = self . _build_parameters ( ) , <EOL> mimetype = flaskrequest . mimetype , <EOL> resource = resource , <EOL> resources = resources , <EOL> url = flaskrequest . url , <EOL> ) <EOL> except ( errors . ValidationError , WerkzeugBadRequest ) as e : <EOL> raise errors . BadRequestError ( message = e . args [ <NUM_LIT:0> ] if e . args else None ) <EOL> def _create_resource ( self , data , partial ) : <EOL> data . update ( flaskrequest . view_args ) <EOL> return data <EOL> def _build_parameters ( self ) : <EOL> if not self . dispatcher . Parameters : <EOL> return None <EOL> flask_params = flaskrequest . args . to_dict ( ) if flaskrequest . args else { } <EOL> if isinstance ( self . dispatcher . Parameters , ParametersMetaClass ) : <EOL> return self . dispatcher . Parameters . validate ( flask_params ) <EOL> else : <EOL> self . _validate_no_extra_query_params ( flask_params ) <EOL> flask_params . update ( flaskrequest . view_args ) <EOL> params = self . dispatcher . Parameters . init_from_dict ( <EOL> data = flask_params , <EOL> partial = True , <EOL> cast = True , <EOL> ) <EOL> params . sort = flask_params . get ( '<STR_LIT>' ) <EOL> if not hasattr ( params , '<STR_LIT>' ) : <EOL> params . offset = flask_params . get ( '<STR_LIT>' ) <EOL> if not hasattr ( params , '<STR_LIT>' ) : <EOL> params . limit = flask_params . get ( '<STR_LIT>' ) <EOL> return params <EOL> def _validate_no_extra_query_params ( self , flask_params ) : <EOL> param_fields = dict ( self . dispatcher . Parameters . all_fields ( ) ) <EOL> for name , param in flask_params . items ( ) : <EOL> if name not in param_fields and name not in VALID_QUERY_PARAMETERS : <EOL> raise errors . ValidationError ( <EOL> '<STR_LIT>' . format ( name ) <EOL> ) </s>
