|
| 1 | +from flask import Blueprint, request, Response |
| 2 | +import json |
| 3 | + |
| 4 | +from tdd.common import ( |
| 5 | + delete_id, |
| 6 | +) |
| 7 | +from tdd.utils import ( |
| 8 | + POSSIBLE_MIMETYPES, |
| 9 | + negociate_mime_type, |
| 10 | + update_collection_etag, |
| 11 | +) |
| 12 | +from tdd.errors import WrongMimeType |
| 13 | + |
| 14 | + |
| 15 | +from domus_tdd_api_plugin_aid.aas import ( |
| 16 | + get_aas_description, |
| 17 | + put_aas_json_in_sparql, |
| 18 | + put_aas_rdf_in_sparql, |
| 19 | + validate_aas, |
| 20 | +) |
| 21 | +from domus_tdd_api_plugin_aid.aml import translate_aml_to_aas |
| 22 | + |
| 23 | +blueprint = Blueprint("domus_tdd_api_plugin_aid", __name__, url_prefix="/aas") |
| 24 | + |
| 25 | + |
| 26 | +@blueprint.route("/<id>", methods=["DELETE"]) |
| 27 | +def delete_route_aas(id): |
| 28 | + response = delete_id(id) |
| 29 | + if response.status_code in [200, 204]: |
| 30 | + update_collection_etag() |
| 31 | + return response |
| 32 | + |
| 33 | + |
| 34 | +@blueprint.route("/<id>", methods=["GET"]) |
| 35 | +def describe_aas(id): |
| 36 | + mime_type_negociated = negociate_mime_type( |
| 37 | + request, default_mimetype="application/aas+json" |
| 38 | + ) |
| 39 | + description = get_aas_description(id, content_type=mime_type_negociated) |
| 40 | + if mime_type_negociated == "application/aas+json": |
| 41 | + description = json.dumps(description) |
| 42 | + return Response(description, content_type=mime_type_negociated) |
| 43 | + |
| 44 | + |
| 45 | +@blueprint.route("/<id>", methods=["PUT"]) |
| 46 | +def create_aas(id): |
| 47 | + mimetype = request.content_type |
| 48 | + if mimetype == "application/json": |
| 49 | + mimetype = "application/aas+json" |
| 50 | + if mimetype == "application/aas+json": |
| 51 | + json_ld_content = request.get_data() |
| 52 | + content = validate_aas(json_ld_content, uri=id) |
| 53 | + updated, uri = put_aas_json_in_sparql(content, uri=id) |
| 54 | + elif mimetype == "application/aml+xml": |
| 55 | + aml_data = request.get_data() |
| 56 | + uri, rdf_aas_data = translate_aml_to_aas(aml_data, uri=id) |
| 57 | + updated, uri = put_aas_rdf_in_sparql( |
| 58 | + rdf_aas_data, "application/n-triples", uri=uri |
| 59 | + ) |
| 60 | + elif mimetype in POSSIBLE_MIMETYPES: |
| 61 | + rdf_content = request.get_data() |
| 62 | + updated, uri = put_aas_rdf_in_sparql(rdf_content, mimetype, uri=id) |
| 63 | + else: |
| 64 | + raise WrongMimeType(mimetype) |
| 65 | + |
| 66 | + update_collection_etag() |
| 67 | + return Response(status=201 if not updated else 204, headers={"Location": uri}) |
| 68 | + |
| 69 | + |
| 70 | +@blueprint.route("", methods=["POST"]) |
| 71 | +def create_anonymous_aas(): |
| 72 | + mimetype = request.content_type |
| 73 | + if mimetype == "application/json": |
| 74 | + mimetype = "application/aas+json" |
| 75 | + if mimetype == "application/aas+json": |
| 76 | + json_ld_content = request.get_data() |
| 77 | + content = validate_aas(json_ld_content) |
| 78 | + updated, uri = put_aas_json_in_sparql(content, delete_if_exists=False) |
| 79 | + elif mimetype == "application/aml+xml": |
| 80 | + aml_data = request.get_data() |
| 81 | + uri, rdf_aas_data = translate_aml_to_aas(aml_data) |
| 82 | + updated, uri = put_aas_rdf_in_sparql( |
| 83 | + rdf_aas_data, "application/n-triples", uri=uri |
| 84 | + ) |
| 85 | + elif mimetype in POSSIBLE_MIMETYPES: |
| 86 | + content = request.get_data() |
| 87 | + updated, uri = put_aas_rdf_in_sparql(content, mimetype, delete_if_exists=False) |
| 88 | + else: |
| 89 | + raise WrongMimeType(mimetype) |
| 90 | + update_collection_etag() |
| 91 | + return Response(status=201 if not updated else 204, headers={"Location": uri}) |
0 commit comments