-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from flask import Response, Request, request, abort | ||
from functools import wraps | ||
|
||
def not_found() -> Response: | ||
return Response({'message': 'Not Found'}, status=404, mimetype='application/json') | ||
|
||
|
||
def success(data: dict) ->Response: | ||
return Response(data, status=200, mimetype='application/json') | ||
|
||
|
||
def bad_request() -> Response: | ||
return Response("{'message': 'Bad Request'}", status=400, mimetype='application/json') | ||
|
||
|
||
def created(data: dict) -> Response: | ||
return Response(data, status=201, mimetype='application/json') | ||
|
||
|
||
def conflict(data: dict) -> Response: | ||
return Response(data, status=409, mimetype='application/json') | ||
|
||
|
||
def validate_request(request: dict, fields=['username']): | ||
return all(field in request for field in fields) | ||
|
||
|
||
def unprocessable_entity() -> Response: | ||
return Response("{'message': 'Unprocessable Entity}", status=429, mimetype="application/json") | ||
|
||
|
||
def validate_request(fields: list = None): | ||
fields = fields or ["username"] | ||
def decorator(view): | ||
@wraps(view) | ||
def wrapper(*args, **kwargs): | ||
if all(field in request.json for field in fields): | ||
return view(*args, **kwargs) | ||
else: | ||
abort(429, "Missing fields") | ||
return wrapper | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters