Skip to content

Commit

Permalink
initialise the endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
brngylni committed Jun 30, 2024
1 parent 56364a3 commit fbcd585
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions webhook_to_fedora_messaging/endpoints/user.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
from flask import Blueprint, Flask, request, Response
from flask import Blueprint, Flask, request, Response, Request
from ..database import db
from ..models.user import User
from sqlalchemy_helpers import get_or_create

app = Flask(__name__)
user_endpoint = Blueprint("user_endpoint", __name__)

@user_endpoint.route("/user/", methods=["POST"])
@user_endpoint.route("/user", methods=["POST"])
def create_user():
"""
Used for creating a new user by sending a post request to /user/ path.
"""

if request.json['username'] is None:
if not validate_request(request):
return Response("{'message': 'Bad Request'}", status=400, mimetype='application/json')

session = db.Session()

user, created = get_or_create(User, username=request.json['username'])
user, created = get_or_create(session, User, username=request.json['username'])
if not created:
return Response("{'message': 'User Already Exists'}", status=409, mimetype='application/json')
else:
return Response("{'message': 'Created', 'uuid': '%s'}" % user['id'], status=201, mimetype='application/json')


@user_endpoint.route("/user/search", methods=["GET"])
def get_user():

if not validate_request(request):
return Response("{'message': 'Bad Request'}", status=400, mimetype='application/json')
session = db.Session()
users = session.query(User).filter(User.username.like(request.json['username'])).all()
if users is None or users == []:
return Response("{'message': 'Not Found'}", status=404, mimetype='application/json')
else:
return Response("{'user_list': "+ users +"}", status=200, mimetype='application/json')


@user_endpoint.route("/user", methods=["GET"])
def lookup_user():
if not validate_request(request):
return Response("{'message': 'Bad Request'}", status=400, mimetype='application/json')
session = db.Session()

user = session.query(User).filter(User.username == request.json['username']).first()
if user is None:
return Response("{'message': 'Not Found'}", status=404, mimetype='application/json')
else:
return Response("{'uuid': "+ user.id +", 'username': '"+ user.username +"'}", status=200, mimetype='application/json')




def validate_request(request: Request):
if request.json['username'] is None:
return False
return True


0 comments on commit fbcd585

Please sign in to comment.