from flask import Flask, request
from python_graphql_client import GraphqlClient
AQ_WEBHOOK_SECRET = os.getenv("AQ_WEBHOOK_SECRET")
LABELING_API_KEY = os.getenv("LABELING_API_KEY")
LABELING_API_ENDPOINT = os.getenv("LABELING_API_ENDPOINT")
# Replace with the proper API key header if any for your service
"Authorization": f"Bearer {LABELING_API_KEY}"
client = GraphqlClient(endpoint=LABELING_API_ENDPOINT, headers=labeling_api_headers)
# Replace with appropriate graphql mutation.
# In this example, the way to requeue a label is to remove it and mark as a template
relabel_mutation_fragment = """
mutation BulkDeleteLabels (
$makeTemplates: Boolean = true,
project (where: {id: $projectId}) {
makeTemplates: $makeTemplates,
@app.route("/webhook", methods=["POST"])
def handle_webhook_payload():
# Optionally verify that the payload came from Aquarium
aq_secret = request.headers.get("x-aquarium-secret")
if aq_secret != AQ_WEBHOOK_SECRET:
return f"Bad Request: {msg}", 400
payload_envelope = request.get_json()
msg = "no payload body received"
return f"Bad Request: {msg}", 400
if not isinstance(payload_envelope, dict) or not payload_envelope.get("event"):
msg = "invalid webhook payload format"
return f"Bad Request: {msg}", 400
event_type = payload_envelope["event"]
if event_type == "issue-exported":
if not payload_envelope.get("issue"):
msg = "webhook payload did not contain expected key: issue"
return f"Bad Request: {msg}", 400
_format_and_export_to_graphql_api(payload_envelope["project_name"], payload_envelope["issue"])
msg = f"endpoint not setup to handle {event_type} events yet"
return f"Bad Request: {msg}", 400
def _format_and_export_to_graphql_api(project_name, issue):
for element in issue["elements"]:
if element["element_type"] == "crop":
label_ids.add(element["crop_data"]["uuid"])
for label in element["frame_data"]["label_data"]:
label_ids.add(label["uuid"])
"projectId": project_name,
"labelIds": list(label_ids)
query=relabel_mutation_fragment,
if __name__ == "__main__":
PORT = int(os.getenv("PORT")) if os.getenv("PORT") else 8080
app.run(host="127.0.0.1", port=PORT, debug=True)