diff --git a/.github/workflows/check-and-publish.yml b/.github/workflows/check-and-publish.yml index 0184526..5af80f8 100644 --- a/.github/workflows/check-and-publish.yml +++ b/.github/workflows/check-and-publish.yml @@ -35,7 +35,7 @@ jobs: fail-fast: false matrix: platform: [windows-latest, ubuntu-latest, macos-latest] - python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] name: Python ${{ matrix.python-version }} on ${{ matrix.platform }} runs-on: ${{ matrix.platform }} diff --git a/cdsapi/api.py b/cdsapi/api.py index 509f579..9b47e55 100644 --- a/cdsapi/api.py +++ b/cdsapi/api.py @@ -45,6 +45,35 @@ def read_config(path): return config +def get_url_key_verify(url, key, verify): + if url is None: + url = os.environ.get("CDSAPI_URL") + if key is None: + key = os.environ.get("CDSAPI_KEY") + dotrc = os.environ.get("CDSAPI_RC", os.path.expanduser("~/.cdsapirc")) + + if url is None or key is None: + if os.path.exists(dotrc): + config = read_config(dotrc) + + if key is None: + key = config.get("key") + + if url is None: + url = config.get("url") + + if verify is None: + verify = bool(int(config.get("verify", 1))) + + if url is None or key is None or key is None: + raise Exception("Missing/incomplete configuration file: %s" % (dotrc)) + + # If verify is still None, then we set to default value of True + if verify is None: + verify = True + return url, key, verify + + def toJSON(obj): to_json = getattr(obj, "toJSON", None) if callable(to_json): @@ -248,6 +277,14 @@ def __del__(self): class Client(object): logger = logging.getLogger("cdsapi") + def __new__(cls, url=None, key=None, *args, **kwargs): + _, token, _ = get_url_key_verify(url, key, None) + if ":" in token: + return super().__new__(cls) + import cads_api_client.legacy_api_client + + return super().__new__(cads_api_client.legacy_api_client.LegacyApiClient) + def __init__( self, url=None, @@ -285,31 +322,7 @@ def __init__( handler.setFormatter(formatter) self.logger.addHandler(handler) - if url is None: - url = os.environ.get("CDSAPI_URL") - if key is None: - key = os.environ.get("CDSAPI_KEY") - dotrc = os.environ.get("CDSAPI_RC", os.path.expanduser("~/.cdsapirc")) - - if url is None or key is None: - if os.path.exists(dotrc): - config = read_config(dotrc) - - if key is None: - key = config.get("key") - - if url is None: - url = config.get("url") - - if verify is None: - verify = bool(int(config.get("verify", 1))) - - if url is None or key is None or key is None: - raise Exception("Missing/incomplete configuration file: %s" % (dotrc)) - - # If verify is still None, then we set to default value of True - if verify is None: - verify = True + url, key, verify = get_url_key_verify(url, key, verify) self.url = url self.key = key diff --git a/setup.py b/setup.py index 1692456..3a79127 100644 --- a/setup.py +++ b/setup.py @@ -45,6 +45,7 @@ def read(fname): packages=setuptools.find_packages(), include_package_data=True, install_requires=[ + "cads-api-client>=0.9.2", "requests>=2.5.0", "tqdm", ], @@ -54,11 +55,11 @@ def read(fname): "Intended Audience :: Developers", "Programming Language :: Python", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Operating System :: OS Independent", diff --git a/tests/test_api.py b/tests/test_api.py index 3725f1f..f037889 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,5 +1,8 @@ import os +import cads_api_client.legacy_api_client +import pytest + import cdsapi @@ -19,3 +22,28 @@ def test_request(): r.download("test.grib") assert os.path.getsize("test.grib") == 2076600 + + +@pytest.mark.parametrize( + "key,expected_client", + [ + ( + ":", + cdsapi.Client, + ), + ( + "", + cads_api_client.legacy_api_client.LegacyApiClient, + ), + ], +) +@pytest.mark.parametrize("key_from_env", [True, False]) +def test_instantiation(monkeypatch, key, expected_client, key_from_env): + if key_from_env: + monkeypatch.setenv("CDSAPI_KEY", key) + c = cdsapi.Client() + else: + c = cdsapi.Client(key=key) + assert isinstance(c, cdsapi.Client) + assert isinstance(c, expected_client) + assert c.key == key diff --git a/tox.ini b/tox.ini index d97570f..ee8bfcc 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = qc, py311, py310, py39, py38, py37, pypy3, pypy, deps +envlist = qc, py312, py311, py310, py39, py38, pypy3, pypy, deps [testenv] setenv = PYTHONPATH = {toxinidir} @@ -21,4 +21,4 @@ line_length=120 [isort] profile=black [flake8] -max-line-length = 120 \ No newline at end of file +max-line-length = 120