<filename>api1/web/app.py
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
from pymongo import MongoClient
import bcrypt

app = Flask(__name__)
api = Api(app)

client = MongoClient("mongodb://db:27017")
db = client.SentencesDatabase
users = db["Users"]

class Register(Resource):
    def post(self):
        postedData = request.get_json()
        username = postedData["username"]
        password = postedData["password"]
        hashed_pw = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
        users.insert({
            "Username" : username,
            "Password" : <PASSWORD>,
            "Sentence" : "",
            "Tokens" : 6
        })
        retJson = { 
            "status":200,
            "msg": "You successfully signed up for the API"
        }
        return jsonify(retJson)

def verifyPw(username, password):
    hashed_pw = users.find({
        "Username":username
    })[0]["Password"]

    if bcrypt.hashpw(password.encode('utf8'), hashed_pw) == hashed_pw:
        return True
    else:
        return False

def countTokens(username):
    return users.find({
        "Username" : username,
    })[0]["Tokens"]

class Store(Resource):
    def post(self):
        postedData = request.get_json()
        username = postedData["username"]
        password = postedData["password"]
        sentence = postedData["sentence"]

        corret_pw = verifyPw(username, password)

        if not corret_pw:
            retJson = { 
                "status":302,
                "msg": "Your password is wrong"
            }
        num_tokens = countTokens(username)
        if num_tokens <= 0:
            retJson = { 
                "status":301,
                "msg": "You do not have enough tokens"
            }
            return jsonify(retJson)
        users.update({
            "Username":username
        }, { 
            "$set": {
                "Sentence":sentence,
                "Tokens" : num_tokens-1
                }
        })

        retJson = { 
            "status":200,
            "msg": "Sentence saved successfully"
        }
        return jsonify(retJson)

class Get(Resource):
    def post(self):
        postedData = request.get_json()
        username = postedData["username"]
        password = postedData["password"]

        correct_pw = verifyPw(username, password)

        if not correct_pw:
            retJson = { 
                "status":302,
                "msg": "Your password is wrong"
            }
            return jsonify(retJson)
        num_tokens = countTokens(username)
        if num_tokens <= 0:
            retJson = {
                "status": 301,
                "msg": "No more tokens"
            }
            return jsonify(retJson)

        users.update({
            "Username":username
        }, {
            "$set":{
                "Tokens":num_tokens-1
                }
        })

        sentence = users.find({
            "Username": username
        })[0]["Sentence"]
        retJson = {
            "status":200,
            "sentence": str(sentence)
        }

        return jsonify(retJson)

api.add_resource(Register, "/register")
api.add_resource(Store, "/store")
api.add_resource(Get, "/get")




@app.route('/')
def hello_would():
    return "Hello World!"



if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
<filename>api1/web/app.py
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
from pymongo import MongoClient
import bcrypt

app = Flask(__name__)
api = Api(app)

client = MongoClient("mongodb://db:27017")
db = client.SentencesDatabase
users = db["Users"]

class Register(Resource):
    def post(self):
        postedData = request.get_json()
        username = postedData["username"]
        password = postedData["password"]
        hashed_pw = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
        users.insert({
            "Username" : username,
            "Password" : <PASSWORD>,
            "Sentence" : "",
            "Tokens" : 6
        })
        retJson = { 
            "status":200,
            "msg": "You successfully signed up for the API"
        }
        return jsonify(retJson)

def verifyPw(username, password):
    hashed_pw = users.find({
        "Username":username
    })[0]["Password"]

    if bcrypt.hashpw(password.encode('utf8'), hashed_pw) == hashed_pw:
        return True
    else:
        return False

def countTokens(username):
    return users.find({
        "Username" : username,
    })[0]["Tokens"]

class Store(Resource):
    def post(self):
        postedData = request.get_json()
        username = postedData["username"]
        password = postedData["password"]
        sentence = postedData["sentence"]

        corret_pw = verifyPw(username, password)

        if not corret_pw:
            retJson = { 
                "status":302,
                "msg": "Your password is wrong"
            }
        num_tokens = countTokens(username)
        if num_tokens <= 0:
            retJson = { 
                "status":301,
                "msg": "You do not have enough tokens"
            }
            return jsonify(retJson)
        users.update({
            "Username":username
        }, { 
            "$set": {
                "Sentence":sentence,
                "Tokens" : num_tokens-1
                }
        })

        retJson = { 
            "status":200,
            "msg": "Sentence saved successfully"
        }
        return jsonify(retJson)

class Get(Resource):
    def post(self):
        postedData = request.get_json()
        username = postedData["username"]
        password = postedData["password"]

        correct_pw = verifyPw(username, password)

        if not correct_pw:
            retJson = { 
                "status":302,
                "msg": "Your password is wrong"
            }
            return jsonify(retJson)
        num_tokens = countTokens(username)
        if num_tokens <= 0:
            retJson = {
                "status": 301,
                "msg": "No more tokens"
            }
            return jsonify(retJson)

        users.update({
            "Username":username
        }, {
            "$set":{
                "Tokens":num_tokens-1
                }
        })

        sentence = users.find({
            "Username": username
        })[0]["Sentence"]
        retJson = {
            "status":200,
            "sentence": str(sentence)
        }

        return jsonify(retJson)

api.add_resource(Register, "/register")
api.add_resource(Store, "/store")
api.add_resource(Get, "/get")




@app.route('/')
def hello_would():
    return "Hello World!"



if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)