<s> from __future__ import unicode_literals <EOL> from flask . _compat import text_type <EOL> from flask_api import exceptions <EOL> from werkzeug . formparser import MultiPartParser as WerkzeugMultiPartParser <EOL> from werkzeug . formparser import default_stream_factory <EOL> from werkzeug . urls import url_decode_stream <EOL> import json <EOL> class BaseParser ( object ) : <EOL> media_type = None <EOL> handles_file_uploads = False <EOL> handles_form_data = False <EOL> def parse ( self , stream , media_type , ** options ) : <EOL> msg = '<STR_LIT>' <EOL> raise NotImplementedError ( msg % self . __class__ . __name__ ) <EOL> class JSONParser ( BaseParser ) : <EOL> media_type = '<STR_LIT:application/json>' <EOL> def parse ( self , stream , media_type , ** options ) : <EOL> data = stream . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> try : <EOL> return json . loads ( data ) <EOL> except ValueError as exc : <EOL> msg = '<STR_LIT>' % text_type ( exc ) <EOL> raise exceptions . ParseError ( msg ) <EOL> class MultiPartParser ( BaseParser ) : <EOL> media_type = '<STR_LIT>' <EOL> handles_file_uploads = True <EOL> handles_form_data = True <EOL> def parse ( self , stream , media_type , ** options ) : <EOL> multipart_parser = WerkzeugMultiPartParser ( default_stream_factory ) <EOL> boundary = media_type . params . get ( '<STR_LIT>' ) <EOL> if boundary is None : <EOL> msg = '<STR_LIT>' <EOL> raise exceptions . ParseError ( msg ) <EOL> boundary = boundary . encode ( '<STR_LIT:ascii>' ) <EOL> content_length = options . get ( '<STR_LIT>' ) <EOL> assert content_length is not None , '<STR_LIT>' <EOL> try : <EOL> return multipart_parser . parse ( stream , boundary , content_length ) <EOL> except ValueError as exc : <EOL> msg = '<STR_LIT>' % text_type ( exc ) <EOL> raise exceptions . ParseError ( msg ) <EOL> class URLEncodedParser ( BaseParser ) : <EOL> media_type = '<STR_LIT>' <EOL> handles_form_data = True <EOL> def parse ( self , stream , media_type , ** options ) : <EOL> return url_decode_stream ( stream ) </s>
