aiocoap.numbers.contentformat module

Module containing the CoRE parameters / CoAP Content-Formats registry

class aiocoap.numbers.contentformat.ContentFormatMeta(cls, bases, classdict, *, boundary=None, _simple=False, **kwds)

Bases: ExtensibleEnumMeta

class aiocoap.numbers.contentformat.ContentFormat(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: 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'))
0
>>> ContentFormat(60)
<ContentFormat 60, media_type='application/cbor', encoding='identity'>
>>> ContentFormat(11060).encoding
'deflate'

Unknown entries do not have these properties:

>>> ContentFormat(12345).is_known()
False
>>> ContentFormat(12345).media_type                    
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 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
0
>>> used = requested_by_client or ContentFormat.LINKFORMAT
>>> assert used == ContentFormat.TEXT
classmethod define(number, media_type: str, encoding: str = 'identity')
classmethod by_media_type(media_type: str, encoding: str = 'identity') ContentFormat

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

is_known()
property media_type: str
property encoding: str
TEXT = 0
LINKFORMAT = 40
OCTETSTREAM = 42
JSON = 50
CBOR = 60
SENML = 112