-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (51 loc) · 2.09 KB
/
app.py
File metadata and controls
66 lines (51 loc) · 2.09 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
"""A Flask API for validating STAC (SpatioTemporal Asset Catalog) items using stac-check.
This API exposes an endpoint that accepts a JSON payload representing a STAC item,
validates it using stac-check, and returns validation results in the response.
"""
from flask import Flask, request, jsonify
from stac_check.lint import Linter
import logging
app = Flask(__name__)
# Configure logging
logging.basicConfig(level=logging.INFO)
@app.route("/", methods=["POST"])
def validate():
"""Validate a STAC item.
This endpoint accepts a JSON payload representing a STAC item, validates it using
stac-check, and returns validation results in the response. If the STAC item is
valid, a 200 status code is returned. If the STAC item is invalid, a 400 status
code is returned along with details about the issues. If there's an error in
processing, a 500 status code is returned with the error message.
:return: JSON response containing validation results
"""
data = request.get_json()
try:
# Using stac_check to validate
linter = Linter(data, assets=True)
best_practices = linter.create_best_practices_dict()
# Determine if there are any issues
issues = [v for k, v in best_practices.items() if v]
if issues:
return (
jsonify(
{
"status": "error",
"message": "STAC validation failed",
"issues": issues,
}
),
400,
)
else:
return jsonify({"status": "success", "message": "Valid STAC"}), 200
except Exception as e:
logging.error(f"Error during validation: {str(e)}")
return (
jsonify({"status": "error", "message": str(e), "error_type": str(type(e))}),
500,
)
if __name__ == "__main__":
"""Run the Flask application if the script is executed as the main module.
Starts the Flask development server on http://localhost:7000.
"""
app.run(debug=True, host="localhost", port=7000)