mirror of
https://github.com/shadoll/scatcher.git
synced 2025-12-20 03:26:59 +00:00
Refactor code structure and import order
This commit is contained in:
33
app.py
33
app.py
@@ -1,19 +1,21 @@
|
|||||||
from fastapi import FastAPI, Request, Response, status
|
|
||||||
from datetime import datetime
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from pydantic import BaseModel
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
from fastapi import FastAPI, Request, Response, status
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
HISTORY_LIMIT = 10
|
HISTORY_LIMIT = 10
|
||||||
HISTORY_STORAGE = "storage"
|
HISTORY_STORAGE = "storage"
|
||||||
|
|
||||||
|
|
||||||
class Status(str, Enum):
|
class Status(str, Enum):
|
||||||
ok = "ok"
|
ok = "ok"
|
||||||
error = "error"
|
error = "error"
|
||||||
|
|
||||||
|
|
||||||
class Methods(str, Enum):
|
class Methods(str, Enum):
|
||||||
GET = "GET"
|
GET = "GET"
|
||||||
POST = "POST"
|
POST = "POST"
|
||||||
@@ -23,10 +25,12 @@ class Methods(str, Enum):
|
|||||||
OPTIONS = "OPTIONS"
|
OPTIONS = "OPTIONS"
|
||||||
HEAD = "HEAD"
|
HEAD = "HEAD"
|
||||||
|
|
||||||
|
|
||||||
class Answer(BaseModel):
|
class Answer(BaseModel):
|
||||||
status: Status
|
status: Status
|
||||||
message: str
|
message: str
|
||||||
|
|
||||||
|
|
||||||
class RequestData(BaseModel):
|
class RequestData(BaseModel):
|
||||||
data: dict
|
data: dict
|
||||||
method: Methods
|
method: Methods
|
||||||
@@ -34,10 +38,8 @@ class RequestData(BaseModel):
|
|||||||
headers: dict
|
headers: dict
|
||||||
time: str
|
time: str
|
||||||
|
|
||||||
def store_last_request(
|
|
||||||
request_data,
|
def store_last_request(request_data, namespace="requests"):
|
||||||
namespace="requests"
|
|
||||||
):
|
|
||||||
|
|
||||||
# Check if the directory exists, if not, create it
|
# Check if the directory exists, if not, create it
|
||||||
if not os.path.exists(HISTORY_STORAGE):
|
if not os.path.exists(HISTORY_STORAGE):
|
||||||
@@ -60,7 +62,6 @@ def store_last_request(
|
|||||||
json.dump(data, f)
|
json.dump(data, f)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/", status_code=status.HTTP_200_OK)
|
@app.get("/", status_code=status.HTTP_200_OK)
|
||||||
@app.post("/", status_code=status.HTTP_200_OK)
|
@app.post("/", status_code=status.HTTP_200_OK)
|
||||||
@app.put("/", status_code=status.HTTP_200_OK)
|
@app.put("/", status_code=status.HTTP_200_OK)
|
||||||
@@ -75,7 +76,9 @@ def store_last_request(
|
|||||||
@app.patch("/{namespace}", status_code=status.HTTP_200_OK)
|
@app.patch("/{namespace}", status_code=status.HTTP_200_OK)
|
||||||
@app.options("/{namespace}", status_code=status.HTTP_200_OK)
|
@app.options("/{namespace}", status_code=status.HTTP_200_OK)
|
||||||
@app.head("/{namespace}", status_code=status.HTTP_200_OK)
|
@app.head("/{namespace}", status_code=status.HTTP_200_OK)
|
||||||
async def catch(request: Request, response: Response, namespace: str = "requests") -> Answer:
|
async def catch(
|
||||||
|
request: Request, response: Response, namespace: str = "requests"
|
||||||
|
) -> Answer:
|
||||||
try:
|
try:
|
||||||
json = await request.json()
|
json = await request.json()
|
||||||
except:
|
except:
|
||||||
@@ -94,6 +97,7 @@ async def catch(request: Request, response: Response, namespace: str = "requests
|
|||||||
|
|
||||||
return Answer(status="ok", message="Request catched.")
|
return Answer(status="ok", message="Request catched.")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/__help", status_code=status.HTTP_200_OK)
|
@app.get("/api/__help", status_code=status.HTTP_200_OK)
|
||||||
def help():
|
def help():
|
||||||
return {
|
return {
|
||||||
@@ -114,9 +118,10 @@ def help():
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/__last_request", status_code=status.HTTP_200_OK)
|
@app.get("/api/__last_request", status_code=status.HTTP_200_OK)
|
||||||
@app.get("/api/__last_request/{namespace}", status_code=status.HTTP_200_OK)
|
@app.get("/api/__last_request/{namespace}", status_code=status.HTTP_200_OK)
|
||||||
async def last_requests(namespace: str = "requests") -> Answer|RequestData:
|
async def last_requests(namespace: str = "requests") -> Answer | RequestData:
|
||||||
filename = f"{HISTORY_STORAGE}/{namespace}.json"
|
filename = f"{HISTORY_STORAGE}/{namespace}.json"
|
||||||
try:
|
try:
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r") as f:
|
||||||
@@ -134,7 +139,9 @@ async def last_requests(namespace: str = "requests") -> Answer|RequestData:
|
|||||||
@app.get("/api/__history", status_code=status.HTTP_200_OK)
|
@app.get("/api/__history", status_code=status.HTTP_200_OK)
|
||||||
@app.get("/api/__history/{namespace}/{id}", status_code=status.HTTP_200_OK)
|
@app.get("/api/__history/{namespace}/{id}", status_code=status.HTTP_200_OK)
|
||||||
@app.get("/api/__history/{namespace}", status_code=status.HTTP_200_OK)
|
@app.get("/api/__history/{namespace}", status_code=status.HTTP_200_OK)
|
||||||
async def history(id: int = None, namespace: str = "requests")->Answer|RequestData|list[RequestData]:
|
async def history(
|
||||||
|
id: int = 0, namespace: str = "requests"
|
||||||
|
) -> Answer | RequestData | list[RequestData]:
|
||||||
filename = f"{HISTORY_STORAGE}/{namespace}.json"
|
filename = f"{HISTORY_STORAGE}/{namespace}.json"
|
||||||
try:
|
try:
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r") as f:
|
||||||
@@ -148,15 +155,17 @@ async def history(id: int = None, namespace: str = "requests")->Answer|RequestDa
|
|||||||
return RequestData(**data[-id])
|
return RequestData(**data[-id])
|
||||||
return [RequestData(**d) for d in data]
|
return [RequestData(**d) for d in data]
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/__clear", status_code=status.HTTP_200_OK)
|
@app.get("/api/__clear", status_code=status.HTTP_200_OK)
|
||||||
@app.get("/api/__clear/{namespace}", status_code=status.HTTP_200_OK)
|
@app.get("/api/__clear/{namespace}", status_code=status.HTTP_200_OK)
|
||||||
async def clear_history(namespace: str = "requests")->Answer:
|
async def clear_history(namespace: str = "requests") -> Answer:
|
||||||
filename = f"{HISTORY_STORAGE}/{namespace}.json"
|
filename = f"{HISTORY_STORAGE}/{namespace}.json"
|
||||||
with open(filename, "w") as f:
|
with open(filename, "w") as f:
|
||||||
json.dump([], f)
|
json.dump([], f)
|
||||||
|
|
||||||
return Answer(status="ok", message="History cleared.")
|
return Answer(status="ok", message="History cleared.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user