Package: fastpy-rest
# FastPy-Rest - A Simple Python Web Framework[FastPy-Rest]() is a lightweight Python web framework designed for educational purposes. It provides a simple and easy-to-understand structure to help you learn the fundamentals of building a web framework from scratch. With FastPy, you can explore various aspects of web development, including routing, request handling, response generation, and more.## Features* Minimalistic design for educational purposes.* Supports common HTTP methods (GET, POST, PUT, PATCH, DELETE).* Route handling and request processing.* Exception handling for HTTP errors.* Asynchronous request handling with asyncio.* Basic support for request parameters, headers, and cookies.## Getting StartedTo get started with FastPy, follow these steps:1. Install the package with `pip install fastpy-rest`2. Create your own routes and handlers3. Start the server```pythonfrom fastpy_rest import FastPyapp = FastPy()@app.get("/get")async def get(id: int):return idif __name__ == "__main__":app.run()```## Documentation### Defining RoutesThis project is highly inspired by FastAPI and it has the very same route defining syntax too, using decorators. The handler function may or may not be async, framework takes care of that by itself whether to await or call without await.```python@app.get("/path")async def get_handler():...```Similary other methods like `post`, `put`, `patch` and `delete` can be defined.### Various Input Parameters#### Query ParamsWe can take query params in our handlers by simply taking input parameters with type annotations except some annotations which we will discuss in body, headers, cookies and path.```python@app.get("/path")async def get_handler(id: int):return id```#### Path ParamsAny parameter in handler function which is mentioned in path as `/{path_param}` will be considered a path parameter.```python@app.get("/path/{id}")async def get_handler(id: int):return id```#### BodyAny parameter with annotation of subclass of `BaseModel` defined in `pydantic` is considered as a body json and body will be validated according to this body.```pythonfrom pydantic import BaseModelclass StudentIn(BaseModel):name: strclass: int@app.post("/create")async def create_student(student: StudentIn):return student```#### HeadersParameters with primitive type annotations along with default value of `Headers` will be extracted from headers.```pythonfrom fastpy_rest import Headers@app.get("/path")async def handler(authorization: str = Headers())):return {"authorization": authorization}```#### CookiesVery same as headers, instead of `Headers()`, `Cookies` is used as a default value.### Data Serialization and ValidationFor the purpose of serialization, deserialization and validation, Pydantic is used heavily. For body, we can denote it in a pydantic model and deserialize accordingly. In the framework, input annotations are mandatory and data is deserialized according to those annotations. Return annotation is not mandatory but we can also define that and enforce the validation of output data.```pythonfrom pydantic import BaseModelclass StudentIn(BaseModel):name: strclass: int@app.post("/create")async def create_student(student: StudentIn) -> StudentIn:return student```### Request ClassTo avoid the data validation, `Request` class can be used in annotations and in handler, request object will be provider with parsed path, query and other params.```pythonfrom fastpy_rest import Request@app.post("/create")async def create_student(request: Request):...```### Response ClassTo send custom status code for success (default=200), Response class object should be returned from handler. In this case, return type will not be validated.```pythonfrom fastpy_rest import Request, Response@app.post("/create")async def create_student(request: Request):return Response(201, "Student has been created.")```### Custom ExceptionsTo raise exceptions with appropriate code and messages, `HttpException` class has been defined.```pythonfrom fastpy_rest import Request, HttpException@app.post("/create")async def create_student(request: Request):raise HttpException(401, "Request not authenticated.")```## TestingYou can run the provided unit tests to verify the functionality of FastPy. To run all the tests, use the following command:```bashpython -m unittest discover -p 'test_*.py'```## ContributingFastPy is intended for educational purposes and may not be suitable for production use. If you'd like to contribute to the project or have any suggestions, please feel free to create an issue or submit a pull request.## LicenseThis project is licensed under the MIT License. See the [LICENSE](https://github.com/sineshashi/FastPy/blob/master/LICENSE) file for details.## AcknowledgmentsFastPy was created as an educational project to help individuals learn the basics of web framework development. We acknowledge the contributions of the open-source community and various educational resources that inspired this project.---Happy learning and building with FastPy! ðŸ˜Š