<s> '''<STR_LIT>''' <EOL> import requests <EOL> import simplejson as json <EOL> _use_gevent = False <EOL> try : <EOL> import gevent <EOL> from gevent . coros import RLock <EOL> _use_gevent = True <EOL> except ImportError : <EOL> pass <EOL> class ElasticConnection ( object ) : <EOL> if _use_gevent : <EOL> session = None <EOL> session_lock = RLock ( ) <EOL> def __init__ ( self , timeout = None , ** params ) : <EOL> self . status_code = <NUM_LIT:0> <EOL> self . timeout = timeout <EOL> self . encoding = None <EOL> self . headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> if params . has_key ( '<STR_LIT>' ) : <EOL> self . encoding = '<STR_LIT:utf8>' <EOL> del params [ '<STR_LIT>' ] <EOL> if _use_gevent : <EOL> if ElasticConnection . session is None : <EOL> ElasticConnection . session_lock . acquire ( ) <EOL> ElasticConnection . session = requests . Session ( ** params ) <EOL> ElasticConnection . session_lock . release ( ) <EOL> else : <EOL> self . session = requests . Session ( timeout = timeout , ** params ) <EOL> def get ( self , url ) : <EOL> try : <EOL> response = self . session . get ( url , headers = self . headers , timeout = self . timeout ) <EOL> except requests . ConnectionError as e : <EOL> self . status_code = <NUM_LIT:0> <EOL> return { '<STR_LIT:error>' : e . message } <EOL> self . status_code = response . status_code <EOL> return json . loads ( response . content , encoding = self . encoding ) <EOL> def post ( self , url , data ) : <EOL> body = json . dumps ( data ) <EOL> try : <EOL> response = self . session . post ( url , data = body , headers = self . headers , timeout = self . timeout ) <EOL> except requests . ConnectionError as e : <EOL> self . status_code = <NUM_LIT:0> <EOL> return { '<STR_LIT:error>' : e . message } <EOL> self . status_code = response . status_code <EOL> return json . loads ( response . content , encoding = self . encoding ) <EOL> def put ( self , url , data ) : <EOL> body = json . dumps ( data ) <EOL> try : <EOL> response = self . session . post ( url , data = body , headers = self . headers , timeout = self . timeout ) <EOL> except requests . ConnectionError as e : <EOL> self . status_code = <NUM_LIT:0> <EOL> return { '<STR_LIT:error>' : e . message } <EOL> self . status_code = response . status_code <EOL> return json . loads ( response . content , encoding = self . encoding ) <EOL> def delete ( self , url ) : <EOL> try : <EOL> response = self . session . delete ( url , headers = self . headers , timeout = self . timeout ) <EOL> except requests . ConnectionError as e : <EOL> self . status_code = <NUM_LIT:0> <EOL> return { '<STR_LIT:error>' : e . message } <EOL> self . status_code = response . status_code <EOL> return json . loads ( response . content , encoding = self . encoding ) </s>
