ResultStatus class
Please note, for further usage illustrations see ResultStatus class.
API ResultStatus.
- class bh_apistatus.result_status.ResultStatus(code=200, text='', session_id=None, **kwargs)
Bases:
object
Encapsulate result status of server operations in response to client requests.
Status includes a response code, an optional response message, an optional unique web session identifier. It is assumed that, at the server-side, the web session identifier also gets logged for each request, and so it should serve to identify the web sessions which caused the errors.
Finally, status can also include optional data, an instance of
result_data.ResultData
.Final result status can be can be serialised as a dictionary, and converted into JSON and returned to (web) clients.
- Parameters:
code (int) – one of those HTTP codes defined in http — HTTP modules. Default value is
HTTPStatus.OK.value
.text (str) – an optional message.
session_id (str) – an optional web session identifier.
**kwargs – keyword arguments for ResultData class:
- Keyword Arguments:
data (
Union[dict, list]
) – a dictionary or a list of dictionaries to be added.data_name (
str
) – associated name fordata
above.
- add_data(data: Union[dict, list], name=None) ResultStatus
Add a dictionary or a list of dictionaries to
data
, which is an instance ofresult_data.ResultData
.- Parameters:
data – the dictionary or the list of dictionaries to be added.
name (str) – the associated name for
data
.
This method maintains chainability.
- Returns:
self.
- Return type:
- as_dict() dict
Return result status as a dictionary whose root key is
status
.I.e.:
status = ResultStatus(text="I am okay...") status_dict = status.as_dict() # status_dict = {'status': {'code': 200, 'text': 'I am okay...'}} assert status_dict['status']['code'] == HTTPStatus.OK.value assert status_dict['status']['text'] == "I am okay..."
- Returns:
a dictionary representation of the result status.
- Return type:
dict.
- property code: int
Read and write property.
Get and set internal HTTP status code.
It’s one of those HTTP codes defined in http — HTTP modules.
- copy_status_info(source: ResultStatus) ResultStatus
Copy the values of
code
,text
andsession_id
from anResultStatus
instance.- Parameters:
source (
ResultStatus
) – the instance whose status info are copied over.
This method maintains chainability.
- Returns:
self.
- Return type:
- property data: ResultData
Read only property.
Get internal data.
It’s an instance of
result_data.ResultData
.Its default value is
None
.
- serialise_data() Union[dict, list]
All
data
consolidated as a dictionary whose root key isdata
. Return the value (object) of root keydata
.This method is just a wrapper of
result_data.ResultData.serialise_data()
.- Returns:
a dictionary or a list of dictionaries representation of all internal data.
- Return type:
Union[dict, list].
- property session_id: str
Read and write property.
Get and set internal optional web session identifier.
- property text: str
Read and write property.
Get and set internal text message.
- bh_apistatus.result_status.clone_status(source: ResultStatus) ResultStatus
Create an instance of
ResultStatus
using status information from an existing instance.Status information includes the following properties
code
,text
andsession_id
.- Parameters:
source (
ResultStatus
) – the instance whose status info is used to create a new instance.- Returns:
a
ResultStatus
instance.
- bh_apistatus.result_status.make_500_status(text, session_id=None, **kwargs) ResultStatus
Create an instance of
ResultStatus
, with possibly all properities have values assigned.For parameters documentation, please see
ResultStatus
.code
is assigned a value of500
orHTTPStatus.INTERNAL_SERVER_ERROR.value
.- Returns:
a
ResultStatus
instance.
- bh_apistatus.result_status.make_status(code=200, text='', session_id=None, **kwargs) ResultStatus
Create an instance of
ResultStatus
, with possibly all properities have values assigned.For parameters documentation, please see
ResultStatus
.- Returns:
a
ResultStatus
instance.