From 9ad85ccdf29cfbe64d1cd1384236cd9c0eac04af Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Tue, 25 Jun 2024 10:42:55 -0400 Subject: [PATCH] Add JSON parsing and fetching from GitHub This is how pre-commit-hooks will get the latest version of the metadata, since pre-commit caches the environment and doesn't update rapids-metadata as it's updated. --- dependencies.yaml | 1 + pyproject.toml | 1 + src/rapids_metadata/http.py | 31 +++++++++++++++++++++++++++++ tests/test_http.py | 39 +++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 src/rapids_metadata/http.py create mode 100644 tests/test_http.py diff --git a/dependencies.yaml b/dependencies.yaml index ec4c0d0..2462e54 100644 --- a/dependencies.yaml +++ b/dependencies.yaml @@ -67,3 +67,4 @@ dependencies: - output_types: [conda, requirements, pyproject] packages: - pytest + - pytest-httpserver diff --git a/pyproject.toml b/pyproject.toml index e2d1874..158a858 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,6 +44,7 @@ requires = [ [project.optional-dependencies] test = [ "pytest", + "pytest-httpserver", ] # This list was generated by `rapids-dependency-file-generator`. To make changes, edit dependencies.yaml and run `rapids-dependency-file-generator`. [tool.setuptools] diff --git a/src/rapids_metadata/http.py b/src/rapids_metadata/http.py new file mode 100644 index 0000000..360b9ef --- /dev/null +++ b/src/rapids_metadata/http.py @@ -0,0 +1,31 @@ +# Copyright (c) 2024, NVIDIA CORPORATION. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import urllib.request + +import pydantic + +from .metadata import RAPIDSMetadata + + +GITHUB_METADATA_URL = "https://raw.githubusercontent.com/rapidsai/rapids-metadata/main/rapids-metadata.json" + + +def fetch_from_url(url: str) -> RAPIDSMetadata: + with urllib.request.urlopen(url) as f: + return pydantic.TypeAdapter(RAPIDSMetadata).validate_json(f.read()) + + +def fetch_from_github() -> RAPIDSMetadata: + return fetch_from_url(GITHUB_METADATA_URL) diff --git a/tests/test_http.py b/tests/test_http.py new file mode 100644 index 0000000..3210240 --- /dev/null +++ b/tests/test_http.py @@ -0,0 +1,39 @@ +# Copyright (c) 2024, NVIDIA CORPORATION. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from unittest.mock import patch + +from pytest_httpserver import HTTPServer +from pydantic import TypeAdapter + +import rapids_metadata.http as rapids_http +from rapids_metadata import all_metadata +from rapids_metadata.metadata import RAPIDSMetadata + + +def test_fetch_from_url(httpserver: HTTPServer): + httpserver.expect_request("/rapids-metadata.json").respond_with_json( + TypeAdapter(RAPIDSMetadata).dump_python(all_metadata) + ) + assert ( + rapids_http.fetch_from_url(httpserver.url_for("/rapids-metadata.json")) + == all_metadata + ) + + +def test_fetch_from_github(): + with patch("rapids_metadata.http.fetch_from_url") as patch_fetch_from_url: + return_value = rapids_http.fetch_from_github() + patch_fetch_from_url.assert_called_once_with(rapids_http.GITHUB_METADATA_URL) + assert return_value == patch_fetch_from_url()