<s> import json <EOL> import urllib <EOL> import urllib2 <EOL> class Request : <EOL> def _request ( self , url , data , action , content_type ) : <EOL> if ( ( data or isinstance ( data , dict ) ) and <EOL> action not in ( '<STR_LIT:GET>' , '<STR_LIT>' , ) ) : <EOL> data = json . dumps ( data ) <EOL> if action == '<STR_LIT>' : <EOL> opener = urllib2 . build_opener ( urllib2 . HTTPHandler ) <EOL> req = urllib2 . Request ( url , data = data ) <EOL> req . add_header ( '<STR_LIT:Content-Type>' , content_type ) <EOL> req . get_method = lambda : '<STR_LIT>' <EOL> conn = opener . open ( req ) <EOL> else : <EOL> headers = { '<STR_LIT:Content-Type>' : content_type } <EOL> try : <EOL> req = urllib2 . Request ( url , data , headers ) <EOL> conn = urllib2 . urlopen ( req ) <EOL> except TypeError : <EOL> data = urllib . urlencode ( data , <NUM_LIT:1> ) <EOL> req = urllib2 . Request ( url , data , headers ) <EOL> conn = urllib2 . urlopen ( req ) <EOL> elif action == '<STR_LIT>' : <EOL> opener = urllib2 . build_opener ( urllib2 . HTTPHandler ) <EOL> req = urllib2 . Request ( url ) <EOL> req . get_method = lambda : '<STR_LIT>' <EOL> conn = opener . open ( req ) <EOL> else : <EOL> req = urllib2 . Request ( url ) <EOL> conn = urllib2 . urlopen ( req ) <EOL> response = conn . read ( ) <EOL> conn . close ( ) <EOL> try : <EOL> content = json . loads ( response ) <EOL> except : <EOL> content = response <EOL> return conn . info ( ) . headers , content <EOL> def get ( self , url , data = None , content_type = '<STR_LIT:application/json>' ) : <EOL> return self . _request ( url , data , '<STR_LIT:GET>' , content_type ) <EOL> def post ( self , url , data = { } , content_type = '<STR_LIT:application/json>' ) : <EOL> return self . _request ( url , data , '<STR_LIT:POST>' , content_type ) <EOL> def put ( self , url , data = { } , content_type = '<STR_LIT:application/json>' ) : <EOL> return self . _request ( url , data , '<STR_LIT>' , content_type ) <EOL> def delete ( self , url , data = None , content_type = '<STR_LIT:application/json>' ) : <EOL> return self . _request ( url , data , '<STR_LIT>' , content_type ) </s>
