-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation_service.py
More file actions
120 lines (92 loc) · 3.97 KB
/
Copy pathvalidation_service.py
File metadata and controls
120 lines (92 loc) · 3.97 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
"""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
logger = logging.getLogger(__name__)
def queue_ro_crate_validation_task(
crate_id, profile_name=None, webhook_url=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.
: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}")
if check_ro_crate_exists(crate_id):
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(crate_id, profile_name, webhook_url)
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
) -> 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.
: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
)
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(
crate_id
) -> tuple[Response, int]:
"""
Retrieves an RO-Crate validation result.
:param crate_id: The ID of the RO-Crate to validate.
: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}")
if check_ro_crate_exists(crate_id):
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(crate_id):
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(crate_id), 200