-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_service.py
More file actions
136 lines (106 loc) · 5.02 KB
/
validation_service.py
File metadata and controls
136 lines (106 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Service methods to queue RO-Crates for validation using the CRS4 validator and Celery."""
# Author: Alexander Hambley
# License: MIT
# Copyright (c) 2025 eScience Lab, The University of Manchester
import logging
import json
from flask import jsonify, Response
from app.tasks.validation_tasks import (
process_validation_task_by_id,
process_validation_task_by_metadata,
return_ro_crate_validation,
check_ro_crate_exists,
check_validation_exists
)
from app.utils.config import InvalidAPIUsage
from app.utils.minio_utils import get_minio_client
logger = logging.getLogger(__name__)
def queue_ro_crate_validation_task(
minio_config, crate_id, root_path=None, profile_name=None, webhook_url=None,
profiles_path=None
) -> tuple[Response, int]:
"""
Queues an RO-Crate for validation with Celery.
:param minio_config: Access settings for Minio instance containing the RO-Crate.
:param crate_id: The ID of the RO-Crate to validate.
:param root_path: The root path containing the RO-Crate.
:param profile_name: The profile to validate against.
:param webhook_url: The URL to POST the validation results to.
:return: A tuple containing a JSON response and an HTTP status code.
:raises: Exception: If an error occurs whilst queueing the task.
"""
logging.info(f"Processing: {crate_id}, {profile_name}, {webhook_url}")
logging.info(f"Minio Bucket: {minio_config['bucket']}; Root path: {root_path}")
minio_client = get_minio_client(minio_config)
if check_ro_crate_exists(minio_client, minio_config["bucket"], crate_id, root_path):
logging.info("RO-Crate exists")
else:
logging.info("RO-Crate does not exist")
raise InvalidAPIUsage(f"No RO-Crate with prefix: {crate_id}", 400)
try:
process_validation_task_by_id.delay(minio_config, crate_id, root_path,
profile_name, webhook_url, profiles_path)
return jsonify({"message": "Validation in progress"}), 202
except Exception as e:
return jsonify({"error": str(e)}), 500
def queue_ro_crate_metadata_validation_task(
crate_json: str, profile_name=None, webhook_url=None, profiles_path=None
) -> tuple[Response, int]:
"""
Queues an RO-Crate for validation with Celery.
:param crate_id: The ID of the RO-Crate to validate.
:param profile_name: The profile to validate against.
:param webhook_url: The URL to POST the validation results to.
:param profiles_path: A path to the profile definition directory.
:return: A tuple containing a JSON response and an HTTP status code.
:raises: Exception: If an error occurs whilst queueing the task.
"""
logging.info(f"Processing: {crate_json}, {profile_name}, {webhook_url}")
if not crate_json:
return jsonify({"error": "Missing required parameter: crate_json"}), 422
try:
json_dict = json.loads(crate_json)
except json.decoder.JSONDecodeError as err:
return jsonify({"error": f"Required parameter crate_json is not valid JSON: {err}"}), 422
else:
if len(json_dict) == 0:
return jsonify({"error": "Required parameter crate_json is empty"}), 422
try:
result = process_validation_task_by_metadata.delay(
crate_json,
profile_name,
webhook_url,
profiles_path
)
if webhook_url:
return jsonify({"message": "Validation in progress"}), 202
else:
return jsonify({"result": result.get()}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
def get_ro_crate_validation_task(
minio_config: dict,
crate_id: str,
root_path: str,
) -> tuple[Response, int]:
"""
Retrieves an RO-Crate validation result.
:param minio_config: Access settings for Minio instance containing the RO-Crate.
:param crate_id: The ID of the RO-Crate to validate.
:param root_path: The root path containing the RO-Crate.
:return: A tuple containing a JSON response and an HTTP status code.
:raises Exception: If an error occurs whilst retreiving validation result
"""
logging.info(f"Retrieving validation for: {crate_id}")
minio_client = get_minio_client(minio_config)
if check_ro_crate_exists(minio_client, minio_config["bucket"], crate_id, root_path):
logging.info("RO-Crate exists")
else:
logging.info("RO-Crate does not exist")
raise InvalidAPIUsage(f"No RO-Crate with prefix: {crate_id}", 400)
if check_validation_exists(minio_client, minio_config["bucket"], crate_id, root_path):
logging.info("Validation result exists")
else:
logging.info("Validation does not exist")
raise InvalidAPIUsage(f"No validation result yet for RO-Crate: {crate_id}", 400)
return return_ro_crate_validation(minio_client, minio_config["bucket"], crate_id, root_path), 200