-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (119 loc) · 4.91 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from fastapi import FastAPI, HTTPException, Request, status
from fastapi.responses import JSONResponse, FileResponse
from app import schemas, methods
from slowapi import Limiter
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
async def rate_limit_exceeded_handler(request: Request, exc: RateLimitExceeded):
detail = []
detail.append(
{'msg': f'Rate limit exceeded: {exc.detail} Try again in a while...'})
res = {'detail': detail}
return JSONResponse(
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
content=res
)
def get_application() -> FastAPI:
application = FastAPI(responses={429: {'model': schemas.ErrorRs}, 400: {'model': schemas.ErrorRs}}, exception_handlers={
429: rate_limit_exceeded_handler}, title="Encryptor", description="Encrypt plain text using simple encryption *i.e*: ***Caesar***, ***Morse***, etc.", version="1.0.3")
return application
app = get_application()
LIMIT = '5/minute'
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, rate_limit_exceeded_handler)
@app.get("/", include_in_schema=False)
async def health_check():
"""Used to check the status of the app
Returns:
JSON: Current status
"""
return {"Status": "running"}
@app.get("/favicon.ico", include_in_schema=False)
async def get_favicon():
return FileResponse('app/assets/favicon.ico')
@app.post("/caesar", response_model=schemas.EncryptRs)
@limiter.limit(LIMIT)
async def caesar(body: schemas.EncryptCaesarRq, request: Request):
"""Takes input & validates against schema then encrypts using Caesar encryption & returns result
Args:
body (schemas.EncryptCaesarRq): Input JSON request
Raises:
HTTPException: Error in execution
Returns:
JSON: Encrypted text
"""
result = methods.encrypt_caesar(body.plainText, body.language, body.shift)
if result['status'] != 200:
raise HTTPException(status_code=result['status'], detail=[
{'msg': result['msg']}])
else:
return {"cypherText": result['cypher_text']}
@app.post("/morse", response_model=schemas.EncryptRs)
@limiter.limit(LIMIT)
async def morse(body: schemas.EncryptRq, request: Request):
"""Takes input & validates against schema then encrypts using Morse encryption & returns result
Args:
body (schemas.EncryptRq): Input JSON request
Raises:
HTTPException: Error in execution
Returns:
JSON: Encrypted text
"""
result = methods.encrypt_morse(body.plainText, body.language)
if result['status'] != 200:
raise HTTPException(status_code=result['status'], detail=[
{'msg': result['msg']}])
else:
return {"cypherText": result['cypher_text']}
@app.post("/numeric", response_model=schemas.EncryptRs)
@limiter.limit(LIMIT)
async def numeric(body: schemas.EncryptRq, request: Request):
"""Takes input & validates against schema then encrypts using Numeric encryption & returns result
Args:
body (schemas.EncryptRq): Input JSON request
Raises:
HTTPException: Error in execution
Returns:
JSON: Encrypted text
"""
result = methods.encrypt_numeric(body.plainText, body.language)
if result['status'] != 200:
raise HTTPException(status_code=result['status'], detail=[
{'msg': result['msg']}])
else:
return {"cypherText": result['cypher_text']}
@app.post("/reversenumeric", response_model=schemas.EncryptRs)
@limiter.limit(LIMIT)
async def reverse_numeric(body: schemas.EncryptRq, request: Request):
"""Takes input & validates against schema then encrypts using Inverse Numeric encryption & returns result
Args:
body (schemas.EncryptRq): Input JSON request
Raises:
HTTPException: Error in execution
Returns:
JSON: Encrypted text
"""
result = methods.encrypt_reverse_numeric(body.plainText, body.language)
if result['status'] != 200:
raise HTTPException(status_code=result['status'], detail=[
{'msg': result['msg']}])
else:
return {"cypherText": result['cypher_text']}
@app.post("/natoalphabet", response_model=schemas.EncryptRs)
@limiter.limit(LIMIT)
async def nato_alphabet(body: schemas.EncryptNATORq, request: Request):
"""Takes input & validates against schema then encodes using NATO alphabet encoding & returns result
Args:
body (schemas.EncryptNATORq): Input JSON request
Raises:
HTTPException: Error in execution
Returns:
JSON: Encrypted text
"""
result = methods.encode_NATO(body.plainText)
if result['status'] != 200:
raise HTTPException(status_code=result['status'], detail=[
{'msg': result['msg']}])
else:
return {"cypherText": result['cypher_text']}