From 8fa1b74a71d94fc51cda461c75c029227d6399a7 Mon Sep 17 00:00:00 2001 From: kegeotech Date: Sat, 21 Sep 2024 15:40:10 +0300 Subject: [PATCH 1/2] Add support for urls/network documents --- requirements.txt | 3 ++- tools/validator.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index c0c3a3e..82d3605 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ jsonschema exhaust -pytest \ No newline at end of file +pytest +requests \ No newline at end of file diff --git a/tools/validator.py b/tools/validator.py index 09a9525..af0e12b 100644 --- a/tools/validator.py +++ b/tools/validator.py @@ -46,12 +46,22 @@ def create_custom_validator(schema_id, schema_store=None): if __name__ == "__main__": import argparse parser = argparse.ArgumentParser() + parser.add_argument('--source', type=str, choices=['url', 'file'], default='file', help='Source of the CoverageJSON document') parser.add_argument('covjson_path', type=str, help='Path to CoverageJSON document') + args = parser.parse_args() - with open(args.covjson_path, encoding="utf-8") as f: - obj = json.load(f) + if args.source == 'url': + import requests + # Get the file from the URL + response = requests.get(args.covjson_path) + response.raise_for_status() # Raise an exception if the request was unsuccessful + obj = response.json() + else: + # Assume the covjson_path is a local file + with open(args.covjson_path, encoding="utf-8") as f: + obj = json.load(f) validator = create_custom_validator("/schemas/coveragejson") validator.validate(obj) From 15f22333fa4a36439de9a216062bfc5f7381844e Mon Sep 17 00:00:00 2001 From: kegeotech Date: Sat, 21 Sep 2024 15:53:59 +0300 Subject: [PATCH 2/2] Update README to include added option --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 97bef39..dc38db6 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,12 @@ N.B. Make sure to install requirements via `pip`, not `conda` (at the time of wr python -m tools.validator my.covjson ``` +To test a server/api response, specify that the file source is an URL using the `--source` flag. Its default value is `file` + +```sh +python -m tools.validator --source=url "https://mydomain.com/collections/cov" +``` + ## Testing the validator ```sh python -m pytest