hikari.internal.data_binding#

Data binding utilities.

Module Contents#

class hikari.internal.data_binding.JSONObjectBuilder[source]#

Bases: Dict[str, JSONish]

Helper class used to quickly build JSON objects from various values.

If provided with any values that are hikari.undefined.UNDEFINED, then these values will be ignored.

This speeds up generation of JSON payloads for low level HTTP and websocket API interaction.

Warning

Because this subclasses dict, you should not use the index operator to set items on this object. Doing so will skip any form of validation on the type. Use the put* methods instead.

put(key, value, /)[source]#
put(key: str, value: hikari.undefined.UndefinedNoneOr[T_co], /, *, conversion: Callable[[T_co], JSONish]) None

Put a JSON value.

If the value is hikari.undefined.UNDEFINED it will not be stored.

Parameters:
keystr

The key to give the element.

valuehikari.undefined.UndefinedOr[typing.Any]

The JSON type to put. This may be a non-JSON type if a conversion is also specified. This may alternatively be undefined. In the latter case, nothing is performed.

Other Parameters:
conversiontyping.Optional[typing.Callable[[typing.Any], JSONish]]

The optional conversion to apply.

put_array(key, values, /)[source]#
put_array(key: str, values: hikari.undefined.UndefinedOr[Iterable[T_co]], /, *, conversion: Callable[[T_co], JSONish]) None

Put a JSON array.

If the value is hikari.undefined.UNDEFINED it will not be stored.

If provided, a conversion will be applied to each item.

Parameters:
keystr

The key to give the element.

valueshikari.undefined.UndefinedOr[typing.Iterable[T_co]]

The JSON types to put. This may be an iterable of non-JSON types if a conversion is also specified. This may alternatively be undefined. In the latter case, nothing is performed.

Other Parameters:
conversiontyping.Optional[typing.Callable[[typing.Any], JSONType]]

The optional conversion to apply.

put_snowflake(key, value, /)[source]#

Put a key with a snowflake value into the builder.

If the value is hikari.undefined.UNDEFINED it will not be stored.

Parameters:
keystr

The key to give the element.

valuehikari.undefined.UndefinedNoneOr[hikari.snowflakes.SnowflakeishOr[hikari.snowflakes.Unique]]

The JSON type to put. This may alternatively be undefined, in this case, nothing is performed. This may also be None, in this case the value isn’t cast.

put_snowflake_array(key, values, /)[source]#

Put an array of snowflakes with the given key into this builder.

If the value is hikari.undefined.UNDEFINED it will not be stored.

Each snowflake should be castable to an int.

Parameters:
keystr

The key to give the element.

valueshikari.undefined.UndefinedOr[typing.Iterable[hikari.snowflakes.SnowflakeishOr[hikari.snowflakes.Unique]]]

The JSON snowflakes to put. This may alternatively be undefined. In the latter case, nothing is performed.

class hikari.internal.data_binding.JSONPayload(value, dumps=default_json_dumps)[source]#

Bases: aiohttp.BytesPayload

A JSON payload to use in an aiohttp request.

class hikari.internal.data_binding.StringMapBuilder(arg=(), **kwargs)[source]#

Bases: multidict.MultiDict[str]

Helper class used to quickly build query strings or header maps.

This will consume any items that are not hikari.undefined.UNDEFINED. If a value is unspecified, it will be ignored when inserting it. This reduces the amount of boilerplate needed for generating the headers and query strings for low-level HTTP API interaction, amongst other things.

Warning

Because this subclasses dict, you should not use the index operator to set items on this object. Doing so will skip any form of validation on the type. Use the put* methods instead.

put(key, value, /)[source]#
put(key: str, value: hikari.undefined.UndefinedOr[T_co], /, *, conversion: Callable[[T_co], Stringish]) None

Add a key and value to the string map.

Note

The value will always be cast to a str before inserting it. True will be translated to "true", False will be translated to "false", and None will be translated to "null".

Parameters:
keystr

The string key.

valuehikari.undefined.UndefinedOr[typing.Any]

The value to set.

Other Parameters:
conversiontyping.Optional[typing.Callable[[typing.Any], typing.Any]]

An optional conversion to perform.

class hikari.internal.data_binding.URLEncodedFormBuilder[source]#

Helper class to generate aiohttp.FormData.

hikari.internal.data_binding.Headers[source]#

Type hint for HTTP headers.

hikari.internal.data_binding.JSONArray[source]#

Type hint for a JSON-decoded array representation as a sequence.

hikari.internal.data_binding.JSONObject[source]#

Type hint for a JSON-decoded object representation as a mapping.

hikari.internal.data_binding.JSONish[source]#

Type hint for any valid JSON-decoded type.

hikari.internal.data_binding.Query[source]#

Type hint for HTTP query string.

hikari.internal.data_binding.default_json_dumps: JSONEncoder[source]#

Default json encoder to use.

hikari.internal.data_binding.default_json_loads: JSONDecoder[source]#

Default json decoder to use.