Merge branch 'master' into 1978-compare-pipelines
[arvados.git] / sdk / python / arvados / api.py
1 import httplib2
2 import json
3 import logging
4 import os
5 import re
6 import types
7
8 import apiclient
9 import apiclient.discovery
10 import config
11 import errors
12 import util
13
14 services = {}
15
16 class CredentialsFromEnv(object):
17     @staticmethod
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')
23         try:
24             return self.orig_http_request(uri, **kwargs)
25         except BadStatusLine:
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
31             # risky.
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)
36         return http
37
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):
42     global _cast_orig
43     if (type(value) != type('') and
44         (schema_type == 'object' or schema_type == 'array')):
45         return json.dumps(value)
46     else:
47         return _cast_orig(value, schema_type)
48 apiclient.discovery._cast = _cast_objects_too
49
50 def http_cache(data_type):
51     path = os.environ['HOME'] + '/.cache/arvados/' + data_type
52     try:
53         util.mkdir_dash_p(path)
54     except OSError:
55         path = None
56     return path
57
58 def api(version=None):
59     global services
60
61     if 'ARVADOS_DEBUG' in config.settings():
62         logging.basicConfig(level=logging.DEBUG)
63
64     if not services.get(version):
65         apiVersion = version
66         if not version:
67             apiVersion = 'v1'
68             logging.info("Using default API version. " +
69                          "Call arvados.api('%s') instead." %
70                          apiVersion)
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()
76
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
81
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]
91