Spaces:
Build error
Build error
Update app.py
Browse files12345671 unmodified line910111213141526 unmodified lines4243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788import 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/"# WebSocket URIws_uri = "wss://onlinewebsocketserver.onrender.com"26 unmodified linesprint(f"Failed to send or receive data: {e}")websocket = Nonedef extract_branch_info(stream_id, server_url=None, token=None):client = SpeckleClient(host=server_url)client.authenticate_with_token(token=token)branches = client.branch.list(stream_id, 100)branch_info = []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.itemsif commits:latest_commit = commits[0]branch_data["updated"] = latest_commit.createdAtbranch_data["commit_url"] = f"{server_url}streams/{stream_id}/commits/{latest_commit.id}"branch_data["commit_message"] = latest_commit.messagebranch_data["author"] = latest_commit.authorNamebranch_info.append(branch_data)return branch_info@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}")# 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")return "All streams updated successfully!"# Uncomment below if you want to use Gradio for a manual interface"""1234561 unmodified line891011121314151626 unmodified lines434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126import osimport jsonimport requestsfrom fastapi import Requestimport websocketsimport asyncio1 unmodified line# Speckle stream and authentication informationspeckle_token = os.getenv("SPECKLE_TOKEN")api_url = "https://speckle.xyz/graphql"stream_id = "1dab2d05eb"branch_name = "scenario_sycer"# WebSocket URIws_uri = "wss://onlinewebsocketserver.onrender.com"26 unmodified linesprint(f"Failed to send or receive data: {e}")websocket = Nonedef 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 Nonedef construct_commit_query(stream_id, branch_name):return f"""{{stream(id: "{stream_id}") {{branch(name: "{branch_name}") {{commits(limit: 1) {{items {{idmessagereferencedObject}}}}}}}}}}"""def fetch_referenced_object(speckleToken, apiUrl, stream_id, object_id):query = f"""{{stream(id: "{stream_id}") {{object(id: "{object_id}") {{iddata}}}}}}"""return send_graphql_query(speckleToken, apiUrl, query)@webhook_endpointasync 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 stringnested_data_str = object_data['data']['stream']['object']['data']['data']# Parse the JSON string to a dictionarynested_data = json.loads(nested_data_str)# Access the 'teamName' fieldteam_name = nested_data.get('teamName', 'N/A')print(f"Team Name: {team_name}")# Send the team name to the WebSocket serverawait send_data({"teamName": 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"""
app.py+71-33