Skip to content

ietfparse.datastructures

ietfparse.datastructures.Baggage(initial=None)

A W3C Baggage header.

The Baggage header, specified by https://www.w3.org/TR/baggage/, propagates application-defined key=value pairs across service boundaries. This class behaves like an ordered dict that maps a key to its (percent-decoded) value while retaining the optional properties -- the semicolon delimited metadata -- attached to each member.

>>> from ietfparse import headers
>>> baggage = headers.parse_baggage('userId=alice,serverNode=DF%2028')
>>> baggage['userId']
'alice'
>>> baggage['serverNode']
'DF 28'

Values are stored and returned in their decoded form; percent-encoding is applied when the header is serialized with str. Keys are case-sensitive RFC-7230 tokens and duplicate keys resolve to the last value seen, following dict semantics.

Parameters:

Name Type Description Default
initial Mapping[str, str] | Iterable[tuple[str, str]] | None

optional mapping or iterable of (key, value) pairs (or another :class:Baggage) used to seed the header

None

Methods:

Name Description
__setitem__

Set key to value, discarding any existing properties.

get_properties

Return the properties associated with key.

set

Set key to value along with optional properties.

__setitem__(key, value)

Set key to value, discarding any existing properties.

get_properties(key)

Return the properties associated with key.

Parameters:

Name Type Description Default
key str

the member to look up

required

Returns:

Type Description
Sequence[tuple[str, str | None]]

a possibly empty immutable sequence of (name, value) pairs where a value of None denotes a valueless property

Raises:

Type Description
KeyError

if key is not present

set(key, value, *, properties=None)

Set key to value along with optional properties.

Parameters:

Name Type Description Default
key str

the case-sensitive RFC-7230 token naming the member

required
value str

the (decoded) value to associate with key

required
properties Mapping[str, str | None] | Iterable[tuple[str, str | None]] | None

optional metadata as a mapping or an iterable of (name, value) pairs. A property value of None indicates a valueless property such as the k3 in k1=v1;k3.

None

Raises:

Type Description
ValueError

if key or a property name is not a valid token

ietfparse.datastructures.ContentType(content_type, content_subtype, parameters=None, content_suffix=None)

A MIME Content-Type header.

Internet content types are described by the Content-Type header from RFC-2045. It was reused across many other protocol specifications, most notably HTTP (RFC-9110). In its most basic form, a content type header looks like text/html. The primary content type is text with a subtype of html. Content type headers may include parameters as name=value pairs separated by colons.

RFC-6839 added the ability to use a content type to identify the semantic value of a representation with a content type and also identify the document format as a content type suffix. For example, application/vnd.github.v3+json is used to identify documents that match version 3 of the GitHub API that are represented as JSON documents. The same entity encoded as msgpack would have the content type application/vnd.github.v3+msgpack. In this case, the content type identifies the information that is in the document and the suffix is used to identify the content format.

Parameters:

Name Type Description Default
content_type str

the primary content type

required
content_subtype str

the content subtype

required
content_suffix str | None

optional content suffix

None
parameters Mapping[str, str | int] | None

optional dictionary of content type parameters

None

Attributes:

Name Type Description
quality float

The normalized quality metadata associated with this value.

quality property writable

The normalized quality metadata associated with this value.

ietfparse.datastructures.LinkHeader(target, parameters=None)

Represents a single link within a Link header.

The Link header is specified by RFC-8288. It is one of the methods used to represent HyperMedia links between HTTP resources.

Methods:

Name Description
__getitem__

Return the parameter values for param_name as a list.

Attributes:

Name Type Description
parameters Sequence[tuple[str, str]]

Possibly empty sequence of name and value pairs.

rel str

Space-separated relationship parameter.

target str

The target URL of the link.

parameters cached property

Possibly empty sequence of name and value pairs.

Parameters are represented as a sequence since a single parameter may occur more than once.

rel cached property

Space-separated relationship parameter.

This will be the empty string if the rel parameter was not included.

target property

The target URL of the link.

This may be a relative URL so the caller may have to make the link absolute by resolving it against a base URL as described in RFC-3986.

__getitem__(param_name)

Return the parameter values for param_name as a list.

If param_name is not present, then an empty sequence is returned.