Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COPDS-1531: beta api #95

Merged
merged 17 commits into from
Apr 24, 2024
2 changes: 1 addition & 1 deletion .github/workflows/check-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
63 changes: 38 additions & 25 deletions cdsapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand All @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os

import cads_api_client.legacy_api_client
import pytest

import cdsapi


Expand All @@ -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
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -21,4 +21,4 @@ line_length=120
[isort]
profile=black
[flake8]
max-line-length = 120
max-line-length = 120
Loading