From c03e9d8262a67e28620e204eef91d797632e7b50 Mon Sep 17 00:00:00 2001 From: Alan Rynne Date: Sun, 31 Oct 2021 20:59:49 +0100 Subject: [PATCH] Updated backend to return commit instead of url --- server/main.py | 47 ++++++++++++++++++++++++++++++++++++++++----- server/mesh_diff.py | 32 ++++++++++++++++-------------- 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/server/main.py b/server/main.py index 30b2e3c..f8f9d19 100644 --- a/server/main.py +++ b/server/main.py @@ -1,8 +1,8 @@ """FastAPI Backend for the AEC Tech Masterclass""" -from fastapi import FastAPI, Request +from fastapi import FastAPI, Request, HTTPException from fastapi.middleware.cors import CORSMiddleware -import mesh_diff as md +from mesh_diff import SpeckleMeshDiff app = FastAPI() @@ -23,6 +23,43 @@ app.add_middleware( @app.get("/diff/{stream_id}/{commit_current}/{commit_previous}") def get_diff(stream_id: str, commit_current: str, commit_previous: str, request: Request): """Diffing endpoint""" - token = request.headers.get("Authorisation").split(" ")[1] - md.authenticate(token) - return md.compare_meshes(stream_id, commit_current, commit_previous) + auth_header = request.headers.get("Authorisation") + + if auth_header is None: + raise HTTPException(405, "No token provided") + + token = auth_header.split(" ")[1] + + try: + mesh_differ = SpeckleMeshDiff(token, "https://latest.speckle.dev") + diff_commit = mesh_differ.process_diff( + stream_id, commit_current, commit_previous) + except Exception as e: + raise HTTPException(500, str(e)) + return {"commit": diff_commit} + + +@app.get("/diff_check/{stream_id}/{commit_current}/{commit_previous}") +def get_diff_check(stream_id: str, commit_current: str, commit_previous: str, request: Request): + """Diffing endpoint""" + auth_header = request.headers.get("Authorisation") + + if auth_header is None: + raise HTTPException(405, "No token provided") + + token = auth_header.split(" ")[1] + + try: + mesh_differ = SpeckleMeshDiff(token, "https://latest.speckle.dev") + mesh_differ.stream_id = stream_id + mesh_differ.commit_current = commit_current + mesh_differ.commit_prev = commit_previous + existing_diff_commit = mesh_differ.check_existing_commits() + if existing_diff_commit is not None: + + return {"exists": True, "commit": existing_diff_commit} + else: + return {"exists": False, "commit": None} + + except Exception as e: + raise HTTPException(500, str(e)) diff --git a/server/mesh_diff.py b/server/mesh_diff.py index a54a1db..12977f1 100644 --- a/server/mesh_diff.py +++ b/server/mesh_diff.py @@ -1,6 +1,6 @@ """SpeckleMeshDiff for AEC Tech Masterclass""" -from typing import Any, List +from typing import List import math from specklepy.api import operations from specklepy.api.client import SpeckleClient @@ -42,16 +42,6 @@ class SpeckleMeshDiff: self.commit_current = commit_current self.commit_prev = commit_previous - # see if existing diff commit already exists - # query for latest x commits in diff branch - # read commit message & parse - # return url if found - existing_diff_commit = None - if existing_diff_commit is not None: - url = f"{self.host}/streams/{self.stream_id}/commits/{existing_diff_commit}" - print(f"Returning existing diff: {url}") - return url - print("Did not find existing diff, fetching commits now....") # get meshes from commits previous_commit = self.receive_data( @@ -74,8 +64,9 @@ class SpeckleMeshDiff: self.commit_current + "-" + self.commit_prev) print("Successfully sent data to Speckle") - diff_url = f"{self.host}/streams/{self.stream_id}/commits/{diff_commit_id}" - return diff_url + return self.client.commit.get(self.stream_id, diff_commit_id) + # diff_url = f"{self.host}/streams/{self.stream_id}/commits/{diff_commit_id}" + # return diff_url def check_existing_commits(self) -> bool or None: """Checks if a specific diff commit already exists in the diff_branch""" @@ -84,7 +75,7 @@ class SpeckleMeshDiff: for commit in branch_commits.commits.items: if commit.message == f"{self.commit_current}-{self.commit_prev}": - return commit.id + return commit return None @@ -283,6 +274,19 @@ class SpeckleMeshDiff: (p.displayMesh, p.id, p.applicationId, p)) elif isinstance(p, Mesh): meshes.append((p, p.id, p.applicationId)) + elif isinstance(p, list): + for subp in p: + if isinstance(subp, Brep): + if not hasattr(subp, "displayMesh"): + break + meshes.append( + (subp.displayMesh, subp.id, subp.applicationId, subp)) + elif isinstance(subp, Mesh): + meshes.append( + (subp, subp.id, subp.applicationId)) + elif isinstance(subp, Base): + meshes += SpeckleMeshDiff.get_all_meshes(subp) + return meshes @staticmethod