Some description.


License
MIT
Install
pip install r3sponse==2.10.4

Documentation

r3sponse

Author(s): Daan van den Bergh.
Copyright: © 2020 Daan van den Bergh All Rights Reserved.
Supported Operating Systems: macos & linux.


Bergh-Encryption

Table of content:

Description:

Univeral python responses.

Installation:

Install the package.

pip3 install r3sponse --upgrade && python3 -c 'import r3sponse' --create-alias r3sponse --sudo

Example Function.

An example function that returns a ResponseObject class.

# import the package
from r3sponse import r3sponse

# universal responses.
def my_function():

	# return an error response from a function.
	return r3sponse.error("Failed to retrieve your personal API key")

	# return a success response from a function.
	return r3sponse.success("Success retrieved your personal API key", {
		"api_key":api_key,
	})

# check if a response was successfull.
response = my_function()
if response.success:
	message = response["message"]
else:
	error = response["error"]

Code Examples:

Table of content:

Parameters:

The parameters object class.

# initialize the parameters object class.
parameters = Parameters()

Functions:

get:
# call parameters.get.
_ = parameters.get(
    # the django request (1).
    request=None,
    # the identifiers (#2).
    #    str instance: return the parameters value.
    #    list instance: return a parameters object & return an error response when a parameter is undefined.
    #    dict instance: return a parameters object & return the parameter's value from the dict as a default when undefined.
    parameters=[],
    # default return value (dict instance of parameters overwrites the default parameter).
    default=None,
    # traceback id.
    traceback=None, )
check:
# call parameters.check.
response = parameters.check(
    # the parameters (dict) (#1).
    parameters={"parameter":None},
    # the recognizer value for when the parameters are supposed to be empty.
    default=None,
    # the traceback id.
    traceback=None, )

R3sponse:

The r3sponse object class.

# import the r3sponse object class.
from r3sponse import r3sponse

Functions:

success:
# call r3sponse.success.
_ = r3sponse.success(
    # the message (must be param #1).
    message,
    # additional returnable functions (must be param #2).
    variables={},
    # log log level of the message (int).
    log_level=None,
    # the required log level for when printed to console (leave None to use r3sponse.log_level).
    required_log_level=None,
    # save the error to the logs file.
    save=False,
    # return as a django JsonResponse.
    django=False, )
error:
# call r3sponse.error.
_ = r3sponse.error(
    # the error message.
    error="",
    # log log level of the message (int).
    log_level=None,
    # the required log level for when printed to console (leave None to use r3sponse.log_level).
    required_log_level=None,
    # save the error to the erros file.
    save=False,
    # return as a django JsonResponse.
    django=False,
    # raise error for developer traceback.
    traceback=ERROR_TRACEBACK, )
log:
# call r3sponse.log.
_ = r3sponse.log(
    # option 1:
    # the message (#1 param).
    message=None,
    # option 2:
    # the error.
    error=None,
    # option 3:
    # the response dict (leave message None to use).
    response={},
    # print the response as json.
    json=False,
    # optionals:
    # the log level for printing to console.
    log_level=0,
    # the required log level for when printed to console (leave None to use r3sponse.log_level).
    required_log_level=None,
    # save to log file.
    save=False,
    # save errors only (for option 2 only).
    save_errors=False, )
load_logs:
# call r3sponse.load_logs.
_ = r3sponse.load_logs(format="webserver", options=["webserver", "cli", "array", "string"])
reset_logs:
# call r3sponse.reset_logs.
_ = r3sponse.reset_logs()
serialize:
# call r3sponse.serialize.
_ = r3sponse.serialize(response={})
response:
# call r3sponse.response.
_ = r3sponse.response(
    # the blank response (dict) (#1).
    response={
        "success":False,
        "message":None,
        "error":None,
    },
    # the safe boolean (serialize from dumped json).
    safe=False, )

ResponseObject:

The response_object object class.

# initialize the response_object object class.
response_object = ResponseObject(
    # the response attributes.
    attributes={
        "success":False,
        "message":None,
        "error":None,
    },
    # import a dumped json response (str) (ignores attributes).
    json=None, )

Functions:

clean:
# call response_object.clean.
_ = response_object.clean()
assign:
# call response_object.assign.
_ = response_object.assign(dictionary)
crash:
# call response_object.crash.
_ = response_object.crash(error="ValueError", traceback=True, json=False)
unpack:
# call response_object.unpack.
_ = response_object.unpack(
    # the key / keys / defaults parameter (#1).
    # str instance:
    #   unpack the str key
    # list instance:
    #   unpack all keys in the list.
    # dict instance:
    #   unpack all keys from the dict & when not present return the key's value as default.
    keys, )
remove:
# call response_object.remove.
_ = response_object.remove(keys=[], values=[], save=False)
iterate:
# call response_object.iterate.
_ = response_object.iterate(sorted=False, reversed=False)
items:
# call response_object.items.
_ = response_object.items(sorted=False, reversed=False, dictionary=None)
keys:
# call response_object.keys.
_ = response_object.keys(sorted=False, reversed=False)
values:
# call response_object.values.
_ = response_object.values(sorted=False, reversed=False, dictionary=None)
reversed:
# call response_object.reversed.
_ = response_object.reversed(dictionary=None)
sort:
# call response_object.sort.
_ = response_object.sort(alphabetical=True, ascending=False, reversed=False, dictionary=None)
dict:
# call response_object.dict.
_ = response_object.dict(sorted=False, reversed=False, json=False)
json:
# call response_object.json.
_ = response_object.json(sorted=False, reversed=False, indent=4, dictionary=None, )
serialize:
# call response_object.serialize.
_ = response_object.serialize(sorted=False, reversed=False, json=False, dictionary=None)
instance:
# call response_object.instance.
_ = response_object.instance()
raw:
# call response_object.raw.
_ = response_object.raw()