diff --git a/Auth/AuthAPI.py b/Auth/AuthAPI.py index b903496..0d4dbf3 100644 --- a/Auth/AuthAPI.py +++ b/Auth/AuthAPI.py @@ -1,7 +1,62 @@ # Score API here from flask import Blueprint import sys + +from flask.globals import current_app, request +from flask.json import jsonify from db import db +import jwt sys.path.append("../") auth_api = Blueprint("auth", __name__) + + +myArray = [] + + +@auth_api.route('/register', methods=["POST"]) +def register(): + form = request.form + username = form["username"] + passwordHash = form["passwordHash"] + myArray.append({ + "username": username, + "passwordHash": passwordHash + }) + token = jwt.encode( + { + "username": username, + "passwordHash": passwordHash + }, + current_app.config["SECRET_KEY"], + algorithm="HS256" + ) + + return { + "message": "success", + "token": token + } + + +@auth_api.route('/login', methods=["POST"]) +def login(): + username = request.args.get("username") + passwordHash = request.args.get("passwordHash") + testUser = { + "username": username, + "passwordHash": passwordHash + } + if testUser in db: + token = jwt.encode( + testUser, + current_app.config["SECRET_KEY"], + algorithm="HS256" + ) + return jsonify({ + "message": "success", + "token": token + }) + + return jsonify({ + "message": "failed" + }) diff --git a/Profiles/ProfilesAPI.py b/Profiles/ProfilesAPI.py index 4467047..9882b7f 100644 --- a/Profiles/ProfilesAPI.py +++ b/Profiles/ProfilesAPI.py @@ -1,7 +1,47 @@ # Profile API here -from flask import Blueprint -import sys from db import db +from flask import Blueprint, json, request, jsonify +import sys sys.path.append("../") profiles_api = Blueprint("profiles", __name__) + + +@profiles_api.route('/', methods=["POST"]) +def addProfile(): + name = request.args.get("name") + db.append({"name": name}) + + return jsonify({ + "message": "success" + }) + + +@profiles_api.route('/', methods=["GET", "DELETE"]) +def getProfile(id): + if request.method == "GET": + returnData = db[id] + return jsonify({ + "message": "success", + "data": returnData + }) + elif request.method == "DELETE": + returnData = db[id] + del db[id] + return jsonify({ + "message": "success", + "deleted": returnData + }) + + +@profiles_api.route('//score', methods=["GET"]) +def getProfileMinScore(id): + minScore = request.args.get("minScore") + listOfScores = db[id].get("scores") + for i in listOfScores: + if i < minScore: + del i + return { + "message": "success", + "data": listOfScores + } diff --git a/README.md b/README.md index 08c35ed..67cf229 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,18 @@ # RHDEV-BE-2-flask + Homewwork template for BE training lesson 2: Flask and web servers Setup a basic API to simulate a website that tracks profiles and scores for exams A simulated db is provided. Note that the db will not be updated between runs - In main: +In main: GET / homepage that returns a welcome message - In profiles API (/profiles prefix) +In profiles API (/profiles prefix) GET /{id} to retrieve the name and all scores of a profile POST /profiles to create a new profile (name only) DELETE /{id} to delete a profile GET /{id}/score?minScore= to retrieve all scores of a profile, above the min score - In authentication API (/auth prefix) +In authentication API (/auth prefix) POST /register stores a username and hashedPassword (given as hashed) Store it in a local array Login /login checks if the provided information is valid and return a jwt token + success message @@ -20,10 +21,10 @@ Give a reasonable return format with appropriate status code and messages. {“message” : “success/fail”, “data”:””} Also submit a simplified documentation of your API. You can use the format below. - - -OPTIONALS: +OPTIONALS: Add environmental variables into the system (for jwt signing secret) In the login route, check if jwt token is provided and valid Assume URL argument has token “?token=sdlkaskdnalsdnsald” See if username and password field arre present + +documentation :))))) diff --git a/main.py b/main.py index fc7fbd4..e20aaec 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,18 @@ from flask import Flask from db import db - # Write your flask code here app = Flask(__name__) app.register_blueprint(profiles_api, url_prefix="/profiles") app.register_blueprint(auth_api, url_prefix="/auth") + + +@app.route("/", methods=["GET"]) +def getHomepage(): + return "Welcome to my Flask App!" + + +if __name__ == "__main__": + app.run("localhost", port=8000)