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 for data above.

add_data(data: Union[dict, list], name=None) ResultStatus

Add a dictionary or a list of dictionaries to data, which is an instance of result_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:

ResultStatus.

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 and session_id from an ResultStatus instance.

Parameters:

source (ResultStatus) – the instance whose status info are copied over.

This method maintains chainability.

Returns:

self.

Return type:

ResultStatus.

property data: ResultData

Read only property.

Get internal data.

It’s an instance of result_data.ResultData.

Its default value is None.

property has_data: bool

Read only property. Return True when data is not None, False otherwise.

serialise_data() Union[dict, list]

All data consolidated as a dictionary whose root key is data. Return the value (object) of root key data.

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 and session_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 of 500 or HTTPStatus.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.