Skip to content

Instantly share code, notes, and snippets.

@borsna
Created April 4, 2024 15:15
Show Gist options
  • Save borsna/e56134eebe4e104edc85c103475bd7c5 to your computer and use it in GitHub Desktop.
Save borsna/e56134eebe4e104edc85c103475bd7c5 to your computer and use it in GitHub Desktop.
Validate responses from endpoint against maDMP 1.1
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)
@borsna
Copy link
Author

borsna commented Apr 4, 2024

pip install jsonschema requests
python madmp-validate.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment