9 import apiclient.discovery
16 class CredentialsFromEnv(object):
18 def http_request(self, uri, **kwargs):
19 from httplib import BadStatusLine
20 if 'headers' not in kwargs:
21 kwargs['headers'] = {}
22 kwargs['headers']['Authorization'] = 'OAuth2 %s' % config.get('ARVADOS_API_TOKEN', 'ARVADOS_API_TOKEN_not_set')
24 return self.orig_http_request(uri, **kwargs)
26 # This is how httplib tells us that it tried to reuse an
27 # existing connection but it was already closed by the
28 # server. In that case, yes, we would like to retry.
29 # Unfortunately, we are not absolutely certain that the
30 # previous call did not succeed, so this is slightly
32 return self.orig_http_request(uri, **kwargs)
33 def authorize(self, http):
34 http.orig_http_request = http.request
35 http.request = types.MethodType(self.http_request, http)
38 # Monkey patch discovery._cast() so objects and arrays get serialized
39 # with json.dumps() instead of str().
40 _cast_orig = apiclient.discovery._cast
41 def _cast_objects_too(value, schema_type):
43 if (type(value) != type('') and
44 (schema_type == 'object' or schema_type == 'array')):
45 return json.dumps(value)
47 return _cast_orig(value, schema_type)
48 apiclient.discovery._cast = _cast_objects_too
50 def http_cache(data_type):
51 path = os.environ['HOME'] + '/.cache/arvados/' + data_type
53 util.mkdir_dash_p(path)
58 def api(version=None):
61 if 'ARVADOS_DEBUG' in config.settings():
62 logging.basicConfig(level=logging.DEBUG)
64 if not services.get(version):
68 logging.info("Using default API version. " +
69 "Call arvados.api('%s') instead." %
71 if 'ARVADOS_API_HOST' not in config.settings():
72 raise Exception("ARVADOS_API_HOST is not set. Aborting.")
73 url = ('https://%s/discovery/v1/apis/{api}/{apiVersion}/rest' %
74 config.get('ARVADOS_API_HOST'))
75 credentials = CredentialsFromEnv()
77 # Use system's CA certificates (if we find them) instead of httplib2's
78 ca_certs = '/etc/ssl/certs/ca-certificates.crt'
79 if not os.path.exists(ca_certs):
80 ca_certs = None # use httplib2 default
82 http = httplib2.Http(ca_certs=ca_certs,
83 cache=http_cache('discovery'))
84 http = credentials.authorize(http)
85 if re.match(r'(?i)^(true|1|yes)$',
86 config.get('ARVADOS_API_HOST_INSECURE', 'no')):
87 http.disable_ssl_certificate_validation=True
88 services[version] = apiclient.discovery.build(
89 'arvados', apiVersion, http=http, discoveryServiceUrl=url)
90 return services[version]