diff --git a/backend/api-docs.txt b/backend/api-docs.txt new file mode 100644 index 0000000..aba2f79 --- /dev/null +++ b/backend/api-docs.txt @@ -0,0 +1,47 @@ +WARNING:- + This api is not yet capable of handling any exceptions that may arise when opened for the first time + + +_____________________ +Login:- + URL:- /login + + Method:- POST + + Data Type:- Form Data + + Data content:- { + user_name: "", + password: "" + } + + Response:- + 200 OK:- + Data:- No + Note:- Assume login completed. + + 400 Bad Request:- + Content Type:- text/plain + Note:- user_name or password not given a Form Data. + Data:- What is missing will be mentioned. + + 403 Forbidden:- + Content Type:- text/plain + Note:- user_name or password is wrong, can display the response data to user. + Data:- What is wrong will be mentioned + + Example:- + formData = new FormData(); + formData.append('name', 'John'); + formData.append('password', 'John123'); + fetch( + "/login", + { + body: formData, + method: "post" + } + ); + + This will give a response status 200 with no data if login is done. + This will return a 403 if the user gave wrong name or password. +_____________________ diff --git a/backend/rest_api.py b/backend/rest_api.py index ff0919d..b8764b5 100644 --- a/backend/rest_api.py +++ b/backend/rest_api.py @@ -1,5 +1,8 @@ -from flask import Flask, send_file, abort, Response from os import path +from flask import Flask, request, send_file, abort, Response +from werkzeug.datastructures import ImmutableMultiDict + +from data_handler import DataHandler app: Flask = Flask(__name__) @@ -15,6 +18,44 @@ def handle_get(url_path: str = "index.html") -> Response: response: Response = send_file(requested_file_path) response.status = 200 if path.isfile(requested_file_path): - print('a') return send_file(requested_file_path) abort(404) + + +class QueryHandler: + """ + This is the bridge between the frontend and DataHandler class + """ + __data_handler: DataHandler + + @staticmethod + @app.post("/login") + def login() -> Response: + """ + Logs the user in + """ + data: ImmutableMultiDict[str, str] = request.form + if "user_name" not in data: + return Response( + "user_name not given", + 400, + content_type="text/plain" + ) + if "password" not in data: + return Response( + "password is not given", + 400, + content_type="text/plain" + ) + + try: + QueryHandler.__data_handler = \ + DataHandler(data["user_name"], data["password"]) + except ValueError as value_error: + return Response( + str(value_error), + 403, + content_type="text/plain" + ) + + return Response(status=200)