jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 1 | """ |
| 2 | This module provides utility functions useful when writing clients for the RPC |
| 3 | server. |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 4 | """ |
| 5 | |
| 6 | __author__ = 'showard@google.com (Steve Howard)' |
| 7 | |
jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 8 | import getpass, os |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 9 | from json_rpc import proxy |
jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import utils |
| 11 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 12 | |
mbligh | b68405d | 2010-03-11 18:32:39 +0000 | [diff] [blame^] | 13 | class AuthError(Exception): |
| 14 | pass |
| 15 | |
| 16 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 17 | def get_proxy(*args, **kwargs): |
jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 18 | """Use this to access the AFE or TKO RPC interfaces.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 19 | return proxy.ServiceProxy(*args, **kwargs) |
jamesren | 1a2914a | 2010-02-12 00:44:31 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def _base_authorization_headers(username, server): |
| 23 | """ |
| 24 | Don't call this directly, call authorization_headers(). |
| 25 | This implementation may be overridden by site code. |
| 26 | |
| 27 | @returns A dictionary of authorization headers to pass in to get_proxy(). |
| 28 | """ |
| 29 | if not username: |
| 30 | if 'AUTOTEST_USER' in os.environ: |
| 31 | username = os.environ['AUTOTEST_USER'] |
| 32 | else: |
| 33 | username = getpass.getuser() |
| 34 | return {'AUTHORIZATION' : username} |
| 35 | |
| 36 | |
| 37 | authorization_headers = utils.import_site_function( |
| 38 | __file__, 'autotest_lib.frontend.afe.site_rpc_client_lib', |
| 39 | 'authorization_headers', _base_authorization_headers) |