Spaces:
Build error
Build error
Update app.py
Browse files12344561 unmodified line89101211121314151626 unmodified lines43444545464746474849505152535455565758495059606162636465666768697071727374755253545556575859606162636465666768767778798081828384858687888990919293757677787994959697989910010110210310410510610710810911011111211311411581828311611711811912012112285123124125126import osimport jsonimport requestsfrom specklepy.api.client import SpeckleClientfrom fastapi import Requestimport websocketsimport asyncio1 unmodified line
# Speckle stream and authentication informationspeckle_token = os.getenv("SPECKLE_TOKEN")speckle_server_url = "https://speckle.xyz/"api_url = "https://speckle.xyz/graphql"stream_id = "1dab2d05eb"branch_name = "scenario_sycer"
# WebSocket URIws_uri = "wss://onlinewebsocketserver.onrender.com"26 unmodified lines print(f"Failed to send or receive data: {e}") websocket = None
def extract_branch_info(stream_id, server_url=None, token=None): client = SpeckleClient(host=server_url) client.authenticate_with_token(token=token)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
branches = client.branch.list(stream_id, 100) branch_info = []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 }} }} }} }} }} """
for branch in branches: branch_data = { "Name": branch.name, "description": branch.description, "url": f"{server_url}/streams/{stream_id}/branches/{branch.name.replace('/', '%2F').replace('+', '%2B')}" }
commits = client.branch.get(stream_id, branch.name).commits.items if commits: latest_commit = commits[0] branch_data["updated"] = latest_commit.createdAt branch_data["commit_url"] = f"{server_url}streams/{stream_id}/commits/{latest_commit.id}" branch_data["commit_message"] = latest_commit.message branch_data["author"] = latest_commit.authorName branch_info.append(branch_data)
return branch_infodef 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_endpointasync def update_streams(request: Request): payload = await request.json() print(f"Received webhook payload: {payload}")
if 'payload' in payload and 'stream' in payload['payload']: stream_info = payload['payload']['stream'] if 'id' in stream_info: stream_id = stream_info['id'] print(f"Fetching data for stream: {stream_id}") 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}")
# Extract branch info and send it via WebSocket #branch_data = extract_branch_info(stream_id, server_url=speckle_server_url, token=speckle_token) await send_data("lalala::lalala") # Send the team name to the WebSocket server await send_data({"teamName": team_name}) else: print("Failed to retrieve the referenced object data.") else: print("Failed to retrieve commit data.")
return "All streams updated successfully!" return "Data sent successfully!"
# Uncomment below if you want to use Gradio for a manual interface"""app.py+71-33