Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 9d6810a

Browse files
committed
First Commit
0 parents  commit 9d6810a

30 files changed

+853
-0
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
APP_HOST = '0.0.0.0'
2+
APP_PORT = 8000

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* linguist-detectable=false
2+
*.yml linguist-detectable=true

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__pycache__/
2+
*.pyc
3+
*.pyo
4+
*.env

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Natthasath Saksupanara
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 🎉 Template Python FastAPI Exceptions
2+
3+
Exceptions are events that occur during program execution that disrupt the normal flow of code. They can be caught and handled, allowing for more robust and error-resistant programs.
4+
5+
![version](https://img.shields.io/badge/version-1.0-blue)
6+
![rating](https://img.shields.io/badge/rating-★★★★★-yellow)
7+
![uptime](https://img.shields.io/badge/uptime-100%25-brightgreen)
8+
9+
### 🏆 Run
10+
11+
- [http://localhost:8000/docs](http://localhost:8000/docs)
12+
- [http://localhost:8000/subapi/docs](http://localhost:8000/subapi/docs)
13+
14+
```shell
15+
docker-compose up -d
16+
```

app/api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from fastapi import FastAPI
2+
from fastapi.middleware.cors import CORSMiddleware
3+
from app.routers import template, client, server
4+
from app.tag import SubTags, Tags
5+
6+
app = FastAPI(
7+
title="FastAPI",
8+
description="Web API helps you do awesome stuff. 🚀",
9+
version="0.0.1",
10+
terms_of_service="http://example.com/terms/",
11+
contact={
12+
"name": "Information and Digital Technology Center (IDT)",
13+
"url": "https://codeinsane.wordpress.com/",
14+
"email": "[email protected]",
15+
},
16+
license_info={
17+
"name": "Apache 2.0",
18+
"url": "https://www.apache.org/licenses/LICENSE-2.0.html",
19+
},
20+
openapi_url="/api/v1/openapi.json",
21+
docs_url="/docs",
22+
openapi_tags=Tags(),
23+
swagger_ui_parameters={"defaultModelsExpandDepth": -1}
24+
)
25+
26+
origins = ["*"]
27+
app.add_middleware(
28+
CORSMiddleware,
29+
allow_origins=origins,
30+
allow_credentials=True,
31+
allow_methods=["*"],
32+
allow_headers=["*"],
33+
)
34+
35+
app.include_router(client.router)
36+
app.include_router(server.router)
37+
#
38+
#
39+
40+
subapi = FastAPI(openapi_tags=SubTags(), swagger_ui_parameters={"defaultModelsExpandDepth": -1})
41+
42+
subapi.add_middleware(
43+
CORSMiddleware,
44+
allow_origins=origins,
45+
allow_credentials=True,
46+
allow_methods=["*"],
47+
allow_headers=["*"],
48+
)
49+
50+
subapi.include_router(template.router)
51+
#
52+
#
53+
#
54+
55+
app.mount("/subapi", subapi)

app/doc/.gitkeep

Whitespace-only changes.

app/font/.gitkeep

Whitespace-only changes.

app/handler/.gitkeep

Whitespace-only changes.

app/handler/client_exceptions.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
from typing import Any
2+
from fastapi.exception_handlers import HTTPException
3+
4+
class BadRequestException(HTTPException):
5+
def __init__(self, detail: Any = None) -> None:
6+
super().__init__(status_code=400, detail=detail)
7+
8+
class UnauthorizedException(HTTPException):
9+
def __init__(self, detail: Any = None) -> None:
10+
super().__init__(status_code=401, detail=detail)
11+
12+
class PaymentRequiredException(HTTPException):
13+
def __init__(self, detail: Any = None) -> None:
14+
super().__init__(status_code=402, detail=detail)
15+
16+
class ForbiddenException(HTTPException):
17+
def __init__(self, detail: Any = None) -> None:
18+
super().__init__(status_code=403, detail=detail)
19+
20+
class NotFoundException(HTTPException):
21+
def __init__(self, detail: Any = None) -> None:
22+
super().__init__(status_code=404, detail=detail)
23+
24+
class MethodNotAllowedException(HTTPException):
25+
def __init__(self, detail: Any = None) -> None:
26+
super().__init__(status_code=405, detail=detail)
27+
28+
class NotAcceptableException(HTTPException):
29+
def __init__(self, detail: Any = None) -> None:
30+
super().__init__(status_code=406, detail=detail)
31+
32+
class ProxyAuthenticationRequiredException(HTTPException):
33+
def __init__(self, detail: Any = None) -> None:
34+
super().__init__(status_code=407, detail=detail)
35+
36+
class RequestTimeoutException(HTTPException):
37+
def __init__(self, detail: Any = None) -> None:
38+
super().__init__(status_code=408, detail=detail)
39+
40+
class ConflictException(HTTPException):
41+
def __init__(self, detail: Any = None) -> None:
42+
super().__init__(status_code=409, detail=detail)
43+
44+
class GoneException(HTTPException):
45+
def __init__(self, detail: Any = None) -> None:
46+
super().__init__(status_code=410, detail=detail)
47+
48+
class LengthRequiredException(HTTPException):
49+
def __init__(self, detail: Any = None) -> None:
50+
super().__init__(status_code=411, detail=detail)
51+
52+
class PreconditionFailedException(HTTPException):
53+
def __init__(self, detail: Any = None) -> None:
54+
super().__init__(status_code=412, detail=detail)
55+
56+
class PayloadTooLargeException(HTTPException):
57+
def __init__(self, detail: Any = None) -> None:
58+
super().__init__(status_code=413, detail=detail)
59+
60+
class URITooLongException(HTTPException):
61+
def __init__(self, detail: Any = None) -> None:
62+
super().__init__(status_code=414, detail=detail)
63+
64+
class UnsupportedMediaTypeException(HTTPException):
65+
def __init__(self, detail: Any = None) -> None:
66+
super().__init__(status_code=415, detail=detail)
67+
68+
class RangeNotSatisfiableException(HTTPException):
69+
def __init__(self, detail: Any = None) -> None:
70+
super().__init__(status_code=416, detail=detail)
71+
72+
class ExpectationFailedException(HTTPException):
73+
def __init__(self, detail: Any = None) -> None:
74+
super().__init__(status_code=417, detail=detail)
75+
76+
class ImATeapotException(HTTPException):
77+
def __init__(self, detail: Any = None) -> None:
78+
super().__init__(status_code=418, detail=detail)
79+
80+
class MisdirectedRequestException(HTTPException):
81+
def __init__(self, detail: Any = None) -> None:
82+
super().__init__(status_code=421, detail=detail)
83+
84+
class UnprocessableEntityException(HTTPException):
85+
def __init__(self, detail: Any = None) -> None:
86+
super().__init__(status_code=422, detail=detail)
87+
88+
class LockedException(HTTPException):
89+
def __init__(self, detail: Any = None) -> None:
90+
super().__init__(status_code=423, detail=detail)
91+
92+
class FailedDependencyException(HTTPException):
93+
def __init__(self, detail: Any = None) -> None:
94+
super().__init__(status_code=424, detail=detail)
95+
96+
class TooEarlyException(HTTPException):
97+
def __init__(self, detail: Any = None) -> None:
98+
super().__init__(status_code=425, detail=detail)
99+
100+
class UpgradeRequiredException(HTTPException):
101+
def __init__(self, detail: Any = None) -> None:
102+
super().__init__(status_code=426, detail=detail)
103+
104+
class PreconditionRequiredException(HTTPException):
105+
def __init__(self, detail: Any = None) -> None:
106+
super().__init__(status_code=428, detail=detail)
107+
108+
class TooManyRequestsException(HTTPException):
109+
def __init__(self, detail: Any = None) -> None:
110+
super().__init__(status_code=429, detail=detail)
111+
112+
class RequestHeaderFieldsTooLargeException(HTTPException):
113+
def __init__(self, detail: Any = None) -> None:
114+
super().__init__(status_code=431, detail=detail)
115+
116+
class NoResponseException(HTTPException):
117+
def __init__(self, detail: Any = None) -> None:
118+
super().__init__(status_code=444, detail=detail)
119+
120+
class UnavailableForLegalReasonsException(HTTPException):
121+
def __init__(self, detail: Any = None) -> None:
122+
super().__init__(status_code=451, detail=detail)

app/handler/server_exceptions.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import Any
2+
from fastapi.exception_handlers import HTTPException
3+
4+
class InternalServerErrorException(HTTPException):
5+
def __init__(self, detail: Any = None) -> None:
6+
super().__init__(status_code=500, detail=detail)
7+
8+
class NotImplementedException(HTTPException):
9+
def __init__(self, detail: Any = None) -> None:
10+
super().__init__(status_code=501, detail=detail)
11+
12+
class BadGatewayException(HTTPException):
13+
def __init__(self, detail: Any = None) -> None:
14+
super().__init__(status_code=502, detail=detail)
15+
16+
class ServiceUnavailableException(HTTPException):
17+
def __init__(self, detail: Any = None) -> None:
18+
super().__init__(status_code=503, detail=detail)
19+
20+
class GatewayTimeoutException(HTTPException):
21+
def __init__(self, detail: Any = None) -> None:
22+
super().__init__(status_code=504, detail=detail)
23+
24+
class HTTPVersionNotSupportedException(HTTPException):
25+
def __init__(self, detail: Any = None) -> None:
26+
super().__init__(status_code=505, detail=detail)
27+
28+
class VariantAlsoNegotiatesException(HTTPException):
29+
def __init__(self, detail: Any = None) -> None:
30+
super().__init__(status_code=506, detail=detail)
31+
32+
class InsufficientStorageException(HTTPException):
33+
def __init__(self, detail: Any = None) -> None:
34+
super().__init__(status_code=507, detail=detail)
35+
36+
class LoopDetectedException(HTTPException):
37+
def __init__(self, detail: Any = None) -> None:
38+
super().__init__(status_code=508, detail=detail)
39+
40+
class NotExtendedException(HTTPException):
41+
def __init__(self, detail: Any = None) -> None:
42+
super().__init__(status_code=510, detail=detail)
43+
44+
class NetworkAuthenticationRequiredException(HTTPException):
45+
def __init__(self, detail: Any = None) -> None:
46+
super().__init__(status_code=511, detail=detail)

app/img/.gitkeep

Whitespace-only changes.

app/json/.gitkeep

Whitespace-only changes.

app/log/access.log

Whitespace-only changes.

app/models/model_template.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from fastapi import Form
2+
from pydantic import BaseModel, Field, EmailStr, SecretStr
3+
from typing import List, Union
4+
import inspect
5+
6+
def form_body(cls):
7+
cls.__signature__ = cls.__signature__.replace(
8+
parameters=[
9+
arg.replace(default=Form(default = arg.default) if arg.default is not inspect._empty else Form(...))
10+
for arg in cls.__signature__.parameters.values()
11+
]
12+
)
13+
return cls
14+
15+
@form_body
16+
class TemplateSchema(BaseModel):
17+
id: int = Field(default=None)
18+
19+
class Config:
20+
schema_extra = {
21+
"example": {
22+
"id": "Incremental Number"
23+
}
24+
}

0 commit comments

Comments
 (0)