<s> import traceback <EOL> import flask <EOL> from oslo_log import log as logging <EOL> from oslo_middleware import request_id as oslo_req_id <EOL> from werkzeug import datastructures <EOL> from sahara import context <EOL> from sahara import exceptions as ex <EOL> from sahara . i18n import _ <EOL> from sahara . i18n import _LE <EOL> from sahara . utils import wsgi <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Rest ( flask . Blueprint ) : <EOL> def get ( self , rule , status_code = <NUM_LIT:200> ) : <EOL> return self . _mroute ( '<STR_LIT:GET>' , rule , status_code ) <EOL> def post ( self , rule , status_code = <NUM_LIT> ) : <EOL> return self . _mroute ( '<STR_LIT:POST>' , rule , status_code ) <EOL> def post_file ( self , rule , status_code = <NUM_LIT> ) : <EOL> return self . _mroute ( '<STR_LIT:POST>' , rule , status_code , file_upload = True ) <EOL> def put ( self , rule , status_code = <NUM_LIT> ) : <EOL> return self . _mroute ( '<STR_LIT>' , rule , status_code ) <EOL> def put_file ( self , rule , status_code = <NUM_LIT> ) : <EOL> return self . _mroute ( '<STR_LIT>' , rule , status_code , file_upload = True ) <EOL> def delete ( self , rule , status_code = <NUM_LIT> ) : <EOL> return self . _mroute ( '<STR_LIT>' , rule , status_code ) <EOL> def patch ( self , rule , status_code = <NUM_LIT> ) : <EOL> return self . _mroute ( '<STR_LIT>' , rule , status_code ) <EOL> def _mroute ( self , methods , rule , status_code = None , ** kw ) : <EOL> if type ( methods ) is str : <EOL> methods = [ methods ] <EOL> return self . route ( rule , methods = methods , status_code = status_code , ** kw ) <EOL> def route ( self , rule , ** options ) : <EOL> status = options . pop ( '<STR_LIT>' , None ) <EOL> file_upload = options . pop ( '<STR_LIT>' , False ) <EOL> def decorator ( func ) : <EOL> endpoint = options . pop ( '<STR_LIT>' , func . __name__ ) <EOL> def handler ( ** kwargs ) : <EOL> context . set_ctx ( None ) <EOL> LOG . debug ( "<STR_LIT>" <EOL> . format ( kwargs = kwargs ) ) <EOL> _init_resp_type ( file_upload ) <EOL> if status : <EOL> flask . request . status_code = status <EOL> kwargs . pop ( "<STR_LIT>" ) <EOL> req_id = flask . request . environ . get ( oslo_req_id . ENV_REQUEST_ID ) <EOL> auth_plugin = flask . request . environ . get ( '<STR_LIT>' ) <EOL> ctx = context . Context ( <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) , <EOL> auth_plugin = auth_plugin , <EOL> request_id = req_id ) <EOL> context . set_ctx ( ctx ) <EOL> if flask . request . method in [ '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT:data>' ] = request_data ( ) <EOL> try : <EOL> return func ( ** kwargs ) <EOL> except ex . Forbidden as e : <EOL> return access_denied ( e ) <EOL> except ex . SaharaException as e : <EOL> return bad_request ( e ) <EOL> except Exception as e : <EOL> return internal_error ( <NUM_LIT> , '<STR_LIT>' , e ) <EOL> f_rule = "<STR_LIT>" + rule <EOL> self . add_url_rule ( f_rule , endpoint , handler , ** options ) <EOL> self . add_url_rule ( f_rule + '<STR_LIT>' , endpoint , handler , ** options ) <EOL> return func <EOL> return decorator <EOL> class RestV2 ( Rest ) : <EOL> def route ( self , rule , ** options ) : <EOL> status = options . pop ( '<STR_LIT>' , None ) <EOL> file_upload = options . pop ( '<STR_LIT>' , False ) <EOL> def decorator ( func ) : <EOL> endpoint = options . pop ( '<STR_LIT>' , func . __name__ ) <EOL> def handler ( ** kwargs ) : <EOL> context . set_ctx ( None ) <EOL> LOG . debug ( "<STR_LIT>" <EOL> . format ( kwargs = kwargs ) ) <EOL> _init_resp_type ( file_upload ) <EOL> if status : <EOL> flask . request . status_code = status <EOL> req_id = flask . request . environ . get ( oslo_req_id . ENV_REQUEST_ID ) <EOL> auth_plugin = flask . request . environ . get ( '<STR_LIT>' ) <EOL> ctx = context . Context ( <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] , <EOL> flask . request . headers [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) , <EOL> auth_plugin = auth_plugin , <EOL> request_id = req_id ) <EOL> context . set_ctx ( ctx ) <EOL> if flask . request . method in [ '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> kwargs [ '<STR_LIT:data>' ] = request_data ( ) <EOL> try : <EOL> return func ( ** kwargs ) <EOL> except ex . Forbidden as e : <EOL> return access_denied ( e ) <EOL> except ex . SaharaException as e : <EOL> return bad_request ( e ) <EOL> except Exception as e : <EOL> return internal_error ( <NUM_LIT> , '<STR_LIT>' , e ) <EOL> self . add_url_rule ( rule , endpoint , handler , ** options ) <EOL> self . add_url_rule ( rule + '<STR_LIT>' , endpoint , handler , ** options ) <EOL> return func <EOL> return decorator <EOL> RT_JSON = datastructures . MIMEAccept ( [ ( "<STR_LIT:application/json>" , <NUM_LIT:1> ) ] ) <EOL> def _init_resp_type ( file_upload ) : <EOL> """<STR_LIT>""" <EOL> resp_type = flask . request . accept_mimetypes <EOL> if flask . request . path . endswith ( '<STR_LIT>' ) : <EOL> resp_type = RT_JSON <EOL> flask . request . resp_type = resp_type <EOL> flask . request . file_upload = file_upload <EOL> def render ( res = None , resp_type = None , status = None , ** kwargs ) : <EOL> if not res : <EOL> res = { } <EOL> if type ( res ) is dict : <EOL> res . update ( kwargs ) <EOL> elif kwargs : <EOL> abort_and_log ( <NUM_LIT> , <EOL> _ ( "<STR_LIT>" ) ) <EOL> status_code = getattr ( flask . request , '<STR_LIT>' , None ) <EOL> if status : <EOL> status_code = status <EOL> if not status_code : <EOL> status_code = <NUM_LIT:200> <EOL> if not resp_type : <EOL> resp_type = getattr ( flask . request , '<STR_LIT>' , RT_JSON ) <EOL> if not resp_type : <EOL> resp_type = RT_JSON <EOL> serializer = None <EOL> if "<STR_LIT:application/json>" in resp_type : <EOL> resp_type = RT_JSON <EOL> serializer = wsgi . JSONDictSerializer ( ) <EOL> else : <EOL> abort_and_log ( <NUM_LIT> , _ ( "<STR_LIT>" ) % resp_type ) <EOL> body = serializer . serialize ( res ) <EOL> resp_type = str ( resp_type ) <EOL> return flask . Response ( response = body , status = status_code , <EOL> mimetype = resp_type ) <EOL> def request_data ( ) : <EOL> if hasattr ( flask . request , '<STR_LIT>' ) : <EOL> return flask . request . parsed_data <EOL> if not flask . request . content_length > <NUM_LIT:0> : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> return dict ( ) <EOL> if flask . request . file_upload : <EOL> return flask . request . data <EOL> deserializer = None <EOL> content_type = flask . request . mimetype <EOL> if not content_type or content_type in RT_JSON : <EOL> deserializer = wsgi . JSONDeserializer ( ) <EOL> else : <EOL> abort_and_log ( <NUM_LIT> , <EOL> _ ( "<STR_LIT>" ) % content_type ) <EOL> parsed_data = deserializer . deserialize ( flask . request . data ) [ '<STR_LIT:body>' ] <EOL> flask . request . parsed_data = parsed_data <EOL> return flask . request . parsed_data <EOL> def get_request_args ( ) : <EOL> return flask . request . args <EOL> def abort_and_log ( status_code , descr , exc = None ) : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( code = status_code , <EOL> message = descr ) ) <EOL> if exc is not None : <EOL> LOG . error ( traceback . format_exc ( ) ) <EOL> flask . abort ( status_code , description = descr ) <EOL> def render_error_message ( error_code , error_message , error_name ) : <EOL> message = { <EOL> "<STR_LIT>" : error_code , <EOL> "<STR_LIT>" : error_message , <EOL> "<STR_LIT>" : error_name <EOL> } <EOL> resp = render ( message ) <EOL> resp . status_code = error_code <EOL> return resp <EOL> def internal_error ( status_code , descr , exc = None ) : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( code = status_code , <EOL> message = descr ) ) <EOL> if exc is not None : <EOL> LOG . error ( traceback . format_exc ( ) ) <EOL> error_code = "<STR_LIT>" <EOL> if status_code == <NUM_LIT> : <EOL> error_code = "<STR_LIT>" <EOL> return render_error_message ( status_code , descr , error_code ) <EOL> def bad_request ( error ) : <EOL> error_code = <NUM_LIT> <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( code = error_code , <EOL> message = error . message , <EOL> name = error . code ) ) <EOL> return render_error_message ( error_code , error . message , error . code ) <EOL> def access_denied ( error ) : <EOL> error_code = <NUM_LIT> <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( code = error_code , <EOL> message = error . message , <EOL> name = error . code ) ) <EOL> return render_error_message ( error_code , error . message , error . code ) <EOL> def not_found ( error ) : <EOL> error_code = <NUM_LIT> <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( code = error_code , <EOL> message = error . message , <EOL> name = error . code ) ) <EOL> return render_error_message ( error_code , error . message , error . code ) <EOL> def to_wrapped_dict ( func , id , * args , ** kwargs ) : <EOL> obj = func ( id , * args , ** kwargs ) <EOL> if obj is None : <EOL> e = ex . NotFoundException ( <EOL> { '<STR_LIT:id>' : id } , _ ( '<STR_LIT>' ) ) <EOL> return not_found ( e ) <EOL> return render ( obj . to_wrapped_dict ( ) ) </s>
