Skip to content

Commit

Permalink
Create User endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
brngylni authored and gridhead committed Jul 15, 2024
1 parent de4b606 commit 38c6661
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
60 changes: 60 additions & 0 deletions webhook_to_fedora_messaging/endpoints/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from flask import Blueprint, Flask, request, Response, Request
from ..database import db
from ..models.user import User
from sqlalchemy_helpers import get_or_create
from .util import success, bad_request, conflict, created, not_found, validate_request, unprocessable_entity


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


@user_endpoint.route("/user", methods=["POST"])
@validate_request
def create_user():
"""Used for creating a new user by sending a post request to /user/ path.
Request Body:
username: Username of the user
"""
session = db.Session()
user, is_created = get_or_create(session, User, username=request.json['username'])
if not is_created:
return conflict({'message': 'User Already Exists'})
else:
return created({'message': 'Created', 'uuid': user.id})


@user_endpoint.route("/user/search", methods=["GET"])
@validate_request
def get_user():
"""Used for retrieving a user by sending a get request to /user/search path.
Request Body:
username: Username of the user
"""
session = db.Session()
users = session.query(User).filter(User.username.like(request.json['username'])).all()
if users is None or users == []:
return not_found()
else:
return success({'user_list': users})


@user_endpoint.route("/user", methods=["GET"])
@validate_request
def lookup_user():
"""Used for searching a user by sending a get request to /user/ path.
Request Body:
username: Username of the user
"""
session = db.Session()

user = session.query(User).filter(User.username == request.json['username']).first()
if user is None:
return not_found()
else:
return success({'uuid': user.id, 'username': user.username})
5 changes: 3 additions & 2 deletions webhook_to_fedora_messaging/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .config import get_config
from .config.defaults import LOGGER_CONFIG
from logging.config import dictConfig
from .endpoints.user import user_endpoint
from webhook_to_fedora_messaging.exceptions import ConfigError
import logging

Expand All @@ -22,7 +23,6 @@ def create_app():
main = Flask(
"webhook_to_fedora_messaging"
)

# First attempt loading the defaults from the Defaults object
main.config.from_object(
"webhook_to_fedora_messaging.config.defaults.Defaults"
Expand All @@ -39,5 +39,6 @@ def create_app():
main.config.from_mapping(confdata)
db.init_app(main)
dictConfig(confdata["logsconf"])

main.register_blueprint(user_endpoint)

return main

0 comments on commit 38c6661

Please sign in to comment.