|
4 | 4 | # License: MIT |
5 | 5 | # Copyright (c) 2025 eScience Lab, The University of Manchester |
6 | 6 |
|
7 | | -from flask import Blueprint, request, Response |
| 7 | +from apiflask import APIBlueprint, Schema |
| 8 | +from apiflask.fields import Integer, String |
| 9 | +from flask import request, Response |
8 | 10 |
|
9 | 11 | from app.services.validation_service import queue_ro_crate_validation_task |
10 | 12 |
|
11 | | -post_routes_bp = Blueprint("post_routes", __name__) |
| 13 | +post_routes_bp = APIBlueprint("post_routes", __name__) |
12 | 14 |
|
| 15 | +class validate_data(Schema): |
| 16 | + crate_id = String(required=True) |
| 17 | + profile_name = String(required=False) |
| 18 | + webhook_url = String(required=False) |
13 | 19 |
|
14 | | -@post_routes_bp.route("/validate_by_id", methods=["POST"]) |
15 | | -def validate_ro_crate_from_id() -> tuple[Response, int]: |
16 | | - """ |
17 | | - Endpoint to validate an RO-Crate using its ID from MinIO. Requires webhook_url. |
18 | 20 |
|
19 | | - :param id: The ID of the RO-Crate to validate. Required. |
20 | | - :param profile_name: The profile name for validation. Optional. |
21 | | - :param webhook_url: The webhook URL where validation results will be sent. Required. |
22 | | - :return: A tuple containing the validation task's response and an HTTP status code. |
23 | | - :raises: KeyError: If required parameters (`id` or `webhook_url`) are missing. |
| 21 | +@post_routes_bp.post("/validate_by_id") |
| 22 | +@post_routes_bp.input(validate_data(partial=True), location='json') |
| 23 | +def validate_ro_crate_from_id(json_data) -> tuple[Response, int]: |
24 | 24 | """ |
| 25 | + Endpoint to validate an RO-Crate using its ID from MinIO. |
25 | 26 |
|
26 | | - crate_id = request.form.get("id") |
27 | | - profile_name = request.form.get("profile_name") |
28 | | - webhook_url = request.form.get("webhook_url") |
| 27 | + Parameters: |
| 28 | + - **crate_id**: The ID of the RO-Crate to validate. _Required_. |
| 29 | + - **profile_name**: The profile name for validation. _Optional_. |
| 30 | + - **webhook_url**: The webhook URL where validation results will be sent. _Required_. |
29 | 31 |
|
30 | | - if not crate_id: |
31 | | - raise KeyError("Missing required parameter: 'id'") |
32 | | - if not webhook_url: |
| 32 | + Returns: |
| 33 | + - A tuple containing the validation task's response and an HTTP status code. |
| 34 | +
|
| 35 | + Raises: |
| 36 | + - KeyError: If required parameters (`crate_id` or `webhook_url`) are missing. |
| 37 | + """ |
| 38 | + |
| 39 | + try: |
| 40 | + crate_id = json_data["crate_id"] |
| 41 | + except: |
| 42 | + raise KeyError("Missing required parameter: 'crate_id'") |
| 43 | + try: |
| 44 | + webhook_url = json_data["webhook_url"] |
| 45 | + except: |
33 | 46 | raise KeyError("Missing required parameter: 'webhook_url'") |
34 | 47 |
|
35 | | - return queue_ro_crate_validation_task(crate_id, profile_name, webhook_url) |
| 48 | + try: |
| 49 | + profile_name = json_data["profile_name"] |
| 50 | + except: |
| 51 | + profile_name = None |
36 | 52 |
|
| 53 | + return queue_ro_crate_validation_task(crate_id, profile_name, webhook_url) |
37 | 54 |
|
38 | | -@post_routes_bp.route("/validate_by_id_no_webhook", methods=["POST"]) |
39 | | -def validate_ro_crate_from_id_no_webhook() -> tuple[Response, int]: |
| 55 | +@post_routes_bp.post("/validate_by_id_no_webhook") |
| 56 | +@post_routes_bp.input(validate_data(partial=True), location='json') # -> json_data |
| 57 | +def validate_ro_crate_from_id_no_webhook(json_data) -> tuple[Response, int]: |
40 | 58 | """ |
41 | | - Endpoint to validate an RO-Crate using its ID from MinIO. Does not require webhook_url. |
| 59 | + Endpoint to validate an RO-Crate using its ID from MinIO. |
| 60 | +
|
| 61 | + Parameters: |
| 62 | + - **crate_id**: The ID of the RO-Crate to validate. _Required_. |
| 63 | + - **profile_name**: The profile name for validation. _Optional_. |
42 | 64 |
|
43 | | - :param id: The ID of the RO-Crate to validate. Required. |
44 | | - :param profile_name: The profile name for validation. Optional. |
45 | | - :return: A tuple containing the validation task's response and an HTTP status code. |
46 | | - :raises: KeyError: If required parameters (`id` or `webhook_url`) are missing. |
| 65 | + Returns: |
| 66 | + - A tuple containing the validation task's response and an HTTP status code. |
| 67 | +
|
| 68 | + Raises: |
| 69 | + - KeyError: If required parameters (`crate_id`) are missing. |
47 | 70 | """ |
48 | | - crate_id = request.form.get("id") |
49 | | - profile_name = request.form.get("profile_name") |
50 | 71 |
|
51 | | - if not crate_id: |
| 72 | + try: |
| 73 | + crate_id = json_data['crate_id'] |
| 74 | + except: |
52 | 75 | raise KeyError("Missing required parameter: 'id'") |
53 | 76 |
|
| 77 | + try: |
| 78 | + profile_name = json_data['profile_name'] |
| 79 | + except: |
| 80 | + profile_name = None |
| 81 | + |
54 | 82 | return queue_ro_crate_validation_task(crate_id, profile_name) |
0 commit comments