-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
110 lines (85 loc) · 2.53 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
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
import uvicorn
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from utils import data, getSize, getRandomData
app = FastAPI(
title="BlackHolesAPI",
version="1.0.0",
contact={
"name": "Dogukan Urker",
"url": "https://dogukanurker.com",
"email": "[email protected]",
},
)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_methods=["*"],
allow_headers=["*"],
allow_origins=origins,
allow_credentials=True,
)
@app.get("/data")
def blackHoles():
return data()
@app.get("/data/{id}")
def getByID(id):
for i in data():
match str(i["id"]) == id:
case True:
return [i]
raise HTTPException(status_code=404, detail="Data not found.")
@app.get("/random")
def getRandom():
return [getRandomData()]
@app.get("/randomValue/{value}")
def getRandomValue(value: str):
match value in getRandomData():
case True:
return getRandomData()[value]
raise HTTPException(status_code=404, detail="Data not found.")
@app.get("/getValueByID/{value}/{id}")
def getValueByID(value, id: int):
id -= 1
match len(data()) > id:
case True:
match value in data()[id]:
case True:
return data()[id][value]
raise HTTPException(status_code=404, detail="Data not found.")
@app.get("/getRandomDatas/{dataCount}")
def getRandomDatas(dataCount: int):
match len(data()) > dataCount:
case True:
output = []
for _ in range(dataCount):
output.append(getRandomData())
return output
raise HTTPException(status_code=404, detail="Too much data is requested.")
@app.get("/getAll/{value}")
def getAll(value: str):
output = []
for i in data():
try:
output.append(i[value])
except:
raise HTTPException(status_code=404, detail="Data not found.")
return output
@app.get("/getAllWithID/{value}")
def getAllWithID(value: str):
output = []
for i in data():
try:
blackhole = []
blackhole.append(i["id"])
blackhole.append(i[value])
output.append(blackhole)
except:
raise HTTPException(status_code=404, detail="Data not found.")
return output
@app.get("/getDataSize")
def getDataSize():
return int(getSize() / 1024)
match __name__ == "__main__":
case True:
uvicorn.run("main:app", host="192.168.1.129", port=1818, workers=1, reload=True)