diff --git a/dandischema/metadata.py b/dandischema/metadata.py index b1f4134..f639d9c 100644 --- a/dandischema/metadata.py +++ b/dandischema/metadata.py @@ -1,4 +1,5 @@ from copy import deepcopy +from functools import lru_cache import json from pathlib import Path from typing import Any, Dict, Iterable, Optional, TypeVar, Union, cast @@ -129,6 +130,14 @@ def _validate_asset_json(data: dict, schema_dir: Union[str, Path]) -> None: _validate_obj_json(data, schema) +@lru_cache +def _get_schema(schema_version: str, schema_name: str) -> Any: + return requests.get( + "https://raw.githubusercontent.com/dandi/schema/" + f"master/releases/{schema_version}/{schema_name}" + ).json() + + def validate( obj: dict, schema_version: Optional[str] = None, @@ -184,11 +193,7 @@ def validate( "Only dandisets and assets can be validated " "using json schema for older versions" ) - schema_filename = schema_map[schema_key] - schema = requests.get( - f"https://raw.githubusercontent.com/dandi/schema/" - f"master/releases/{schema_version}/{schema_filename}" - ).json() + schema = _get_schema(schema_version, schema_map[schema_key]) _validate_obj_json(obj, schema, missing_ok) klass = getattr(models, schema_key) try: @@ -223,10 +228,7 @@ def migrate( if version2tuple(schema_version) > version2tuple(to_version): raise ValueError(f"Cannot migrate from {schema_version} to lower {to_version}.") if not (skip_validation): - schema = requests.get( - f"https://raw.githubusercontent.com/dandi/schema/" - f"master/releases/{schema_version}/dandiset.json" - ).json() + schema = _get_schema(schema_version, "dandiset.json") _validate_obj_json(obj, schema) if version2tuple(schema_version) < version2tuple("0.6.0"): for val in obj.get("about", []):