<s> """<STR_LIT>""" <EOL> from synapse . api . errors import SynapseError , Codes <EOL> import logging <EOL> import simplejson <EOL> logger = logging . getLogger ( __name__ ) <EOL> def parse_integer ( request , name , default = None , required = False ) : <EOL> """<STR_LIT>""" <EOL> if name in request . args : <EOL> try : <EOL> return int ( request . args [ name ] [ <NUM_LIT:0> ] ) <EOL> except : <EOL> message = "<STR_LIT>" % ( name , ) <EOL> raise SynapseError ( <NUM_LIT> , message ) <EOL> else : <EOL> if required : <EOL> message = "<STR_LIT>" % ( name , ) <EOL> raise SynapseError ( <NUM_LIT> , message , errcode = Codes . MISSING_PARAM ) <EOL> else : <EOL> return default <EOL> def parse_boolean ( request , name , default = None , required = False ) : <EOL> """<STR_LIT>""" <EOL> if name in request . args : <EOL> try : <EOL> return { <EOL> "<STR_LIT:true>" : True , <EOL> "<STR_LIT:false>" : False , <EOL> } [ request . args [ name ] [ <NUM_LIT:0> ] ] <EOL> except : <EOL> message = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % ( name , ) <EOL> raise SynapseError ( <NUM_LIT> , message ) <EOL> else : <EOL> if required : <EOL> message = "<STR_LIT>" % ( name , ) <EOL> raise SynapseError ( <NUM_LIT> , message , errcode = Codes . MISSING_PARAM ) <EOL> else : <EOL> return default <EOL> def parse_string ( request , name , default = None , required = False , <EOL> allowed_values = None , param_type = "<STR_LIT:string>" ) : <EOL> """<STR_LIT>""" <EOL> if name in request . args : <EOL> value = request . args [ name ] [ <NUM_LIT:0> ] <EOL> if allowed_values is not None and value not in allowed_values : <EOL> message = "<STR_LIT>" % ( <EOL> name , "<STR_LIT:U+002CU+0020>" . join ( repr ( v ) for v in allowed_values ) <EOL> ) <EOL> raise SynapseError ( <NUM_LIT> , message ) <EOL> else : <EOL> return value <EOL> else : <EOL> if required : <EOL> message = "<STR_LIT>" % ( param_type , name ) <EOL> raise SynapseError ( <NUM_LIT> , message , errcode = Codes . MISSING_PARAM ) <EOL> else : <EOL> return default <EOL> def parse_json_value_from_request ( request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> content_bytes = request . content . read ( ) <EOL> except : <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> try : <EOL> content = simplejson . loads ( content_bytes ) <EOL> except simplejson . JSONDecodeError : <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" , errcode = Codes . NOT_JSON ) <EOL> return content <EOL> def parse_json_object_from_request ( request ) : <EOL> """<STR_LIT>""" <EOL> content = parse_json_value_from_request ( request ) <EOL> if type ( content ) != dict : <EOL> message = "<STR_LIT>" <EOL> raise SynapseError ( <NUM_LIT> , message , errcode = Codes . BAD_JSON ) <EOL> return content <EOL> class RestServlet ( object ) : <EOL> """<STR_LIT>""" <EOL> def register ( self , http_server ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> patterns = self . PATTERNS <EOL> for method in ( "<STR_LIT:GET>" , "<STR_LIT>" , "<STR_LIT:POST>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if hasattr ( self , "<STR_LIT>" % ( method , ) ) : <EOL> method_handler = getattr ( self , "<STR_LIT>" % ( method , ) ) <EOL> http_server . register_paths ( method , patterns , method_handler ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" ) </s>
