blpapi.test.MessageFormatter
- class blpapi.test.MessageFormatter(handle)
MessageFormatter
is used to populate/format a message. It supports writing only once to each field. Attempting to set an already set element will fail.Note that the behavior is undefined if:
A message is formatted with
formatMessageJson()
orformatMessageXml()
orformatMessageDict()
is further formatted.A message formatted with set*() or append*() is further formatted using
formatMessageJson()
orformatMessageXml()
orformatMessageDict()
.
Currently, the JSON and XML formatting methods have some known limitations:
The parsers can not differentiate complex field types (sequences, choices, arrays) that are empty with complex field types that are missing / null. These fields are dropped and absent in the message contents.
Enumerations of type “Datetime”, or any “Datetime” element with timezone offset or sub-microsecond precision (e.g. nanoseconds) are not supported.
- appendElement()
Create an array
blpapi.Element
and append it to theblpapi.Element
on which thisMessageFormatter
is operating.- Raises
Exception – If the schema of the
blpapi.Message
is flat or the currentblpapi.Element
to which this newblpapi.Element
is being appended is not an array, a sequence or a choice.- Return type
- appendValue(value)
Append the specified
value
to the element on which thisMessageFormatter
is operating.
- formatMessageDict(dictMessage)
Format the
blpapi.Message
from itsdict
representation.- Parameters
dictMessage (
dict
) – A dictionary representing the content used to format the message.- Raises
Exception – If the method fails to format the
blpapi.Message
.
The behavior is undefined if further formatting is done using this
MessageFormatter
.- Return type
- formatMessageJson(jsonMessage)
Format the
blpapi.Message
from itsJSON
representation.- Parameters
jsonMessage (str) – A
JSON
-formattedstr
representing the content used to format the message.- Raises
Exception – If the method fails to format the
blpapi.Message
.
The behavior is undefined if further formatting is done using this
MessageFormatter
.- Return type
- formatMessageXml(xmlMessage)
Format the
blpapi.Message
from itsXML
representation.- Parameters
xmlMessage (str) – An
XML
-formattedstr
representing the content used to format the message.- Raises
Exception – If the method fails to format the
blpapi.Message
.
The behavior is undefined if further formatting is done using this
MessageFormatter
.- Return type
- popElement()
Undo the most recent call to
pushElement()
orappendElement()
on thisMessageFormatter
and return the context of theMessageFormatter
to where it was before the call topushElement()
orappendElement()
. OncepopElement()
has been called, it is invalid to attempt to re-visit the same context.- Return type
- pushElement(name)
Change the level at which this
MessageFormatter
is operating to the element specified byname
.After this returns, the context of the
MessageFormatter
is set to the element specified byname
in the schema and any calls tosetElement()
or meth:pushElement are applied at that level.If
name
represents an array of scalars thenappendValue()
must be used to add values.If
name
represents an array of complex types thenappendElement()
must be used.- Parameters
name (
Union
[str
,Name
,bytes
]) – Specifies theblpapi.Element
on which to operate. The element must identify either a choice, a sequence or an array at the current level of the schema or the behavior is undefined.- Raises
Exception – If the
name
is invalid for the current message or if the element identified byname
has already been set.- Return type
- setElement(name, value)
Set the element with the specified
name
to the specifiedvalue
in the currentblpapi.Message
referenced by thisMessageFormatter
.- Parameters
- Raises
Exception – An exception is raised if any of the following are true: - The
name
is invalid for the current message - The element identified byname
has already been set - Thevalue
cannot be assigned to the element identified byname
- Return type