Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
-
from specklepy.api.client import SpeckleClient
|
| 5 |
from fastapi import Request
|
| 6 |
import websockets
|
| 7 |
import asyncio
|
|
@@ -9,7 +8,9 @@ from huggingface_hub import webhook_endpoint
|
|
| 9 |
|
| 10 |
# Speckle stream and authentication information
|
| 11 |
speckle_token = os.getenv("SPECKLE_TOKEN")
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# WebSocket URI
|
| 15 |
ws_uri = "wss://onlinewebsocketserver.onrender.com"
|
|
@@ -42,47 +43,84 @@ async def send_data(data):
|
|
| 42 |
print(f"Failed to send or receive data: {e}")
|
| 43 |
websocket = None
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
branch_data["commit_message"] = latest_commit.message
|
| 65 |
-
branch_data["author"] = latest_commit.authorName
|
| 66 |
-
branch_info.append(branch_data)
|
| 67 |
-
|
| 68 |
-
return branch_info
|
| 69 |
|
| 70 |
@webhook_endpoint
|
| 71 |
async def update_streams(request: Request):
|
| 72 |
payload = await request.json()
|
| 73 |
print(f"Received webhook payload: {payload}")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
return "
|
| 86 |
|
| 87 |
# Uncomment below if you want to use Gradio for a manual interface
|
| 88 |
"""
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
|
|
|
| 4 |
from fastapi import Request
|
| 5 |
import websockets
|
| 6 |
import asyncio
|
|
|
|
| 8 |
|
| 9 |
# Speckle stream and authentication information
|
| 10 |
speckle_token = os.getenv("SPECKLE_TOKEN")
|
| 11 |
+
api_url = "https://speckle.xyz/graphql"
|
| 12 |
+
stream_id = "1dab2d05eb"
|
| 13 |
+
branch_name = "scenario_sycer"
|
| 14 |
|
| 15 |
# WebSocket URI
|
| 16 |
ws_uri = "wss://onlinewebsocketserver.onrender.com"
|
|
|
|
| 43 |
print(f"Failed to send or receive data: {e}")
|
| 44 |
websocket = None
|
| 45 |
|
| 46 |
+
def send_graphql_query(speckleToken, apiUrl, query):
|
| 47 |
+
headers = {
|
| 48 |
+
"Authorization": f"Bearer {speckleToken}",
|
| 49 |
+
"Content-Type": "application/json",
|
| 50 |
+
"Accept": "application/json"
|
| 51 |
+
}
|
| 52 |
+
response = requests.post(apiUrl, headers=headers, json={"query": query})
|
| 53 |
+
if response.status_code == 200:
|
| 54 |
+
return response.json()
|
| 55 |
+
else:
|
| 56 |
+
print(f"HTTP Error: {response.status_code}, {response.text}")
|
| 57 |
+
return None
|
| 58 |
|
| 59 |
+
def construct_commit_query(stream_id, branch_name):
|
| 60 |
+
return f"""
|
| 61 |
+
{{
|
| 62 |
+
stream(id: "{stream_id}") {{
|
| 63 |
+
branch(name: "{branch_name}") {{
|
| 64 |
+
commits(limit: 1) {{
|
| 65 |
+
items {{
|
| 66 |
+
id
|
| 67 |
+
message
|
| 68 |
+
referencedObject
|
| 69 |
+
}}
|
| 70 |
+
}}
|
| 71 |
+
}}
|
| 72 |
+
}}
|
| 73 |
+
}}
|
| 74 |
+
"""
|
| 75 |
|
| 76 |
+
def fetch_referenced_object(speckleToken, apiUrl, stream_id, object_id):
|
| 77 |
+
query = f"""
|
| 78 |
+
{{
|
| 79 |
+
stream(id: "{stream_id}") {{
|
| 80 |
+
object(id: "{object_id}") {{
|
| 81 |
+
id
|
| 82 |
+
data
|
| 83 |
+
}}
|
| 84 |
+
}}
|
| 85 |
+
}}
|
| 86 |
+
"""
|
| 87 |
+
return send_graphql_query(speckleToken, apiUrl, query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
@webhook_endpoint
|
| 90 |
async def update_streams(request: Request):
|
| 91 |
payload = await request.json()
|
| 92 |
print(f"Received webhook payload: {payload}")
|
| 93 |
|
| 94 |
+
commit_query = construct_commit_query(stream_id, branch_name)
|
| 95 |
+
result = send_graphql_query(speckle_token, api_url, commit_query)
|
| 96 |
+
|
| 97 |
+
if result and 'data' in result:
|
| 98 |
+
commit_data = result['data']['stream']['branch']['commits']['items'][0]
|
| 99 |
+
referenced_object_id = commit_data['referencedObject']
|
| 100 |
+
print(f"Referenced Object ID: {referenced_object_id}")
|
| 101 |
+
|
| 102 |
+
object_data = fetch_referenced_object(speckle_token, api_url, stream_id, referenced_object_id)
|
| 103 |
+
if object_data:
|
| 104 |
+
print(json.dumps(object_data, indent=2))
|
| 105 |
+
|
| 106 |
+
# Extract the 'data' field, which is a JSON string
|
| 107 |
+
nested_data_str = object_data['data']['stream']['object']['data']['data']
|
| 108 |
+
|
| 109 |
+
# Parse the JSON string to a dictionary
|
| 110 |
+
nested_data = json.loads(nested_data_str)
|
| 111 |
+
|
| 112 |
+
# Access the 'teamName' field
|
| 113 |
+
team_name = nested_data.get('teamName', 'N/A')
|
| 114 |
+
print(f"Team Name: {team_name}")
|
| 115 |
|
| 116 |
+
# Send the team name to the WebSocket server
|
| 117 |
+
await send_data({"teamName": team_name})
|
| 118 |
+
else:
|
| 119 |
+
print("Failed to retrieve the referenced object data.")
|
| 120 |
+
else:
|
| 121 |
+
print("Failed to retrieve commit data.")
|
| 122 |
|
| 123 |
+
return "Data sent successfully!"
|
| 124 |
|
| 125 |
# Uncomment below if you want to use Gradio for a manual interface
|
| 126 |
"""
|