summaryrefslogtreecommitdiff
path: root/cdsapi/api.py
diff options
context:
space:
mode:
Diffstat (limited to 'cdsapi/api.py')
-rw-r--r--cdsapi/api.py29
1 files changed, 8 insertions, 21 deletions
diff --git a/cdsapi/api.py b/cdsapi/api.py
index 17f8e51..27a6b09 100644
--- a/cdsapi/api.py
+++ b/cdsapi/api.py
@@ -9,10 +9,11 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import json
-import time
-import os
import logging
+import os
+import time
import uuid
+
import requests
try:
@@ -35,16 +36,15 @@ def bytes_to_string(n):
def read_config(path):
config = {}
with open(path) as f:
- for l in f.readlines():
- if ":" in l:
- k, v = l.strip().split(":", 1)
+ for line in f.readlines():
+ if ":" in line:
+ k, v = line.strip().split(":", 1)
if k in ("url", "key", "verify"):
config[k] = v.strip()
return config
def toJSON(obj):
-
to_json = getattr(obj, "toJSON", None)
if callable(to_json):
return to_json()
@@ -63,7 +63,6 @@ def toJSON(obj):
class Result(object):
def __init__(self, client, reply):
-
self.reply = reply
self._url = client.url
@@ -95,7 +94,6 @@ class Result(object):
return r
def _download(self, url, size, target):
-
if target is None:
target = url.split("/")[-1]
@@ -109,7 +107,6 @@ class Result(object):
headers = None
while tries < self.retry_max:
-
r = self.robust(self.session.get)(
url,
stream=True,
@@ -211,7 +208,6 @@ class Result(object):
self.reply = result.json()
def delete(self):
-
if self._deleted:
return
@@ -245,7 +241,6 @@ class Result(object):
class Client(object):
-
logger = logging.getLogger("cdsapi")
def __init__(
@@ -270,9 +265,7 @@ class Client(object):
forget=False,
session=requests.Session(),
):
-
if not quiet:
-
if debug:
level = logging.DEBUG
else:
@@ -326,7 +319,7 @@ class Client(object):
self.session = session
self.session.auth = tuple(self.key.split(":", 2))
- assert len(self.session.auth)==2, (
+ assert len(self.session.auth) == 2, (
"The cdsapi key provided is not the correct format, please ensure it conforms to:\n"
"<UID>:<APIKEY>"
)
@@ -361,7 +354,7 @@ class Client(object):
def service(self, name, *args, **kwargs):
self.delete = False # Don't delete results
name = "/".join(name.split("."))
- mimic_ui = kwargs.pop('mimic_ui', False)
+ mimic_ui = kwargs.pop("mimic_ui", False)
# To mimic the CDS ui the request should be populated directly with the kwargs
if mimic_ui:
request = kwargs
@@ -409,7 +402,6 @@ class Client(object):
pass
def _api(self, url, request, method):
-
self._status(url)
session = self.session
@@ -435,7 +427,6 @@ class Client(object):
result.raise_for_status()
reply = result.json()
except Exception:
-
if reply is None:
try:
reply = result.json()
@@ -465,7 +456,6 @@ class Client(object):
sleep = 1
while True:
-
self.debug("REPLY %s", reply)
if reply["state"] != self.last_state:
@@ -543,7 +533,6 @@ class Client(object):
self.logger.debug(*args, **kwargs)
def _download(self, results, targets=None):
-
if isinstance(results, Result):
if targets:
path = targets.pop(0)
@@ -555,7 +544,6 @@ class Client(object):
return [self._download(x, targets) for x in results]
if isinstance(results, dict):
-
if "location" in results and "contentLength" in results:
reply = dict(
location=results["location"],
@@ -594,7 +582,6 @@ class Client(object):
def robust(self, call):
def retriable(code, reason):
-
if code in [
requests.codes.internal_server_error,
requests.codes.bad_gateway,