serJD commited on
Commit
c7ea683
·
verified ·
1 Parent(s): 6bbe964

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -33
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
- speckle_server_url = "https://speckle.xyz/"
 
 
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 extract_branch_info(stream_id, server_url=None, token=None):
46
- client = SpeckleClient(host=server_url)
47
- client.authenticate_with_token(token=token)
 
 
 
 
 
 
 
 
 
48
 
49
- branches = client.branch.list(stream_id, 100)
50
- branch_info = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- for branch in branches:
53
- branch_data = {
54
- "Name": branch.name,
55
- "description": branch.description,
56
- "url": f"{server_url}/streams/{stream_id}/branches/{branch.name.replace('/', '%2F').replace('+', '%2B')}"
57
- }
58
-
59
- commits = client.branch.get(stream_id, branch.name).commits.items
60
- if commits:
61
- latest_commit = commits[0]
62
- branch_data["updated"] = latest_commit.createdAt
63
- branch_data["commit_url"] = f"{server_url}streams/{stream_id}/commits/{latest_commit.id}"
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
- if 'payload' in payload and 'stream' in payload['payload']:
76
- stream_info = payload['payload']['stream']
77
- if 'id' in stream_info:
78
- stream_id = stream_info['id']
79
- print(f"Fetching data for stream: {stream_id}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- # Extract branch info and send it via WebSocket
82
- #branch_data = extract_branch_info(stream_id, server_url=speckle_server_url, token=speckle_token)
83
- await send_data("lalala::lalala")
 
 
 
84
 
85
- return "All streams updated successfully!"
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
  """