-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (70 loc) · 2.35 KB
/
main.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
import codecs
from fastapi import FastAPI, Request
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from rdflib import Graph
from scripts.querysplitting import QuerySplitting
from scripts.query import Query
app = FastAPI()
g = Graph()
g.parse('data/car-beuty.xml', format='xml')
app.add_middleware(
CORSMiddleware,
allow_credentials=True,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"]
)
app.mount("/scripts", StaticFiles(directory="scripts"), name="scripts")
@app.get("/")
def load_main_page():
f=codecs.open("index.html", 'r')
html_content = f.read()
return HTMLResponse(content=html_content, status_code=200)
@app.get("/extra-feature")
def load_extra_feature():
f=codecs.open("extra-feature.html", 'r')
html_content = f.read()
return HTMLResponse(content=html_content, status_code=200)
@app.post('/sparql')
async def read_items(req: Request):
req_json = await req.json()
row = g.query(req_json.get('query'))
result_query = QuerySplitting.splitting(req_json.get('query'))
final_result = list(row)
final_result.insert(0,list(result_query))
json_compatible_item_data = jsonable_encoder(final_result)
return JSONResponse(content=json_compatible_item_data)
@app.get('/subject')
def get_subjects():
subjects = list(g.subjects())
result = set()
for x in subjects:
result.add(x)
json_compatible_item_data = jsonable_encoder(result)
return JSONResponse(content=json_compatible_item_data)
@app.get('/predicate')
def get_subjects():
predicate = list(g.predicates())
result = set()
for x in predicate:
result.add(x)
json_compatible_item_data = jsonable_encoder(result)
return JSONResponse(content=json_compatible_item_data)
@app.post('/query')
async def query(req: Request):
req_json = await req.json()
print(req_json)
if (req_json['subject'] != ''):
sub = "<"+req_json['subject']+">"
else:
sub = None
if (req_json['predicate'] != ''):
pred = "<"+req_json['predicate']+">"
else:
pred = None
final_result = Query.query(g,sub,pred, None)
json_compatible_item_data = jsonable_encoder(final_result)
return JSONResponse(content=json_compatible_item_data)