-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
111 lines (95 loc) · 3.48 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from fastapi import FastAPI, UploadFile, File, Request,WebSocket
from fastapi.middleware.cors import CORSMiddleware
from modules.pdfHrefExtractor import availableScreeningProfiles
from tempfile import TemporaryDirectory
from modules.orchestratorAPI import UIPathOrchestrator,readJson
from modules.idGenerator import IDGenerator
from modules.appScript import AppScriptAPI
import config as cfg
from datetime import datetime as d
from time import time
import json
app = FastAPI()
# origins = [
# "http://localhost:3000/*",
# "https://https://intellipick.web.app/*"
# ]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
secrets = readJson(jsonPath=cfg.paths["uiPathSecretsPath"])
orchestrator = UIPathOrchestrator(RobotId = secrets["RobotId"],
ReleaseKey = secrets["ReleaseKey"],
MachineId = secrets["MachineId"])
idGen = IDGenerator()
appScript = AppScriptAPI(secretsPath = cfg.paths["appScriptSecrets"])
@app.get("/")
async def main():
return {"message": "Hello Gals and Boiz"}
@app.post("/file_upload")
async def upload_file(resumePdf: UploadFile = File(...)):
data = resumePdf.file.read()
idx = idGen.generate()
with TemporaryDirectory() as d: #Adding the file into a temporary storage for re-reading purposes
tmpf = d + "pdf.pdf"
with open(tmpf,"wb") as f:
f.write(data)
hrefs = availableScreeningProfiles(str(tmpf),idStr = idx)
return hrefs
def completeRequestAndReturnResults(jobParams:dict,weights:dict=None):
if weights is None:
weights = cfg.defaultWeights
date = d.now()
dateString = f'{date.strftime("%Y-%m-%d %H:%M:%S")}__{time()}'
jobParams = {
'jsonArgument':{
'running':False,
'dateString': dateString,
'params':jobParams,
'weights': weights
}
}
if orchestrator.free:
response = orchestrator.startAJob(jobParams = jobParams,
folderId=secrets["folderId"],
runTimeType="Unattended")
else:
return {
"error": True,
"jobId": "NA",
"jobResponse": "NA",
"msg": "Only one Unattended Robot is allowed. Someone else is already using it :(. Please try after some time.",
"status_code":500
}
appScriptResponse = appScript.getSheetData(sheetName="immediateMerged")
appScriptResponse = appScriptResponse.json()
if appScriptResponse["error"]:
return {**appScriptResponse}
return {**response,'as_data':appScriptResponse}
@app.post("/trigger_ui_path")
async def trigger_ui_path(req:Request):
jobParams = await req.json()
return completeRequestAndReturnResults(jobParams = jobParams["allResponses"],
weights=jobParams["weights"])
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
print('Accepting client connection...')
await websocket.accept()
while True:
try:
# Wait for any message from the client
jobParams=await websocket.receive_json()
result = completeRequestAndReturnResults(jobParams = jobParams["allResponses"],
weights=jobParams["weights"])
# Send message to the client
await websocket.send_json(result)
# print(data)
except Exception as e:
print('error:', e)
break
print('Bye..')
#https://cloud.google.com/run/docs/quickstarts/build-and-deploy/python