1""" 2This module provides utility functions useful when writing clients for the RPC 3server. 4""" 5 6__author__ = 'showard@google.com (Steve Howard)' 7 8import getpass, os 9from json_rpc import proxy 10from autotest_lib.client.common_lib import utils 11 12 13class AuthError(Exception): 14 pass 15 16def add_protocol(hostname): 17 """Constructs a normalized URL to make RPC calls 18 19 This function exists because our configuration files 20 (global_config/shadow_config) include only the hostname of the RPC server to 21 hit, not the protocol (http/https). 22 To support endpoints that require a specific protocol, we allow the hostname 23 to include the protocol prefix, and respect the protocol. If no protocol is 24 provided, http is used, viz, 25 26 add_protocol('cautotest') --> 'http://cautotest' 27 add_protocol('http://cautotest') --> 'http://cautotest' 28 add_protocol('https://cautotest') --> 'https://cautotest' 29 30 @param hostname: hostname or url prefix of the RPC server. 31 @returns: A string URL for the RPC server with the protocl prefix. 32 """ 33 if (not hostname.startswith('http://') and 34 not hostname.startswith('https://')): 35 return 'http://' + hostname 36 return hostname 37 38 39def get_proxy(*args, **kwargs): 40 """Use this to access the AFE or TKO RPC interfaces.""" 41 return proxy.ServiceProxy(*args, **kwargs) 42 43 44def _base_authorization_headers(username, server): 45 """ 46 Don't call this directly, call authorization_headers(). 47 This implementation may be overridden by site code. 48 49 @returns A dictionary of authorization headers to pass in to get_proxy(). 50 """ 51 if not username: 52 if 'AUTOTEST_USER' in os.environ: 53 username = os.environ['AUTOTEST_USER'] 54 else: 55 username = getpass.getuser() 56 return {'AUTHORIZATION' : username} 57 58 59authorization_headers = utils.import_site_function( 60 __file__, 'autotest_lib.frontend.afe.site_rpc_client_lib', 61 'authorization_headers', _base_authorization_headers) 62