Skip to content

Commit

Permalink
Add support for urls/network documents (#39)
Browse files Browse the repository at this point in the history
* Add support for urls/network documents

* Update README to include added option
  • Loading branch information
murithigeo authored Sep 24, 2024
1 parent 9c17b98 commit a2dd434
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
jsonschema
exhaust
pytest
pytest
requests
14 changes: 12 additions & 2 deletions tools/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a2dd434

Please sign in to comment.