From 074c7f2926e6e6e96fd07da537461a3b38c2d463 Mon Sep 17 00:00:00 2001 From: YuPei Date: Thu, 14 Oct 2021 02:18:07 +0800 Subject: [PATCH 1/2] Homework :) --- Auth/AuthAPI.py | 54 +++++++++++++++++++++++++++++++++++++++++ Profiles/ProfilesAPI.py | 44 +++++++++++++++++++++++++++++++-- README.md | 13 +++++----- main.py | 10 +++++++- 4 files changed, 112 insertions(+), 9 deletions(-) diff --git a/Auth/AuthAPI.py b/Auth/AuthAPI.py index b903496..34541ec 100644 --- a/Auth/AuthAPI.py +++ b/Auth/AuthAPI.py @@ -1,7 +1,61 @@ # 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(): + username = request.args.get("username") + passwordHash = request.args.get("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=["GET"]) +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..b696e2f 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('/profiles', 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(): + 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(): + 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) From c8b44cf032e9cd3083dee38218c611d5fe5b04ee Mon Sep 17 00:00:00 2001 From: YuPei Date: Wed, 27 Oct 2021 19:00:38 +0800 Subject: [PATCH 2/2] fixes --- Auth/AuthAPI.py | 7 ++++--- Profiles/ProfilesAPI.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Auth/AuthAPI.py b/Auth/AuthAPI.py index 34541ec..0d4dbf3 100644 --- a/Auth/AuthAPI.py +++ b/Auth/AuthAPI.py @@ -16,8 +16,9 @@ @auth_api.route('/register', methods=["POST"]) def register(): - username = request.args.get("username") - passwordHash = request.args.get("passwordHash") + form = request.form + username = form["username"] + passwordHash = form["passwordHash"] myArray.append({ "username": username, "passwordHash": passwordHash @@ -37,7 +38,7 @@ def register(): } -@auth_api.route('/login', methods=["GET"]) +@auth_api.route('/login', methods=["POST"]) def login(): username = request.args.get("username") passwordHash = request.args.get("passwordHash") diff --git a/Profiles/ProfilesAPI.py b/Profiles/ProfilesAPI.py index b696e2f..9882b7f 100644 --- a/Profiles/ProfilesAPI.py +++ b/Profiles/ProfilesAPI.py @@ -7,7 +7,7 @@ profiles_api = Blueprint("profiles", __name__) -@profiles_api.route('/profiles', methods=["POST"]) +@profiles_api.route('/', methods=["POST"]) def addProfile(): name = request.args.get("name") db.append({"name": name}) @@ -18,7 +18,7 @@ def addProfile(): @profiles_api.route('/', methods=["GET", "DELETE"]) -def getProfile(): +def getProfile(id): if request.method == "GET": returnData = db[id] return jsonify({ @@ -35,7 +35,7 @@ def getProfile(): @profiles_api.route('//score', methods=["GET"]) -def getProfileMinScore(): +def getProfileMinScore(id): minScore = request.args.get("minScore") listOfScores = db[id].get("scores") for i in listOfScores: