summaryrefslogtreecommitdiff
path: root/cdsapi
diff options
context:
space:
mode:
authorBaudouin Raoult <baudouin.raoult@ecmwf.int>2018-05-09 14:27:01 +0100
committerBaudouin Raoult <baudouin.raoult@ecmwf.int>2018-05-09 14:27:01 +0100
commitd67c8654bd2676ffb1c58527194835a72f6d9584 (patch)
tree510c883bdafead84faad5c06e588e8ddb734586f /cdsapi
parent63eab605afee61fd1425b1c9cda902b63746d5df (diff)
api
Diffstat (limited to 'cdsapi')
-rw-r--r--cdsapi/api.py43
1 files changed, 39 insertions, 4 deletions
diff --git a/cdsapi/api.py b/cdsapi/api.py
index b840cee..15464f3 100644
--- a/cdsapi/api.py
+++ b/cdsapi/api.py
@@ -2,6 +2,7 @@ import requests
import json
import time
import datetime
+import os
def bytes_to_string(n):
@@ -15,16 +16,50 @@ def bytes_to_string(n):
class Client(object):
- def __init__(self, end_point, user_id, api_key, verbose=False, verify=True, timeout=None, full_stack=False):
+ def __init__(self, end_point=None, user_id=None, api_key=None, verbose=False, verify=None, timeout=None, full_stack=False):
+
+ dotrc = os.path.expanduser('~/.cdsapirc')
+
+ if end_point is None or user_id is None or api_key is None:
+ if os.path.exists(dotrc):
+ config = {}
+ with open(dotrc) as f:
+ for l in f.readlines():
+ k, v = l.strip().split(':', 1)
+ config[k] = v
+ url = config.get('url')
+ key = config.get('key')
+
+ if end_point is None:
+ end_point = url
+
+ if user_id is None and key is not None:
+ user_id = key.split(':')[0]
+
+ if api_key is None and key is not None:
+ api_key = key.split(':')[-1]
+
+ if verify is None:
+ verify = int(config.get('verify', 1))
+
+ if end_point is None or api_key is None or api_key is None:
+ raise Exception("Missing/incomplete configuration file: %s" % (dotrc))
+
+ if verify is None:
+ verify = True
+
self.end_point = end_point
self.user_id = user_id
self.api_key = api_key
+
self.verbose = verbose
self.verify = verify
self.timeout = timeout
self.sleep_max = 120
self.full_stack = full_stack
+ print("===>", self.end_point, self.user_id, self.api_key, self.verify)
+
def get_resource(self, name, request, target=None):
self._api("%s/resources/%s" % (self.end_point, name), request, target)
@@ -51,9 +86,9 @@ class Client(object):
result.raise_for_status()
try:
- reply = result.json()
- except:
- raise Exception(result.text)
+ reply = result.json()
+ except Exception:
+ raise Exception(result.text)
try:
result.raise_for_status()