[ARVADOS] created: c6db15d0529212126920002665c9c99a4f864529

git at public.curoverse.com git at public.curoverse.com
Sat Jul 26 14:37:44 EDT 2014


        at  c6db15d0529212126920002665c9c99a4f864529 (commit)


commit c6db15d0529212126920002665c9c99a4f864529
Author: Tom Clegg <tom at curoverse.com>
Date:   Sat Jul 26 14:37:08 2014 -0400

    2800: Allow api() caller to specify api host and token.

diff --git a/sdk/python/arvados/api.py b/sdk/python/arvados/api.py
index 699c319..64c2125 100644
--- a/sdk/python/arvados/api.py
+++ b/sdk/python/arvados/api.py
@@ -4,6 +4,7 @@ import logging
 import os
 import re
 import types
+import hashlib
 
 import apiclient
 import apiclient.discovery
@@ -13,7 +14,10 @@ import util
 
 services = {}
 
-class CredentialsFromEnv(object):
+class CredentialsFromToken(object):
+    def __init__(self, api_token):
+        self.api_token = api_token
+
     @staticmethod
     def http_request(self, uri, **kwargs):
         from httplib import BadStatusLine
@@ -23,7 +27,7 @@ class CredentialsFromEnv(object):
         if config.get("ARVADOS_EXTERNAL_CLIENT", "") == "true":
             kwargs['headers']['X-External-Client'] = '1'
 
-        kwargs['headers']['Authorization'] = 'OAuth2 %s' % config.get('ARVADOS_API_TOKEN', 'ARVADOS_API_TOKEN_not_set')
+        kwargs['headers']['Authorization'] = 'OAuth2 %s' % self.arvados_api_token
         try:
             return self.orig_http_request(uri, **kwargs)
         except BadStatusLine:
@@ -35,6 +39,7 @@ class CredentialsFromEnv(object):
             # risky.
             return self.orig_http_request(uri, **kwargs)
     def authorize(self, http):
+        http.arvados_api_token = self.api_token
         http.orig_http_request = http.request
         http.request = types.MethodType(self.http_request, http)
         return http
@@ -59,24 +64,43 @@ def http_cache(data_type):
         path = None
     return path
 
-def api(version=None, cache=True):
+def api(version=None, cache=True, host=None, token=None, insecure=False):
     global services
 
     if 'ARVADOS_DEBUG' in config.settings():
         logging.basicConfig(level=logging.DEBUG)
 
-    if not cache or not services.get(version):
-        apiVersion = version
-        if not version:
-            apiVersion = 'v1'
-            logging.info("Using default API version. " +
-                         "Call arvados.api('%s') instead." %
-                         apiVersion)
-        if 'ARVADOS_API_HOST' not in config.settings():
-            raise Exception("ARVADOS_API_HOST is not set. Aborting.")
-        url = ('https://%s/discovery/v1/apis/{api}/{apiVersion}/rest' %
-               config.get('ARVADOS_API_HOST'))
-        credentials = CredentialsFromEnv()
+    if not version:
+        version = 'v1'
+        logging.info("Using default API version. " +
+                     "Call arvados.api('%s') instead." %
+                     version)
+    if host and token:
+        # Provided by caller
+        pass
+    elif not host and not token:
+        # Load from user configuration or environment
+        for x in ['ARVADOS_API_HOST', 'ARVADOS_API_TOKEN']:
+            if x not in config.settings():
+                raise Exception("%s is not set. Aborting." % x)
+        host = config.get('ARVADOS_API_HOST')
+        token = config.get('ARVADOS_API_TOKEN')
+        apiinsecure = re.match(r'(?i)^(true|1|yes)$',
+                               config.get('ARVADOS_API_HOST_INSECURE', 'no'))
+    else:
+        # Caller provided one but not the other
+        if not host:
+            raise Exception("token argument provided, but host missing.")
+        else:
+            raise Exception("host argument provided, but token missing.")
+
+    connprofile = hashlib.sha1(' '.join([
+        version, host, token, ('y' if apiinsecure else 'n')
+    ])).hexdigest()
+
+    if not cache or not services.get(connprofile):
+        url = 'https://%s/discovery/v1/apis/{api}/{apiVersion}/rest' % host
+        credentials = CredentialsFromToken(api_token=token)
 
         # Use system's CA certificates (if we find them) instead of httplib2's
         ca_certs = '/etc/ssl/certs/ca-certificates.crt'
@@ -86,10 +110,10 @@ def api(version=None, cache=True):
         http = httplib2.Http(ca_certs=ca_certs,
                              cache=(http_cache('discovery') if cache else None))
         http = credentials.authorize(http)
-        if re.match(r'(?i)^(true|1|yes)$',
-                    config.get('ARVADOS_API_HOST_INSECURE', 'no')):
-            http.disable_ssl_certificate_validation=True
-        services[version] = apiclient.discovery.build(
-            'arvados', apiVersion, http=http, discoveryServiceUrl=url)
+        if apiinsecure:
+            http.disable_ssl_certificate_validation = True
+        services[connprofile] = apiclient.discovery.build(
+            'arvados', version, http=http, discoveryServiceUrl=url)
         http.cache = None
-    return services[version]
+
+    return services[connprofile]

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list