-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt fixing the merged endpoints codebase
Co-authored-by: Akashdeep Dhar <[email protected]> Co-authored-by: Mehmet Baran Geylani <[email protected]>
- Loading branch information
Showing
6 changed files
with
140 additions
and
195 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,35 +1,29 @@ | ||
from flask import Response, Request, request, abort | ||
from flask import Response, request, abort | ||
from functools import wraps | ||
|
||
|
||
def not_found() -> Response: | ||
return {'message': 'Not Found'}, 404 | ||
def validate_request(fields=None): | ||
fields = fields or ['username'] | ||
|
||
|
||
def bad_request() -> Response: | ||
return abort(400, 'Bad Request') | ||
|
||
|
||
def conflict(data: dict) -> Response: | ||
return abort(409, data) | ||
|
||
|
||
def validate_request(request: dict, fields=['username']): | ||
return all(field in request for field in fields) | ||
|
||
|
||
def unprocessable_entity() -> Response: | ||
return {'message': 'Unprocessable Entity'}, 429 | ||
|
||
|
||
def validate_request(fields: list = None): | ||
fields = fields or ["username"] | ||
def decorator(view): | ||
@wraps(view) | ||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
if all(field in request.json for field in fields): | ||
return view(*args, **kwargs) | ||
else: | ||
abort(429, "Missing fields") | ||
# Check if the request has JSON data | ||
if not request.is_json: | ||
return abort(400, {"error": "Invalid input, JSON required"}) | ||
data = request.get_json() | ||
# Check if all required fields are present | ||
missing_fields = [field for field in fields if field not in data] | ||
if missing_fields: | ||
return abort(400, {"error": f"Missing fields: {', '.join(missing_fields)}"}) | ||
|
||
return func(*args, **kwargs) | ||
return wrapper | ||
|
||
# If the decorator is used without arguments | ||
if callable(fields): | ||
func = fields | ||
fields = ['username'] | ||
return decorator(func) | ||
|
||
return decorator |
Oops, something went wrong.