aiocoap.numbers.contentformat module

Module containing the CoRE parameters / CoAP Content-Formats registry

class aiocoap.numbers.contentformat.ContentFormat

Bases: aiocoap.util.ExtensibleIntEnum

Entry in the CoAP Content-Formats registry of the IANA Constrained RESTful Environments (Core) Parameters group

Known entries have .media_type and .encoding attributes:

>>> ContentFormat(0).media_type
'text/plain; charset=utf-8'
>>> int(ContentFormat.by_media_type('text/plain;charset=utf-8'))
>>> ContentFormat(60)
<ContentFormat 60, media_type='application/cbor', encoding='identity'>
>>> ContentFormat(11060).encoding

Unknown entries do not have these properties:

>>> ContentFormat(12345).is_known()
>>> ContentFormat(12345).media_type                    # doctest: +ELLIPSIS
Traceback (most recent call last):
AttributeError: ...

Only a few formats are available as attributes for easy access. Their selection and naming are arbitrary and biased. The remaining known types are available through the by_media_type() class method. >>> ContentFormat.TEXT <ContentFormat 0, media_type=’text/plain; charset=utf-8’, encoding=’identity’>

A convenient property of ContentFormat is that any known content format is true in a boolean context, and thus when used in alternation with None, can be assigned defaults easily:

>>> requested_by_client = ContentFormat.TEXT
>>> int(requested_by_client) # Usually, this would always pick the default
>>> used = requested_by_client or ContentFormat.LINKFORMAT
>>> assert used == ContentFormat.TEXT
classmethod by_media_type(media_type: str, encoding: str = 'identity') → aiocoap.numbers.contentformat.ContentFormat

Produce known entry for a known media type (and encoding, though ‘identity’ is default due to its prevalence), or raise KeyError.

TEXT = <ContentFormat 0, media_type='text/plain; charset=utf-8', encoding='identity'>
LINKFORMAT = <ContentFormat 40, media_type='application/link-format', encoding='identity'>
OCTETSTREAM = <ContentFormat 42, media_type='application/octet-stream', encoding='identity'>
JSON = <ContentFormat 50, media_type='application/json', encoding='identity'>
CBOR = <ContentFormat 60, media_type='application/cbor', encoding='identity'>
SENML = <ContentFormat 112, media_type='application/senml+cbor', encoding='identity'>