-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added feature to pull entire remote catalog for use in project
- Loading branch information
Showing
15 changed files
with
183 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pytest | ||
|
||
from .test_universal_catalog_dataset import TEST_URL, TEST_METHOD | ||
|
||
from kedro.io.data_catalog import DataCatalog | ||
from kedro.io.core import DatasetError | ||
from universal_catalog import RemoteCatalog | ||
|
||
|
||
@pytest.fixture | ||
def remote_catalog(): | ||
return RemoteCatalog() | ||
|
||
|
||
@pytest.fixture | ||
def credentials(): | ||
return dict(remote_catalog=dict(url=TEST_URL)) | ||
|
||
|
||
@pytest.fixture | ||
def bad_credentials(): | ||
return {} | ||
|
||
|
||
@pytest.fixture | ||
def missing_url(): | ||
return dict(remote_catalog={"bad_url": "bad"}) | ||
|
||
|
||
@pytest.fixture | ||
def catalog_json(): | ||
return '{"companies": {"type": "pandas.CSVDataset", "filepath": "data/01_raw/companies.csv"}}' | ||
|
||
|
||
@pytest.fixture | ||
def update_catalog_dict(): | ||
return dict( | ||
cars=dict(type="pandas.CSVDataset", filepath="data/01_raw/companies.csv") | ||
) | ||
|
||
|
||
def test_remote_catalog(remote_catalog): | ||
assert isinstance(remote_catalog, DataCatalog) | ||
|
||
|
||
def test_bad_credentials(bad_credentials): | ||
with pytest.raises(DatasetError): | ||
RemoteCatalog.from_config(catalog=None, credentials=bad_credentials) | ||
|
||
|
||
def test_missing_url(missing_url): | ||
with pytest.raises(DatasetError): | ||
RemoteCatalog.from_config(catalog=None, credentials=missing_url) | ||
|
||
|
||
def test_load_remote_catalog(requests_mock, catalog_json, credentials): | ||
requests_mock.register_uri(TEST_METHOD, TEST_URL + "/catalog/", text=catalog_json) | ||
remote_catalog = RemoteCatalog.from_config(catalog=None, credentials=credentials) | ||
assert isinstance(remote_catalog, DataCatalog) | ||
assert "companies" in remote_catalog._datasets | ||
|
||
|
||
def test_update(requests_mock, catalog_json, credentials, update_catalog_dict): | ||
requests_mock.register_uri(TEST_METHOD, TEST_URL + "/catalog/", text=catalog_json) | ||
remote_catalog = RemoteCatalog.from_config( | ||
catalog=update_catalog_dict, credentials=credentials | ||
) | ||
assert isinstance(remote_catalog, DataCatalog) | ||
assert "companies" in remote_catalog._datasets | ||
assert "cars" in remote_catalog._datasets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .core import UniversalCatalogDataset | ||
from .core import UniversalCatalogDataset, RemoteCatalog | ||
|
||
__all__ = ["UniversalCatalogDataset"] | ||
__version__ = "0.1.0" | ||
__all__ = ["UniversalCatalogDataset", "RemoteCatalog"] | ||
__version__ = "0.1.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
from .universal_catalog_dataset import UniversalCatalogDataset | ||
from .datasets.universal_catalog_dataset import UniversalCatalogDataset | ||
from .datasets.datasets import Datasets | ||
from .datasets.remote_catalog import RemoteCatalog | ||
from .universal_catalog import UniversalCatalog, load_catalog | ||
from .datasets import Datasets | ||
|
||
from .serving import load_server_settings | ||
|
||
__all__ = [ | ||
"UniversalCatalogDataset", | ||
"UniversalCatalog", | ||
"load_catalog", | ||
"Datasets", | ||
"RemoteCatalog", | ||
"load_server_settings", | ||
] |
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import annotations | ||
|
||
from kedro.io import DataCatalog | ||
from kedro.io.core import DatasetError | ||
|
||
from typing import Any | ||
|
||
from .utils import _execute_request | ||
|
||
|
||
class RemoteCatalog(DataCatalog): | ||
"""``RemoteCatalog`` is a DataCatalog subclass that overrides the | ||
`from_config` class method to fetch the entire remote catalog | ||
and merge it with the DataCatalog from your project. | ||
If a dataset is present in both the remote catalog and the project catalog | ||
the project catalog entry is kept. | ||
""" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
@classmethod | ||
def from_config( | ||
cls, | ||
catalog: dict[str, dict[str, Any]] | None, | ||
credentials: dict[str, dict[str, Any]] | None = None, | ||
load_versions: dict[str, str] | None = None, | ||
save_version: str | None = None, | ||
) -> DataCatalog: | ||
if "remote_catalog" not in credentials.keys(): | ||
raise DatasetError( | ||
"`remote_catalog` must be provided in credentials.\nAlong with url." | ||
) | ||
if "url" not in credentials["remote_catalog"].keys(): | ||
raise DatasetError("`url` must be provided in `remote_catalog` entry.") | ||
url = credentials["remote_catalog"]["url"] | ||
url = url + "/catalog/" | ||
cfg = _execute_request(url, None).json() | ||
cfg.update(catalog or {}) | ||
return DataCatalog.from_config(cfg, credentials, load_versions, save_version) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from __future__ import annotations | ||
|
||
import requests | ||
import json | ||
|
||
from kedro.io.core import DatasetError | ||
|
||
|
||
def _execute_request(url: str, json_obj: dict[str, str] | None) -> requests.Response: | ||
try: | ||
response = requests.post(url, data=json.dumps(json_obj)) | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as exc: | ||
raise DatasetError("Failed to fetch data", exc) from exc | ||
except OSError as exc: | ||
raise DatasetError("Failed to connect to the remote server", exc) from exc | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters