<s> import json <EOL> import base64 <EOL> import requests <EOL> from urlparse import urljoin <EOL> from swift_rpc . aeslib import prpcrypt <EOL> class _RPC ( object ) : <EOL> def __init__ ( self , server , name , encryption , encryption_key ) : <EOL> self . encryption = encryption <EOL> self . encryption_key = encryption_key <EOL> self . __HEADERS__ = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> if encryption : <EOL> self . __HEADERS__ [ '<STR_LIT>' ] = self . encryption <EOL> self . _name = name <EOL> self . _url = urljoin ( server , name ) <EOL> def crypto_parser ( self , params ) : <EOL> if not self . encryption : <EOL> data = params <EOL> if self . encryption == "<STR_LIT>" : <EOL> data = base64 . encodestring ( params ) <EOL> elif self . encryption == "<STR_LIT>" : <EOL> salt = prpcrypt ( self . encryption_key ) <EOL> data = salt . encrypt ( params ) <EOL> return data if data else params <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> params = { } <EOL> params [ '<STR_LIT:args>' ] = args <EOL> params [ '<STR_LIT>' ] = kwargs <EOL> post_data = json . dumps ( params ) <EOL> if self . encryption : <EOL> post_data = self . crypto_parser ( post_data ) <EOL> try : <EOL> resp = requests . get ( self . _url , data = post_data , headers = self . __HEADERS__ ) <EOL> except Exception as e : <EOL> raise RPCClient . FailedCall ( e ) <EOL> if resp . status_code == <NUM_LIT> : <EOL> raise RPCClient . MissingMethod ( <EOL> '<STR_LIT>' . format ( self . _name ) ) <EOL> try : <EOL> ret = json . loads ( resp . content ) <EOL> except Exception as e : <EOL> raise RPCClient . InvalidSerializationError ( e ) <EOL> if '<STR_LIT:error>' in ret : <EOL> raise RPCClient . FailedCall ( ret [ '<STR_LIT:error>' ] ) <EOL> return ret [ '<STR_LIT>' ] <EOL> class RPCClient ( object ) : <EOL> class FailedCall ( Exception ) : pass <EOL> class InvalidSerializationError ( Exception ) : pass <EOL> class MissingMethod ( Exception ) : pass <EOL> __UNALLOWED__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def __init__ ( self , server , encryption = None , encryption_key = None , unallowed_calls = [ ] , load_remotes = True ) : <EOL> self . encryption = encryption <EOL> self . encryption_key = encryption_key <EOL> if server . startswith ( '<STR_LIT:http>' ) : <EOL> self . _server = server <EOL> else : <EOL> self . _server = '<STR_LIT>' . format ( server ) <EOL> self . _unallowed = unallowed_calls + self . __UNALLOWED__ <EOL> if load_remotes : <EOL> self . __loadremoteroutes ( ) <EOL> def __send ( self , name ) : <EOL> return _RPC ( self . _server , name , self . encryption , self . encryption_key ) <EOL> def __remoteroutes ( self ) : <EOL> return self . _getroutes ( ) <EOL> def __loadremoteroutes ( self ) : <EOL> for route in self . __remoteroutes ( ) : <EOL> setattr ( self , route , self . __send ( route ) ) <EOL> def __getattr__ ( self , name ) : <EOL> return None if name in self . _unallowed else self . __send ( name ) <EOL> __all__ = ( '<STR_LIT>' ) </s>
