Created
April 4, 2024 15:15
-
-
Save borsna/e56134eebe4e104edc85c103475bd7c5 to your computer and use it in GitHub Desktop.
Validate responses from endpoint against maDMP 1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, json, urllib.request, os.path | |
from jsonschema import validate | |
countValid = 0 | |
countInvalid = 0 | |
api_endpoint = 'https://cthdmps.azurewebsites.net/api/v0/search' | |
bearer_token = 'mytoken' | |
if not os.path.exists('maDMP-schema-1.1.json'): | |
urllib.request.urlretrieve("https://raw.githubusercontent.com/RDA-DMP-Common/RDA-DMP-Common-Standard/master/examples/JSON/JSON-schema/1.1/maDMP-schema-1.1.json", "maDMP-schema-1.1.json") | |
with open('maDMP-schema-1.1.json') as f: | |
schema = json.load(f) | |
try: | |
headers = { | |
'Authorization': f'Bearer {bearer_token}' | |
} | |
response = requests.get(api_endpoint, headers=headers) | |
data = response.json() | |
for item in data['items']: | |
try: | |
validate(instance=item, schema=schema) | |
countValid += 1 | |
except Exception as e: | |
print("INVALID", item['dmp']['dmp_id']['identifier'], ":") | |
print(e.message) | |
print("") | |
countInvalid += 1 | |
print("Valid:", countValid) | |
print("Invalid:", countInvalid) | |
except requests.exceptions.RequestException as e: | |
print("Error fetching data:", e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pip install jsonschema requests
python madmp-validate.py