Many api changes
- Added basic jwt auth - Added keyword endpoints - added share/symbol endpoints - updated postman - refactoring
This commit is contained in:
62
webservice/blueprints/keyword.py
Normal file
62
webservice/blueprints/keyword.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from webservice.db import db
|
||||
from webservice.helper_functions import get_username_from_token_data, extract_token_data, get_token, get_user_id_from_username
|
||||
from webservice.models import Keyword
|
||||
|
||||
keyword_blueprint = Blueprint('keyword', __name__, url_prefix='/api')
|
||||
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
||||
|
||||
|
||||
@keyword_blueprint.route('/keyword', methods=['POST'])
|
||||
def add_keyword():
|
||||
request_data = request.get_json()
|
||||
key = request_data['keyword']
|
||||
|
||||
# get username from jwt token
|
||||
username = get_username_from_token_data(extract_token_data(get_token()))
|
||||
|
||||
check_keyword = db.session.query(Keyword).filter_by(keyword=key, user_id=get_user_id_from_username(username)).first()
|
||||
if check_keyword is None:
|
||||
# Keyword doesn't exist yet for this user
|
||||
new_keyword = Keyword(
|
||||
user_id=get_user_id_from_username(username),
|
||||
keyword=key
|
||||
)
|
||||
db.session.add(new_keyword)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully added keyword", "data": new_keyword.as_dict()})
|
||||
else:
|
||||
return jsonify({"status": 500, "text": "Keyword already exist for this user"})
|
||||
|
||||
|
||||
@keyword_blueprint.route('/keyword', methods=['DELETE'])
|
||||
def remove_keyword():
|
||||
request_data = request.get_json()
|
||||
key = request_data['keyword']
|
||||
|
||||
# get username from jwt token
|
||||
username = get_username_from_token_data(extract_token_data(get_token()))
|
||||
|
||||
db.session.query(Keyword).filter_by(keyword=key, user_id=get_user_id_from_username(username)).delete()
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully removed keyword"})
|
||||
|
||||
|
||||
@keyword_blueprint.route('/keywords', methods=['GET'])
|
||||
def get_keywords():
|
||||
# get username from jwt token
|
||||
username = get_username_from_token_data(extract_token_data(get_token()))
|
||||
|
||||
return_keywords = []
|
||||
keywords = db.session.query(Keyword).filter_by(user_id=get_user_id_from_username(username)).all()
|
||||
|
||||
if keywords is not None:
|
||||
for row in keywords:
|
||||
return_keywords.append(row.as_dict())
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully loaded keywords", "data": return_keywords})
|
62
webservice/blueprints/shares.py
Normal file
62
webservice/blueprints/shares.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from webservice.db import db
|
||||
from webservice.helper_functions import get_username_from_token_data, extract_token_data, get_token, get_user_id_from_username
|
||||
from webservice.models import Keyword, Share
|
||||
|
||||
shares_blueprint = Blueprint('share', __name__, url_prefix='/api')
|
||||
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
||||
|
||||
|
||||
@shares_blueprint.route('/share', methods=['POST'])
|
||||
def add_symbol():
|
||||
request_data = request.get_json()
|
||||
symbol = request_data['symbol']
|
||||
|
||||
# get username from jwt token
|
||||
username = get_username_from_token_data(extract_token_data(get_token()))
|
||||
|
||||
check_share = db.session.query(Share).filter_by(symbol=symbol, user_id=get_user_id_from_username(username)).first()
|
||||
if check_share is None:
|
||||
# Keyword doesn't exist yet for this user
|
||||
new_symbol = Share(
|
||||
user_id=get_user_id_from_username(username),
|
||||
symbol=symbol
|
||||
)
|
||||
db.session.add(new_symbol)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully added symbol", "data": new_symbol.as_dict()})
|
||||
else:
|
||||
return jsonify({"status": 500, "text": "Symbol already exist for this user"})
|
||||
|
||||
|
||||
@shares_blueprint.route('/share', methods=['DELETE'])
|
||||
def remove_symbol():
|
||||
request_data = request.get_json()
|
||||
symbol = request_data['symbol']
|
||||
|
||||
# get username from jwt token
|
||||
username = get_username_from_token_data(extract_token_data(get_token()))
|
||||
|
||||
db.session.query(Share).filter_by(symbol=symbol, user_id=get_user_id_from_username(username)).delete()
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully removed symbol"})
|
||||
|
||||
|
||||
@shares_blueprint.route('/shares', methods=['GET'])
|
||||
def get_symbol():
|
||||
# get username from jwt token
|
||||
username = get_username_from_token_data(extract_token_data(get_token()))
|
||||
|
||||
return_symbols = []
|
||||
symbols = db.session.query(Share).filter_by(user_id=get_user_id_from_username(username)).all()
|
||||
|
||||
if symbols is not None:
|
||||
for row in symbols:
|
||||
return_symbols.append(row.as_dict())
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully loaded symbols", "data": return_symbols})
|
64
webservice/blueprints/user.py
Normal file
64
webservice/blueprints/user.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import datetime
|
||||
import os
|
||||
|
||||
import jwt
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from webservice.db import db
|
||||
from webservice.helper_functions import check_password, hash_password, get_token, extract_token_data
|
||||
from webservice.models import User
|
||||
|
||||
users_blueprint = Blueprint('users', __name__, url_prefix='/api')
|
||||
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
||||
|
||||
|
||||
@users_blueprint.route('/users', methods=['GET'])
|
||||
def users():
|
||||
res = []
|
||||
for i in User.query.all():
|
||||
res.append(i.as_dict())
|
||||
|
||||
return jsonify({"status": 200, "data": res})
|
||||
|
||||
|
||||
@users_blueprint.route('/login', methods=['POST'])
|
||||
def login():
|
||||
request_data = request.get_json()
|
||||
username = request_data['username']
|
||||
password = request_data['password']
|
||||
|
||||
user = db.session.query(User).filter_by(username=username).first()
|
||||
if check_password(user.password, password):
|
||||
token = jwt.encode({'username': user.username, 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=45)}, os.getenv('SECRET_KEY'), "HS256")
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully logged in", "data": token})
|
||||
else:
|
||||
return jsonify({"status": 500, "text": "Unable to login"})
|
||||
|
||||
|
||||
@users_blueprint.route('/logout', methods=['GET'])
|
||||
def logout():
|
||||
# TODO
|
||||
return jsonify({"status": 200, "text": "Successfully logged out"})
|
||||
|
||||
|
||||
@users_blueprint.route('/register', methods=['POST'])
|
||||
def register():
|
||||
request_data = request.get_json()
|
||||
username = request_data['username']
|
||||
password = request_data['password']
|
||||
|
||||
user = db.session.query(User).filter_by(username=username).first()
|
||||
if user is None:
|
||||
# Username doesn't exist yet
|
||||
user = User(
|
||||
username=username,
|
||||
password=hash_password(password),
|
||||
admin=False
|
||||
)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"status": 200, "text": "Successfully registered user", "data": user.as_dict()})
|
||||
else:
|
||||
return jsonify({"status": 500, "text": "Username already exist"})
|
Reference in New Issue
Block a user