<s> import functools <EOL> import logging <EOL> import six <EOL> if six . PY2 : <EOL> from urllib import quote <EOL> else : <EOL> from urllib . parse import quote <EOL> from contextio . lib import helpers <EOL> from contextio . lib . errors import MissingResourceId <EOL> no_resource_id_required = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def only ( * versions ) : <EOL> def _only ( method ) : <EOL> @ functools . wraps ( method ) <EOL> def wrapper ( self , * args , ** kwargs ) : <EOL> if self . api_version not in list ( versions ) : <EOL> raise Exception ( <EOL> "<STR_LIT>" . format ( <EOL> method . __name__ , self . api_version ) ) <EOL> return method ( self , * args , ** kwargs ) <EOL> return wrapper <EOL> return _only <EOL> class BaseResource ( object ) : <EOL> """<STR_LIT>""" <EOL> keys = [ ] <EOL> def __init__ ( self , parent , base_uri , definition ) : <EOL> class_name = self . __class__ . __name__ <EOL> if class_name not in no_resource_id_required and self . resource_id not in definition : <EOL> raise MissingResourceId ( <EOL> "<STR_LIT>" . format ( <EOL> self . __class__ . __name__ , self . resource_id ) ) <EOL> if definition == "<STR_LIT>" : <EOL> logging . error ( '<STR_LIT>' + base_uri + "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> definition = helpers . uncamelize ( definition ) <EOL> except : <EOL> logging . error ( '<STR_LIT>' + base_uri + "<STR_LIT>" ) <EOL> return <EOL> self . parent = parent <EOL> self . api_version = parent . api_version if hasattr ( parent , "<STR_LIT>" ) else "<STR_LIT>" <EOL> self . _set_instance_attributes ( parent , definition ) <EOL> unidict = { six . text_type ( k ) : six . text_type ( v ) for k , v in definition . items ( ) } <EOL> self . base_uri = quote ( base_uri . format ( ** unidict ) ) <EOL> def _set_instance_attributes ( self , parent , definition ) : <EOL> if isinstance ( self . __class__ . keys , dict ) : <EOL> keys = self . __class__ . keys [ self . api_version ] <EOL> else : <EOL> keys = self . __class__ . keys <EOL> for k in keys : <EOL> if k in definition : <EOL> setattr ( self , k , definition [ k ] ) <EOL> else : <EOL> setattr ( self , k , None ) <EOL> def _uri_for ( self , * elems ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:/>' . join ( [ self . base_uri ] + list ( elems ) ) <EOL> def _request_uri ( self , uri_endpoint = "<STR_LIT>" , method = "<STR_LIT:GET>" , params = { } , headers = { } , body = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> uri = self . _uri_for ( uri_endpoint ) <EOL> return self . parent . _request_uri ( <EOL> uri , method = method , params = params , headers = headers , body = body ) <EOL> def get ( self , uri = "<STR_LIT>" , return_bool = True , params = { } , all_args = [ ] , required_args = [ ] ) : <EOL> response = self . _request_uri ( uri , params = helpers . sanitize_params ( params , all_args , required_args ) ) <EOL> self . __init__ ( self . parent , response ) <EOL> if return_bool : <EOL> return True <EOL> return response <EOL> def delete ( self , uri = "<STR_LIT>" ) : <EOL> response = self . _request_uri ( uri , method = '<STR_LIT>' ) <EOL> return bool ( response [ '<STR_LIT:success>' ] ) <EOL> def post ( self , uri = "<STR_LIT>" , return_bool = True , params = { } , headers = { } , all_args = [ ] , required_args = [ ] ) : <EOL> params = helpers . sanitize_params ( params , all_args , required_args ) <EOL> response = self . _request_uri ( uri , method = "<STR_LIT:POST>" , params = params , headers = headers ) <EOL> if return_bool : <EOL> return bool ( response [ '<STR_LIT:success>' ] ) <EOL> return response <EOL> def put ( self ) : <EOL> logging . info ( "<STR_LIT>" ) </s>
