From a2dd4340e27513e978acd3de36484d375fc8785d Mon Sep 17 00:00:00 2001 From: Edwin Gichuru <128323054+murithigeo@users.noreply.github.com> Date: Tue, 24 Sep 2024 19:05:29 +0300 Subject: [PATCH] Add support for urls/network documents (#39) * Add support for urls/network documents * Update README to include added option --- README.md | 6 ++++++ requirements.txt | 3 ++- tools/validator.py | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) 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 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)