Skip to content

Commit

Permalink
Added missing types
Browse files Browse the repository at this point in the history
  • Loading branch information
Seluj78 committed Sep 22, 2024
1 parent 5a64000 commit 1d0d017
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 12 deletions.
2 changes: 2 additions & 0 deletions flask_utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ def validate_params(
:param parameters: Dictionary of parameters to validate. The keys are parameter names
and the values are the expected types.
:type parameters: Dict[Any, Any]
:param allow_empty: Allow empty values for parameters. Defaults to False.
:type allow_empty: bool
:raises BadRequestError: If the JSON body is malformed,
the Content-Type header is missing or incorrect, required parameters are missing,
Expand Down
36 changes: 36 additions & 0 deletions flask_utils/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def _register_error_handlers(application: Flask) -> None:
This function will register all the error handlers for the application
:param application: The Flask application to register the error handlers
:type application: flask.Flask
:return: None
:rtype: None
.. versionchanged:: 0.5.0
Made the function private. If you want to register the custom error handlers, you need to
Expand Down Expand Up @@ -50,7 +53,10 @@ def generate_badrequest(error: BadRequestError) -> Response:
a custom message and the 400 code
:param error: The error body
:type error: BadRequestError
:return: Returns the response formatted
:rtype: flask.Response
"""
return _generate_error_json(error, 400)

Expand All @@ -61,7 +67,10 @@ def generate_conflict(error: ConflictError) -> Response:
a custom message and the 409 code
:param error: The error body
:type error: ConflictError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 409)
Expand All @@ -73,7 +82,10 @@ def generate_forbidden(error: ForbiddenError) -> Response:
a custom message and the 403 code
:param error: The error body
:type error: ForbiddenError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 403)
Expand All @@ -85,7 +97,10 @@ def generate_notfound(error: NotFoundError) -> Response:
a custom message and the 404 code.
:param error: The error body
:type error: NotFoundError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 404)
Expand All @@ -97,7 +112,10 @@ def generate_unauthorized(error: UnauthorizedError) -> Response:
a custom message and the 401 code.
:param error: The error body
:type error: UnauthorizedError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 401)
Expand All @@ -109,7 +127,10 @@ def generate_origin_is_unreachable(error: OriginIsUnreachableError) -> Response:
a custom message and the 523 code.
:param error: The error body
:type error: OriginIsUnreachableError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 523)
Expand All @@ -121,7 +142,10 @@ def generate_web_server_is_down(error: WebServerIsDownError) -> Response:
a custom message and the 521 code.
:param error: The error body
:type error: WebServerIsDownError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 521)
Expand All @@ -133,7 +157,10 @@ def generate_failed_dependency(error: FailedDependencyError) -> Response:
a custom message and the 424 code.
:param error: The error body
:type error: FailedDependencyError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 424)
Expand All @@ -145,7 +172,10 @@ def generate_gone(error: GoneError) -> Response:
a custom message and the 410 code.
:param error: The error body
:type error: GoneError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 410)
Expand All @@ -157,7 +187,10 @@ def generate_unprocessable_entity(error: UnprocessableEntityError) -> Response:
a custom message and the 422 code.
:param error: The error body
:type error: UnprocessableEntityError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 422)
Expand All @@ -169,7 +202,10 @@ def generate_service_unavailable(error: ServiceUnavailableError) -> Response:
a custom message and the 503 code.
:param error: The error body
:type error: ServiceUnavailableError
:return: Returns the response formatted
:rtype: flask.Response
"""

return _generate_error_json(error, 503)
Expand Down
5 changes: 5 additions & 0 deletions flask_utils/errors/_error_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ def _generate_error_json(error: _BaseFlaskException, status_code: int) -> Respon
This function is used to generate a json of the error passed
:param error: The error containing the message and solution
:type error: _BaseFlaskException
:param status_code: The status code of the error.
:type status_code: int
:return: Returns a json containing all the info
:rtype: flask.Response
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/badrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BadRequestError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
28 changes: 28 additions & 0 deletions flask_utils/errors/base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@


class _BaseFlaskException(Exception):
"""
This is the base class for all the exceptions in this package.
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: Optional[str]
:param status_code: The status code of the error.
:type status_code: Optional[int]
:param name: The name of the error.
:type name: Optional[str]
:Example:
.. code-block:: python
from flask_utils.errors import _BaseFlaskException
class MyError(_BaseFlaskException):
self.name = "MyError"
self.msg = msg
self.solution = solution
self.status_code = 666
.. versionadded:: 0.1.0
"""

name: Optional[str] = None
msg: Optional[str] = None
solution: Optional[str] = "Try again."
status_code: Optional[int] = 400
2 changes: 1 addition & 1 deletion flask_utils/errors/conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ConflictError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/failed_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FailedDependencyError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/forbidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ForbiddenError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/gone.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class GoneError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/notfound.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NotFoundError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/origin_is_unreachable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OriginIsUnreachableError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/service_unavailable.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ServiceUnavailableError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/unauthorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UnauthorizedError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/unprocessableentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UnprocessableEntityError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
2 changes: 1 addition & 1 deletion flask_utils/errors/web_server_is_down.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WebServerIsDownError(_BaseFlaskException):
:param msg: The message to be displayed in the error.
:type msg: str
:param solution: The solution to the error.
:type solution: str
:type solution: Optional[str]
:Example:
Expand Down
32 changes: 31 additions & 1 deletion flask_utils/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class FlaskUtils(object):
Call :meth:`init_app` to configure the extension on an application.
:param app: Flask application instance.
:param register_error_handlers: Register the custom error handlers. Default is True.
:type app: Optional[Flask]
:param register_error_handlers: Register the custom error handlers. Default is ``True``.
:param register_error_handlers: bool
:Example:
Expand All @@ -34,6 +37,30 @@ class FlaskUtils(object):
"""

def __init__(self, app: Optional[Flask] = None, register_error_handlers: bool = True):
"""
:param app: Flask application instance.
:type app: Optional[Flask]
:param register_error_handlers: Register the custom error handlers. Default is ``True``.
:type register_error_handlers: bool
:Example:
.. code-block:: python
from flask import Flask
from flask_utils import FlaskUtils
app = Flask(__name__)
fu = FlaskUtils(app)
# or
fu = FlaskUtils()
fu.init_app(app)
.. versionadded:: 0.5.0
"""
self.has_error_handlers_registered = False

if app is not None:
Expand All @@ -42,7 +69,10 @@ def __init__(self, app: Optional[Flask] = None, register_error_handlers: bool =
def init_app(self, app: Flask, register_error_handlers: bool = True) -> None:
"""
:param app: The Flask application to initialize.
:type app: Flask
:param register_error_handlers: Register the custom error handlers. Default is ``True``.
:type register_error_handlers: bool
Initialize a Flask application for use with this extension instance. This
must be called before any request is handled by the application.
Expand Down
3 changes: 3 additions & 0 deletions flask_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ def is_it_true(value: str) -> bool:
Useful for flask's request.form.get() method and request.args.get() method
:param value: String value to check if it is true
:type value: str
:return: True if value is true, False otherwise
:rtype: bool
:Example:
Expand Down

0 comments on commit 1d0d017

Please sign in to comment.