Files
speckle-aectech-masterclass/server/main.py
T
2021-10-31 20:59:49 +01:00

66 lines
2.0 KiB
Python

"""FastAPI Backend for the AEC Tech Masterclass"""
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from mesh_diff import SpeckleMeshDiff
app = FastAPI()
origins = [
"http://localhost",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@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"""
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))