Web Helpers provides various response functions that help the users return data over a Django or Flask API.
WebHelpersUtils
core_lib.web_helpers.web_helprs_utils.WebHelpersUtils [source]
This class is used to set the type of web framework that will be utilized in the application i.e. Django or Flask.
Functions
init()
core_lib.web_helpers.web_helprs_utils.WebHelpersUtils.init() [source]
Used to set the server type used in the application, users must select the server type from the ServerType class provided by the WebHelpersUtils class.
ServerType Class
class ServerType(enum.Enum):
FLASK = 'flask'
DJANGO = 'django'
def init(server_type: ServerType):
Arguments
server_type(ServerType): Sets the server type.
Example
from core_lib.web_helpers.web_helprs_utils import WebHelpersUtils
WebHelpersUtils.init(WebHelpersUtils.ServerType.FLASK)
get_server_type()
core_lib.web_helpers.web_helprs_utils.WebHelpersUtils.get_server_type() [source]
Returns the server type set by the init().
def get_server_type() -> ServerType:
Returns
(ServerType): Server type Django or Flask.
Example
from core_lib.web_helpers.web_helprs_utils import WebHelpersUtils
WebHelpersUtils.get_server_type() # returns flask
Request Response Helpers
Depending on the server type set in WebHelpersUtils class, request and response functions return data.
Functions
response_status()
core_lib.web_helpers.request_response_helpers.response_status() [source]
Returns empty data with HTTPStatus value provided, as a response object.
def response_status(status: int = HTTPStatus.OK.value):
Arguments
status(int): DefaultHTTPStatus.OK.value,HTTPStatusvalue to be set.
Returns
Returns response object with the set status.
Example
from http import HTTPStatus
from core_lib.web_helpers.web_helprs_utils import WebHelpersUtils
from core_lib.web_helpers.request_response_helpers import response_status
WebHelpersUtils.init(WebHelpersUtils.ServerType.FLASK)
response_status(HTTPStatus.OK) # returns status 200 with empty data
response_status(HTTPStatus.INTERNAL_SERVER_ERROR) # returns status 500 with empty data
response_ok()
core_lib.web_helpers.request_response_helpers.response_ok() [source]
Returns message ok with HTTPStatus value provided, as a response object.
def response_ok(status: int = HTTPStatus.OK.value):
Arguments
status(int): DefaultHTTPStatus.OK.value,HTTPStatusvalue to be set.
Returns
Returns response object with the set status and message ok.
Example
from http import HTTPStatus
from core_lib.web_helpers.web_helprs_utils import WebHelpersUtils
from core_lib.web_helpers.request_response_helpers import response_ok
WebHelpersUtils.init(WebHelpersUtils.ServerType.FLASK)
response_ok(HTTPStatus.OK) # returns status 200 with data {'message': 'ok'}
response_message()
core_lib.web_helpers.request_response_helpers.response_message() [source]
Returns message with HTTPStatus value provided, as a response object.
def response_message(message='', status: int = HTTPStatus.OK.value):
Arguments
message: Default'', message to be sent in the response.status(int): DefaultHTTPStatus.OK.value,HTTPStatusvalue to be set.
If
HTTPStatusis set to500the data will be returned inerrorkey.
Returns
Returns response object with the set status and message.
Example
from http import HTTPStatus
from core_lib.web_helpers.web_helprs_utils import WebHelpersUtils
from core_lib.web_helpers.request_response_helpers import response_message
WebHelpersUtils.init(WebHelpersUtils.ServerType.FLASK)
response_message('success', HTTPStatus.OK) # returns status 200 with data {'message': 'success'}
response_message('some error occurred', HTTPStatus.INTERNAL_SERVER_ERROR) # returns status 500 with data {'error': 'some error occurred'}
response_json()
core_lib.web_helpers.request_response_helpers.response_json() [source]
Returns message with HTTPStatus value provided, and data transformed to JSON.
def response_json(data: dict, status: int = HTTPStatus.OK.value):
Arguments
data(dict): Dictionary data to be sent in the response.status(int): DefaultHTTPStatus.OK.value,HTTPStatusvalue to be set.
Returns
Returns response object with the set status and data transformed to JSON.
Example
from http import HTTPStatus
from core_lib.web_helpers.web_helprs_utils import WebHelpersUtils
from core_lib.web_helpers.request_response_helpers import response_json
WebHelpersUtils.init(WebHelpersUtils.ServerType.FLASK)
response_json({'username': 'Jon Doe'}, HTTPStatus.OK) # returns status 200 with data {'username': 'Jon Doe'}
response_json({'error': 'Server Error'}, HTTPStatus.INTERNAL_SERVER_ERROR) # returns status 500 with data {'error': 'Server Error'}
response_json({'error': 'file not found'}, HTTPStatus.NOT_FOUND) # returns status 404 with data {'error': 'file not found'}