<s> '''<STR_LIT>''' <EOL> import logging <EOL> import requests <EOL> import jsonpickle <EOL> class Region : <EOL> def __init__ ( self , name , base_url , type ) : <EOL> '''<STR_LIT>''' <EOL> self . name = name <EOL> self . base_url = base_url <EOL> self . type = type <EOL> self . session = requests . Session ( ) <EOL> def get_all ( self ) : <EOL> '''<STR_LIT>''' <EOL> url = self . base_url + "<STR_LIT>" <EOL> data = requests . get ( url ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> fdata = jsonpickle . decode ( data . text ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" + str ( data ) ) <EOL> return fdata [ self . name ] <EOL> else : <EOL> self . error_response ( data ) <EOL> def create ( self , key , value ) : <EOL> '''<STR_LIT>''' <EOL> url = self . base_url + "<STR_LIT>" + str ( key ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> jvalue = jsonpickle . encode ( value ) <EOL> data = self . session . post ( url , data = jvalue , headers = headers ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT> : <EOL> logging . debug ( "<STR_LIT>" + str ( value ) + "<STR_LIT>" + str ( key ) ) <EOL> return True <EOL> else : <EOL> self . error_response ( data ) <EOL> def put ( self , key , value ) : <EOL> '''<STR_LIT>''' <EOL> url = self . base_url + "<STR_LIT:/>" + str ( key ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> jvalue = jsonpickle . encode ( value ) <EOL> data = self . session . put ( url , data = jvalue , headers = headers ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" + str ( value ) + "<STR_LIT>" + str ( key ) ) <EOL> return True <EOL> else : <EOL> self . error_response ( data ) <EOL> def keys ( self ) : <EOL> '''<STR_LIT>''' <EOL> url = self . base_url + "<STR_LIT>" <EOL> data = self . session . get ( url ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> fdata = jsonpickle . decode ( data . text ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" + str ( data ) ) <EOL> return fdata [ "<STR_LIT>" ] <EOL> else : <EOL> self . error_response ( data ) <EOL> def get ( self , * arg ) : <EOL> '''<STR_LIT>''' <EOL> sub_url = '<STR_LIT:U+002C>' . join ( str ( key ) for key in arg ) <EOL> url = self . base_url + "<STR_LIT:/>" + sub_url + "<STR_LIT>" <EOL> data = self . session . get ( url ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" + str ( data ) ) <EOL> return jsonpickle . decode ( data . text ) <EOL> else : <EOL> self . error_response ( data ) <EOL> def __getitem__ ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> url = self . base_url + "<STR_LIT:/>" + str ( key ) + "<STR_LIT>" <EOL> data = self . session . get ( url ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" + str ( data ) ) <EOL> return jsonpickle . decode ( data . text ) <EOL> else : <EOL> self . error_response ( data ) <EOL> def put_all ( self , item ) : <EOL> '''<STR_LIT>''' <EOL> sub_url = '<STR_LIT:U+002C>' . join ( str ( keys ) for keys in item ) <EOL> url = self . base_url + "<STR_LIT:/>" + sub_url <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> jvalue = jsonpickle . encode ( list ( item . values ( ) ) ) <EOL> data = self . session . put ( url , data = jvalue , headers = headers ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( str ( item ) + "<STR_LIT>" ) <EOL> return True <EOL> else : <EOL> self . error_response ( data ) <EOL> def update ( self , key , value ) : <EOL> '''<STR_LIT>''' <EOL> url = self . base_url + "<STR_LIT:/>" + str ( key ) + "<STR_LIT>" <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> jvalue = jsonpickle . encode ( value ) <EOL> data = self . session . put ( url , data = jvalue , headers = headers ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" + str ( key ) + "<STR_LIT>" + str ( value ) ) <EOL> return True <EOL> else : <EOL> self . error_response ( data ) <EOL> def compare_and_set ( self , key , oldvalue , newvalue ) : <EOL> '''<STR_LIT>''' <EOL> url = self . base_url + "<STR_LIT:/>" + str ( key ) + "<STR_LIT>" <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> value = { "<STR_LIT>" : oldvalue , "<STR_LIT>" : newvalue } <EOL> jvalue = jsonpickle . encode ( value ) <EOL> data = self . session . put ( url , data = jvalue , headers = headers ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( str ( oldvalue ) + "<STR_LIT>" + str ( newvalue ) + "<STR_LIT>" + str ( key ) ) <EOL> return True <EOL> else : <EOL> self . error_response ( data ) <EOL> def delete ( self , * arg ) : <EOL> '''<STR_LIT>''' <EOL> sub_url = '<STR_LIT:U+002C>' . join ( str ( key ) for key in arg ) <EOL> url = self . base_url + "<STR_LIT:/>" + sub_url <EOL> data = self . session . delete ( url ) <EOL> logging . debug ( "<STR_LIT>" + url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" + str ( arg ) + "<STR_LIT>" ) <EOL> return True <EOL> else : <EOL> self . error_response ( data ) <EOL> def clear ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . type == "<STR_LIT>" : <EOL> data = self . session . delete ( self . base_url ) <EOL> if data . status_code == <NUM_LIT:200> : <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return True <EOL> else : <EOL> self . error_response ( data ) <EOL> if self . type == "<STR_LIT>" : <EOL> keys = self . keys ( ) <EOL> temp = "<STR_LIT:U+002C>" . join ( str ( key ) for key in keys ) <EOL> self . delete ( temp ) <EOL> return True <EOL> def error_response ( self , data ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( "<STR_LIT>" + str ( data . status_code ) + "<STR_LIT:U+0020>" + data . reason + "<STR_LIT>" + data . text ) <EOL> return False </s>
