Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework :) #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Auth/AuthAPI.py
Original file line number Diff line number Diff line change
@@ -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"
})
44 changes: 42 additions & 2 deletions Profiles/ProfilesAPI.py
Original file line number Diff line number Diff line change
@@ -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('/<int:id>', 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('/<int:id>/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
}
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 :)))))
10 changes: 9 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)