Skip to content

Commit f121879

Browse files
authored
Merge pull request #38 from eScienceLab/feat/apiflask
Basic APIFlask integration
2 parents a8f712f + 151f480 commit f121879

5 files changed

Lines changed: 72 additions & 35 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,11 @@ app/
6363
## Example Usage
6464

6565
```
66-
curl -X POST http://localhost:5001/ro_crates/post/validate_by_id_no_webhook -d "id=1"
66+
curl -X 'POST' \
67+
'http://localhost:5001/ro_crates/validate_by_id_no_webhook' \
68+
-H 'accept: application/json' \
69+
-H 'Content-Type: application/json' \
70+
-d '{
71+
"crate_id": "1"
72+
}'
6773
```

app/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66

77
import os
88

9-
from flask import Flask
9+
from apiflask import APIFlask
1010

1111
from app.celery_worker import make_celery, celery
1212
from app.ro_crates.routes import ro_crates_bp
1313
from app.utils.config import DevelopmentConfig, ProductionConfig, make_celery
1414

1515

16-
def create_app() -> Flask:
16+
def create_app() -> APIFlask:
1717
"""
1818
Creates and configures Flask application.
1919
2020
:return: Flask: A configured Flask application instance.
2121
"""
22-
app = Flask(__name__)
22+
app = APIFlask(__name__)
2323
app.register_blueprint(ro_crates_bp, url_prefix="/ro_crates")
2424

2525
# Load configuration:

app/ro_crates/routes/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
# License: MIT
55
# Copyright (c) 2025 eScience Lab, The University of Manchester
66

7-
from flask import Blueprint
7+
from apiflask import APIBlueprint
88

99
from app.ro_crates.routes.post_routes import post_routes_bp
1010

11-
ro_crates_bp = Blueprint("ro_crates", __name__)
11+
#ro_crates_bp = APIBlueprint("ro_crates", __name__)
1212

13-
ro_crates_bp.register_blueprint(post_routes_bp, url_prefix="/post")
13+
#ro_crates_bp.register_blueprint(post_routes_bp, url_prefix="/post")
14+
15+
ro_crates_bp = post_routes_bp

app/ro_crates/routes/post_routes.py

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,79 @@
44
# License: MIT
55
# Copyright (c) 2025 eScience Lab, The University of Manchester
66

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
810

911
from app.services.validation_service import queue_ro_crate_validation_task
1012

11-
post_routes_bp = Blueprint("post_routes", __name__)
13+
post_routes_bp = APIBlueprint("post_routes", __name__)
1214

15+
class validate_data(Schema):
16+
crate_id = String(required=True)
17+
profile_name = String(required=False)
18+
webhook_url = String(required=False)
1319

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.
1820

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]:
2424
"""
25+
Endpoint to validate an RO-Crate using its ID from MinIO.
2526
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_.
2931
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:
3346
raise KeyError("Missing required parameter: 'webhook_url'")
3447

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
3652

53+
return queue_ro_crate_validation_task(crate_id, profile_name, webhook_url)
3754

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]:
4058
"""
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_.
4264
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.
4770
"""
48-
crate_id = request.form.get("id")
49-
profile_name = request.form.get("profile_name")
5071

51-
if not crate_id:
72+
try:
73+
crate_id = json_data['crate_id']
74+
except:
5275
raise KeyError("Missing required parameter: 'id'")
5376

77+
try:
78+
profile_name = json_data['profile_name']
79+
except:
80+
profile_name = None
81+
5482
return queue_ro_crate_validation_task(crate_id, profile_name)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Flask~=3.0.3
55
Werkzeug~=3.0.4
66
redis~=5.2.0
77
python-dotenv~=1.0.1
8+
apiflask~=2.4.0

0 commit comments

Comments
 (0)