-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
127 lines (110 loc) · 3.65 KB
/
app.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from fastapi import FastAPI, APIRouter, Depends, HTTPException, status, File, UploadFile, Form
from fastapi.responses import RedirectResponse
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
from caesar import caesar_cipher, rot13_cipher, atbash_cipher, crot13
from uuid import uuid4
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(
title="Caesar Cipher API",
description="A simple API for Caesar cipher",
version="1.0.0",
docs_url="/docs",
redoc_url='/redoc'
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
router = APIRouter()
class Caesar(BaseModel):
text: str
shift: int
option: str
class Default(BaseModel):
text: str
class CaesarFile(BaseModel):
shift: int = Form(...)
option: str = Form(...)
@router.get("/")
async def index():
return {
"message": "Welcome to the Caesar cipher API!",
"docs": "caesar.hardope.tech/docs"
}
@router.post("/rot13")
async def rot13_api(val: Default):
return {
"message": rot13_cipher(val.text)
}
@router.post("/atbash")
async def atbash_api(val: Default):
return {
"message": atbash_cipher(val.text)
}
@router.post("/caesar")
async def caesar_api(caesar: Caesar):
return {
"message": caesar_cipher(caesar.text, caesar.shift, caesar.option)
}
@router.post("/crot13")
async def crot13_api(val: Caesar):
return {
"message": crot13(val.text, val.shift, val.option)
}
@router.post("/caesar/file")
async def caesar_file_api(file: UploadFile = File(...), shift: int = Form(...), option: str = Form(...) ):
try:
contents = await file.read()
out = caesar_cipher(contents.decode("utf-8"), shift, option)
new_file = f"files/{uuid4()}.{file.filename.split('.')[1]}"
with open(new_file, "w") as f:
f.write(out)
except:
raise HTTPException(status_code=400, detail="Invalid file")
return {
"message": "File uploaded successfully!", "file": new_file
}
@router.post("/crot13/file")
async def crot13_file_api(file: UploadFile = File(...), shift: int = Form(...), option: str = Form(...) ):
try:
contents = await file.read()
out = crot13(contents.decode("utf-8"), shift, option)
new_file = f"files/{uuid4()}.{file.filename.split('.')[1]}"
with open(new_file, "w") as f:
f.write(out)
except:
raise HTTPException(status_code=400, detail="Invalid file")
return {
"message": "File uploaded successfully!", "file": new_file
}
@router.post("/rot13/file")
async def rot13_file_api(file: UploadFile = File(...)):
try:
contents = await file.read()
out = rot13_cipher(contents.decode("utf-8"))
new_file = f"files/{uuid4()}.{file.filename.split('.')[1]}"
with open(new_file, "w") as f:
f.write(out)
except:
raise HTTPException(status_code=400, detail="Invalid file")
return {
"message": "File uploaded successfully!", "file": new_file
}
@router.post("/atbash/file")
async def atbash_file_api(file: UploadFile = File(...)):
try:
contents = await file.read()
out = atbash_cipher(contents.decode("utf-8"))
new_file = f"files/{uuid4()}.{file.filename.split('.')[1]}"
with open(new_file, "w") as f:
f.write(out)
except:
raise HTTPException(status_code=400, detail="Invalid file")
return {
"message": "File uploaded successfully!", "file": new_file
}
app.include_router(router, prefix="")
app.mount("/files", StaticFiles(directory="files"), name="files")