add routers
Browse files- main.py +4 -51
- game_util.py → routers/game_util.py +5 -2
- routers/root.py +9 -0
- routers/snake.py +26 -0
- routers/watermelon.py +26 -0
main.py
CHANGED
|
@@ -1,54 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI, Body
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def root():
|
| 10 |
-
return {"message": "hi!"}
|
| 11 |
-
|
| 12 |
-
##############################
|
| 13 |
-
GAME = "sname"
|
| 14 |
-
##############################
|
| 15 |
-
@app.post(f"/games/{GAME}/nlp/c", tags=[GAME])
|
| 16 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 17 |
-
return game_util.create(GAME, code, command)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
@app.post(f"/games/{GAME}/nlp/r", tags=[GAME])
|
| 21 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 22 |
-
return game_util.read(GAME, code, command)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
@app.post(f"/games/{GAME}/nlp/u", tags=[GAME])
|
| 26 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 27 |
-
return game_util.update(GAME, code, command)
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
@app.post(f"/games/{GAME}/nlp/d", tags=[GAME])
|
| 31 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 32 |
-
return game_util.delete(GAME, code, command)
|
| 33 |
-
|
| 34 |
-
##############################
|
| 35 |
-
GAME = "angrybird"
|
| 36 |
-
##############################
|
| 37 |
-
@app.post(f"/games/{GAME}/nlp/c", tags=[GAME])
|
| 38 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 39 |
-
return game_util.create(GAME, code, command)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
@app.post(f"/games/{GAME}/nlp/r", tags=[GAME])
|
| 43 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 44 |
-
return game_util.read(GAME, code, command)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
@app.post(f"/games/{GAME}/nlp/u", tags=[GAME])
|
| 48 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 49 |
-
return game_util.update(GAME, code, command)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
@app.post(f"/games/{GAME}/nlp/d", tags=[GAME])
|
| 53 |
-
def main_snake(code: str=Body(), command: str=Body()):
|
| 54 |
-
return game_util.delete(GAME, code, command)
|
|
|
|
| 1 |
from fastapi import FastAPI, Body
|
| 2 |
+
from routers import root, snake, watermelon
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
+
app.include_router(root.router)
|
| 6 |
+
app.include_router(snake.router)
|
| 7 |
+
app.include_router(watermelon.router)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
game_util.py → routers/game_util.py
RENAMED
|
@@ -16,14 +16,17 @@ def read(game: str, code: str, command: str):
|
|
| 16 |
raise HTTPException(status.HTTP_511_NETWORK_AUTHENTICATION_REQUIRED)
|
| 17 |
|
| 18 |
url = f'https://{os.getenv("hfs_space")}.hf.space/games/{game}'
|
| 19 |
-
|
| 20 |
myobj = {
|
| 21 |
'command': command,
|
| 22 |
}
|
| 23 |
headers = {'Authorization': f'Bearer {os.getenv("hfs_token")}'}
|
| 24 |
|
| 25 |
x = requests.post(url, json=myobj, headers=headers)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
def update(game: str, code: str, command: str):
|
|
|
|
| 16 |
raise HTTPException(status.HTTP_511_NETWORK_AUTHENTICATION_REQUIRED)
|
| 17 |
|
| 18 |
url = f'https://{os.getenv("hfs_space")}.hf.space/games/{game}'
|
| 19 |
+
print(url)
|
| 20 |
myobj = {
|
| 21 |
'command': command,
|
| 22 |
}
|
| 23 |
headers = {'Authorization': f'Bearer {os.getenv("hfs_token")}'}
|
| 24 |
|
| 25 |
x = requests.post(url, json=myobj, headers=headers)
|
| 26 |
+
if x.status_code == status.HTTP_200_OK:
|
| 27 |
+
return json.loads(x.text)
|
| 28 |
+
else:
|
| 29 |
+
raise HTTPException(status_code=x.status_code, detail=json.loads(x.text)['detail'])
|
| 30 |
|
| 31 |
|
| 32 |
def update(game: str, code: str, command: str):
|
routers/root.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
router = APIRouter()
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@router.get("/", tags=["home"])
|
| 8 |
+
def root():
|
| 9 |
+
return {"message": "hi!"}
|
routers/snake.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Body
|
| 2 |
+
from . import game_util
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
GAME = "snake"
|
| 6 |
+
router = APIRouter(prefix=f"/games/{GAME}/nlp", tags=[GAME])
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@router.post("/c", tags=[GAME])
|
| 10 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 11 |
+
return game_util.create(GAME, code, command)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@router.post("/r", tags=[GAME])
|
| 15 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 16 |
+
return game_util.read(GAME, code, command)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@router.post("/u")
|
| 20 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 21 |
+
return game_util.update(GAME, code, command)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@router.post("/d")
|
| 25 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 26 |
+
return game_util.delete(GAME, code, command)
|
routers/watermelon.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Body
|
| 2 |
+
from . import game_util
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
GAME = "watermelon"
|
| 6 |
+
router = APIRouter(prefix=f"/games/{GAME}/nlp", tags=[GAME])
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@router.post("/c", tags=[GAME])
|
| 10 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 11 |
+
return game_util.create(GAME, code, command)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@router.post("/r", tags=[GAME])
|
| 15 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 16 |
+
return game_util.read(GAME, code, command)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@router.post("/u")
|
| 20 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 21 |
+
return game_util.update(GAME, code, command)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@router.post("/d")
|
| 25 |
+
def main_snake(code: str=Body(), command: str=Body()):
|
| 26 |
+
return game_util.delete(GAME, code, command)
|