UUIDPusher / app.py
serJD's picture
Update app.py
31c7a45 verified
raw
history blame
3.93 kB
import os
import json
import requests
from fastapi import Request
import websockets
import asyncio
from huggingface_hub import webhook_endpoint
# Speckle stream and authentication information
speckle_token = os.getenv("SPECKLE_TOKEN")
api_url = "https://speckle.xyz/graphql"
stream_id = "1dab2d05eb"
branch_name = "scenario_sycer"
# WebSocket URI
ws_uri = "wss://onlinewebsocketserver.onrender.com"
websocket = None
# Connect to WebSocket
async def connect():
global websocket
try:
websocket = await websockets.connect(ws_uri)
print("Connected to the WebSocket server")
except Exception as e:
print(f"Connection failed: {e}")
async def send_data(data):
global websocket
if websocket is None or websocket.closed:
await connect()
if websocket is None or websocket.closed:
print("Failed to connect to WebSocket server.")
return
try:
await websocket.send(json.dumps(data))
print(f"Sent: {data}")
response = await websocket.recv()
response_data = json.loads(response)
print(f"Received: {response_data}")
except Exception as e:
print(f"Failed to send or receive data: {e}")
websocket = None
def send_graphql_query(speckleToken, apiUrl, query):
headers = {
"Authorization": f"Bearer {speckleToken}",
"Content-Type": "application/json",
"Accept": "application/json"
}
response = requests.post(apiUrl, headers=headers, json={"query": query})
if response.status_code == 200:
return response.json()
else:
print(f"HTTP Error: {response.status_code}, {response.text}")
return None
def construct_commit_query(stream_id, branch_name):
return f"""
{{
stream(id: "{stream_id}") {{
branch(name: "{branch_name}") {{
commits(limit: 1) {{
items {{
id
message
referencedObject
}}
}}
}}
}}
}}
"""
def fetch_referenced_object(speckleToken, apiUrl, stream_id, object_id):
query = f"""
{{
stream(id: "{stream_id}") {{
object(id: "{object_id}") {{
id
data
}}
}}
}}
"""
return send_graphql_query(speckleToken, apiUrl, query)
@webhook_endpoint
async def update_streams(request: Request):
payload = await request.json()
print(f"Received webhook payload: {payload}")
commit_query = construct_commit_query(stream_id, branch_name)
result = send_graphql_query(speckle_token, api_url, commit_query)
if result and 'data' in result:
commit_data = result['data']['stream']['branch']['commits']['items'][0]
referenced_object_id = commit_data['referencedObject']
print(f"Referenced Object ID: {referenced_object_id}")
object_data = fetch_referenced_object(speckle_token, api_url, stream_id, referenced_object_id)
if object_data:
print(json.dumps(object_data, indent=2))
# Extract the 'data' field, which is a JSON string
nested_data_str = object_data['data']['stream']['object']['data']['data']
# Parse the JSON string to a dictionary
nested_data = json.loads(nested_data_str)
# Access the 'teamName' field
team_name = nested_data.get('teamName', 'N/A')
print(f"Team Name: {team_name}")
# Send the team name to the WebSocket server
await send_data(team_name)
else:
print("Failed to retrieve the referenced object data.")
else:
print("Failed to retrieve commit data.")
return "Data sent successfully!"
# Uncomment below if you want to use Gradio for a manual interface
"""
import gradio as gr
iface = gr.Interface(
fn=update_streams,
inputs=gr.components.Button(value="Update Streams"),
outputs="text",
)
iface.launch()
"""